爬取词库,使用jieba分词库,自定义dict.txt文件+将搜狗词库.scel文件为.txt文件
生活随笔
收集整理的這篇文章主要介紹了
爬取词库,使用jieba分词库,自定义dict.txt文件+将搜狗词库.scel文件为.txt文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一:爬取詞庫,使用jieba分詞庫,自定義dict.txt文件
import jiebafrom urllib.request import urlopen from bs4 import BeautifulSoup# 來源于地圖搜索數(shù)據(jù),按照網(wǎng)民輸入習慣精心篩選使用較多的詞條。包含城市地名、公交、購物、餐飲、樓盤等各種信息,適合本地區(qū)網(wǎng)友使用 url = "http://search.qinggl.com/dict-3687.html" html = urlopen(url).read().decode('utf-8') soup = BeautifulSoup(html, features='lxml') urls = soup.find_all("div", {"id": "box-new","class":"wordbox basic"}) print(urls) print(type(urls)) x = str(urls) x = x.replace("><span>","") x = x.replace("<span>","") x = x.replace("</span","\n") x = x.replace("></div>","") x = x.split("\n") x = x[1:] x.pop() print(len(x)) print(x)jieba.load_userdict("./dict.txt") word_list = jieba.cut("我今天不處理逾期信用貸款,因為成都木材防腐廠根本打不開.我?guī)滋煜肴フZ過添情網(wǎng)吧開心happy一下。") print("|".join(word_list)) # #D:\anacoda\envs\nlp\Lib\site-packages\jieba二:將搜狗詞庫.scel文件為.txt文件
# -*- coding: utf-8 -*- import struct import os# 主要兩部分 # 1.全局拼音表,貌似是所有的拼音組合,字典序 # 格式為(index,len,pinyin)的列表 # index: 兩個字節(jié)的整數(shù) 代表這個拼音的索引 # len: 兩個字節(jié)的整數(shù) 拼音的字節(jié)長度 # pinyin: 當前的拼音,每個字符兩個字節(jié),總長len # # 2.漢語詞組表 # 格式為(same,py_table_len,py_table,{word_len,word,ext_len,ext})的一個列表 # same: 兩個字節(jié) 整數(shù) 同音詞數(shù)量 # py_table_len: 兩個字節(jié) 整數(shù) # py_table: 整數(shù)列表,每個整數(shù)兩個字節(jié),每個整數(shù)代表一個拼音的索引 # # word_len:兩個字節(jié) 整數(shù) 代表中文詞組字節(jié)數(shù)長度 # word: 中文詞組,每個中文漢字兩個字節(jié),總長度word_len # ext_len: 兩個字節(jié) 整數(shù) 代表擴展信息的長度,好像都是10 # ext: 擴展信息 前兩個字節(jié)是一個整數(shù)(不知道是不是詞頻) 后八個字節(jié)全是0 # # {word_len,word,ext_len,ext} 一共重復same次 同音詞 相同拼音表# 拼音表偏移, startPy = 0x1540;# 漢語詞組表偏移 startChinese = 0x2628;# 全局拼音表 GPy_Table = {}# 解析結(jié)果 # 元組(詞頻,拼音,中文詞組)的列表# 原始字節(jié)碼轉(zhuǎn)為字符串 def byte2str(data):pos = 0str = ''while pos < len(data):c = chr(struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0])if c != chr(0):str += cpos += 2return str# 獲取拼音表 def getPyTable(data):data = data[4:]pos = 0while pos < len(data):index = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]pos += 2lenPy = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]pos += 2py = byte2str(data[pos:pos + lenPy])GPy_Table[index] = pypos += lenPy# 獲取一個詞組的拼音 def getWordPy(data):pos = 0ret = ''while pos < len(data):index = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]ret += GPy_Table[index]pos += 2return ret# 讀取中文表 def getChinese(data):GTable = []pos = 0while pos < len(data):# 同音詞數(shù)量same = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]# 拼音索引表長度pos += 2py_table_len = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]# 拼音索引表pos += 2py = getWordPy(data[pos: pos + py_table_len])# 中文詞組pos += py_table_lenfor i in range(same):# 中文詞組長度c_len = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]# 中文詞組pos += 2word = byte2str(data[pos: pos + c_len])# 擴展數(shù)據(jù)長度pos += c_lenext_len = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]# 詞頻pos += 2count = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]# 保存GTable.append((count, py, word))# 到下個詞的偏移位置pos += ext_lenreturn GTabledef scel2txt(file_name):print('-' * 60)with open(file_name, 'rb') as f:data = f.read()print("詞庫名:", byte2str(data[0x130:0x338])) # .encode('GB18030')print("詞庫類型:", byte2str(data[0x338:0x540]))print("描述信息:", byte2str(data[0x540:0xd40]))print("詞庫示例:", byte2str(data[0xd40:startPy]))getPyTable(data[startPy:startChinese])getChinese(data[startChinese:])return getChinese(data[startChinese:])#scel2txt(.ChengDuInformationWord.scel)if __name__ == '__main__':# scel所在文件夾路徑in_path = r"D:\InternetOffice\code"# 輸出詞典所在文件夾路徑out_path = r"D:\InternetOffice\code"#fname = "ChengDuInformationWord.scel"fin = [fname for fname in os.listdir(in_path) if fname[-5:] == ".scel"]for f in fin:try:for word in scel2txt(os.path.join(in_path, f)):file_path = (os.path.join(out_path, str(f).split('.')[0] + '.txt'))# 保存結(jié)果with open(file_path, 'a+', encoding='utf-8')as file:file.write(word[2] + '\n')os.remove(os.path.join(in_path, f))except Exception as e:print(e)pass總結(jié)
以上是生活随笔為你收集整理的爬取词库,使用jieba分词库,自定义dict.txt文件+将搜狗词库.scel文件为.txt文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 研讨会 | 知识图谱前沿技术课程暨学术研
- 下一篇: ltp︱基于ltp的无监督信息抽取模块