日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

获取训练数据的方式

發布時間:2024/8/23 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取训练数据的方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下載搜狗詞庫

  • https://pinyin.sogou.com/dict/

在官網搜索相關的詞庫下載,比如地名等,然后使用腳本將此條轉換成txt保存, 來源

# -*- coding: utf-8 -*- import os import sys import struct # 主要兩部分 # 1.全局拼音表,貌似是所有的拼音組合,字典序 # 格式為(index,len,pinyin)的列表 # index: 兩個字節的整數 代表這個拼音的索引 # len: 兩個字節的整數 拼音的字節長度 # pinyin: 當前的拼音,每個字符兩個字節,總長len # # 2.漢語詞組表 # 格式為(same,py_table_len,py_table,{word_len,word,ext_len,ext})的一個列表 # same: 兩個字節 整數 同音詞數量 # py_table_len: 兩個字節 整數 # py_table: 整數列表,每個整數兩個字節,每個整數代表一個拼音的索引 # # word_len:兩個字節 整數 代表中文詞組字節數長度 # word: 中文詞組,每個中文漢字兩個字節,總長度word_len # ext_len: 兩個字節 整數 代表擴展信息的長度,好像都是10 # ext: 擴展信息 前兩個字節是一個整數(不知道是不是詞頻) 后八個字節全是0 # # {word_len,word,ext_len,ext} 一共重復same次 同音詞 相同拼音表# 拼音表偏移, startPy = 0x1540;# 漢語詞組表偏移 startChinese = 0x2628;# 全局拼音表 GPy_Table = {}# 解析結果 # 元組(詞頻,拼音,中文詞組)的列表# 原始字節碼轉為字符串 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):# 同音詞數量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])# 擴展數據長度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()if data[0:12] != b"\x40\x15\x00\x00\x44\x43\x53\x01\x01\x00\x00\x00":print(file_name)print ("確認你選擇的是搜狗(.scel)詞庫?")return []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])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'))# 保存結果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

百度搜索相關信息,將table展示的信息整理使用

excel導入web數據
Excel操作的相關快捷鍵

  • Ctrl + -刪除選中的行
  • F4重復上一步的操作
  • F5或Ctrl + G 定位,定位條件,去除空格的方法

總結

以上是生活随笔為你收集整理的获取训练数据的方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。