下载搜狗词库
- 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
定位,定位条件,去除空格的方法