天天看點

python - 搜狗詞庫 (.scel 檔案轉 txt)

python - 搜狗詞庫 (.scel 檔案轉 txt)

解析示例:

# -*- coding: utf-8 -*-
import struct
import os
import time
'''
2021-04-21 搜狗詞庫 scel 轉 txt
'''
# 主要兩部分
# 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次 同音詞 相同拼音表
#
_debug = True
_debug = False
# scel所在檔案夾路徑
in_path = r"/Users/xxx/Downloads/搜狗詞庫"
# 輸出詞典所在檔案夾路徑
out_path = r"/Users/xxx/Downloads/搜狗詞庫/output"
# 拼音表偏移,
startPy = 0x1540;
# 漢語詞組表偏移
startChinese = 0x2628;
# 全局拼音表
GPy_Table = {}
# 解析結果
# 元組(詞頻,拼音,中文詞組)的清單

class LibInfo:
    name = ''
    type = ''
    desc = ''
    case = ''
    pinyinDict = None
    cn_chars = None

# 原始位元組碼轉為字元串
def byte2str(data):
    pos = 0
    str = ''
    while pos < len(data):
        c = chr(struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0])
        if c != chr(0):
            str += c
        pos += 2
    return str

# 擷取拼音表
def getPyTable(data):
    data = data[4:]
    pos = 0
    while pos < len(data):
        index = struct.unpack('H', bytes([data[pos],data[pos + 1]]))[0]
        pos += 2
        lenPy = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]
        pos += 2
        py = byte2str(data[pos:pos + lenPy])

        GPy_Table[index] = py
        pos += lenPy
    return GPy_Table

# 擷取一個詞組的拼音
def getWordPy(data):
    pos = 0
    ret = ''
    while pos < len(data):
        index = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]
        ret += GPy_Table[index]
        pos += 2
    return ret

# 讀取中文表
def getChinese(data):
    GTable = []
    pos = 0
    while pos < len(data):
        # 同音詞數量
        same = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]

        # 拼音索引表長度
        pos += 2
        py_table_len = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]

        # 拼音索引表
        pos += 2
        py = getWordPy(data[pos: pos + py_table_len])

        # 中文詞組
        pos += py_table_len
        for i in range(same):
            # 中文詞組長度
            c_len = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]
            # 中文詞組
            pos += 2
            word = byte2str(data[pos: pos + c_len])
            # 擴充資料長度
            pos += c_len
            ext_len = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]
            # 詞頻
            pos += 2
            count = struct.unpack('H', bytes([data[pos], data[pos + 1]]))[0]

            # 儲存
            GTable.append((count, py, word))

            # 到下個詞的偏移位置
            pos += ext_len
    return GTable


def getLibInfo(file_name):
    print('-' * 60)
    with open(file_name, 'rb') as f:
        data = f.read()

    libInfo = LibInfo()
    libInfo.name = byte2str(data[0x130:0x338])
    libInfo.type = byte2str(data[0x338:0x540])
    libInfo.desc = byte2str(data[0x540:0xd40])
    libInfo.case = byte2str(data[0xd40:startPy])
    print("詞庫名:", libInfo.name) # .encode('GB18030')
    print("詞庫類型:",libInfo.type)
    print("描述資訊:", libInfo.desc)
    print("詞庫示例:",libInfo.case)

    libInfo.pinyinDict = getPyTable(data[startPy:startChinese])
    # print(lst)
    libInfo.cn_chars = getChinese(data[startChinese:])
    return libInfo

if __name__ == '__main__':
    fin = [fname for fname in os.listdir(in_path) if fname[-5:] == ".scel"]
    print('所有詞庫檔案',fin)
    for f in fin:
        file_path=(os.path.join(out_path, str(f).split('.')[0] + '.txt'))
        try:
            dt = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
            libInfo = getLibInfo(os.path.join(in_path, f))
            # 儲存結果
            with open(file_path,'w+',encoding='utf-8')as file:
                file.write('#詞庫名:%s\r\n'%libInfo.name)
                file.write('#詞庫類型:%s\r\n'%libInfo.type)
                file.write('#描述資訊:%s\r\n'%libInfo.desc)
                file.write('#詞庫示例:%s\r\n'%libInfo.case)
                file.write('#詞數:%s\r\n'%len(libInfo.cn_chars))
                file.write('#時間:%s\r\n'%dt)
                file.write('#%s\r\n'%('~'*30))
                for (freq,py,w) in libInfo.cn_chars:
                    file.write('%s\t%s\t%s\r\n'%(w,py,freq))
            print('解析 %s -> %s'%(f,file_path))
        except Exception as e:
            print('解析異常:'+f,e)
        if _debug:
            print('終止 debug=',_debug)
            break


'''
>>> f = open('test.txt', 'w',encoding='utf-8') # 若是'wb'就表示寫二進制檔案
>>> f.write('Hello, world!')
>>> f.close()
python檔案對象提供了兩個“寫”方法: write() 和 writelines()。
write()方法和read()、readline()方法對應,是将字元串寫入到檔案中。
writelines()方法和readlines()方法對應,也是針對清單的操作。它接收一個字元串清單作為參數,将他們寫入到檔案中,換行符不會自動的加入,是以,需要顯式的加入換行符。
關于open()的mode參數:
'r':讀
'w':寫
'a':追加
'r+' == r+w(可讀可寫,檔案若不存在就報錯(IOError))
'w+' == w+r(可讀可寫,檔案若不存在就建立)
'a+' ==a+r(可追加可寫,檔案若不存在就建立)
對應的,如果是二進制檔案,就都加一個b就好啦:
'rb'  'wb'  'ab'  'rb+'  'wb+'  'ab+'
'''