获取训练数据的方式
下載搜狗詞庫(kù)
- https://pinyin.sogou.com/dict/
在官網(wǎng)搜索相關(guān)的詞庫(kù)下載,比如地名等,然后使用腳本將此條轉(zhuǎn)換成txt保存, 來(lái)源
# -*- coding: utf-8 -*- import os import sys import struct # 主要兩部分 # 1.全局拼音表,貌似是所有的拼音組合,字典序 # 格式為(index,len,pinyin)的列表 # index: 兩個(gè)字節(jié)的整數(shù) 代表這個(gè)拼音的索引 # len: 兩個(gè)字節(jié)的整數(shù) 拼音的字節(jié)長(zhǎng)度 # pinyin: 當(dāng)前的拼音,每個(gè)字符兩個(gè)字節(jié),總長(zhǎng)len # # 2.漢語(yǔ)詞組表 # 格式為(same,py_table_len,py_table,{word_len,word,ext_len,ext})的一個(gè)列表 # same: 兩個(gè)字節(jié) 整數(shù) 同音詞數(shù)量 # py_table_len: 兩個(gè)字節(jié) 整數(shù) # py_table: 整數(shù)列表,每個(gè)整數(shù)兩個(gè)字節(jié),每個(gè)整數(shù)代表一個(gè)拼音的索引 # # word_len:兩個(gè)字節(jié) 整數(shù) 代表中文詞組字節(jié)數(shù)長(zhǎng)度 # word: 中文詞組,每個(gè)中文漢字兩個(gè)字節(jié),總長(zhǎng)度word_len # ext_len: 兩個(gè)字節(jié) 整數(shù) 代表擴(kuò)展信息的長(zhǎng)度,好像都是10 # ext: 擴(kuò)展信息 前兩個(gè)字節(jié)是一個(gè)整數(shù)(不知道是不是詞頻) 后八個(gè)字節(jié)全是0 # # {word_len,word,ext_len,ext} 一共重復(fù)same次 同音詞 相同拼音表# 拼音表偏移, startPy = 0x1540;# 漢語(yǔ)詞組表偏移 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# 獲取一個(gè)詞組的拼音 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]# 拼音索引表長(zhǎng)度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):# 中文詞組長(zhǎng)度c_len = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]# 中文詞組pos += 2word = byte2str(data[pos: pos + c_len])# 擴(kuò)展數(shù)據(jù)長(zhǎng)度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))# 到下個(gè)詞的偏移位置pos += ext_lenreturn GTabledef scel2txt(file_name):print('-' * 60)with open(file_name, 'rb') as f:data = f.read()if data[0:12] != b"\x40\x15\x00\x00\x44\x43\x53\x01\x01\x00\x00\x00":print(file_name)print ("確認(rèn)你選擇的是搜狗(.scel)詞庫(kù)?")return []print("詞 庫(kù) 名:", byte2str(data[0x130:0x338])) # .encode('GB18030')print("詞庫(kù)類(lèi)型:", byte2str(data[0x338:0x540]))print("描述信息:", byte2str(data[0x540:0xd40]))print("詞庫(kù)示例:", byte2str(data[0xd40:startPy]))getPyTable(data[startPy:startChinese])chinese = getChinese(data[startChinese:])print("詞 條: ", len(chinese))return chineseif __name__ == '__main__':# scel所在文件夾路徑in_path = r"scel"# 輸出詞典所在文件夾路徑out_path = r"text"scel_files = [fname for fname in os.listdir(in_path) if fname[-5:] == ".scel"]for scel_file in scel_files:try:file_path=(os.path.join(out_path, str(scel_file).split('.')[0] + '.txt'))# 保存結(jié)果with open(file_path,'a+',encoding='utf-8')as file:for word in scel2txt(os.path.join(in_path, scel_file)):file.write(word[2] + '\n')# os.remove(os.path.join(in_path, scel_file))except Exception as e:print(e)pass百度搜索相關(guān)信息,將table展示的信息整理使用
excel導(dǎo)入web數(shù)據(jù)
Excel操作的相關(guān)快捷鍵
- Ctrl + -刪除選中的行
- F4重復(fù)上一步的操作
- F5或Ctrl + G 定位,定位條件,去除空格的方法
總結(jié)
- 上一篇: mysql left join、righ
- 下一篇: flink批处理