解析搜狗詞庫(python)



#!/usr/bin/python
# -*- coding: utf-8 -*-


import struct
import sys
import binascii 
import pdb
#搜狗的scel詞庫就是保存的文本的unicode編碼,每兩個字節一個字符(中文漢字或者英文字母)
#找出其每部分的偏移位置即可
#主要兩部分
#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 ={}

#解析結果
#元組(詞頻,拼音,中文詞組)的列表
GTable = []

def byte2str(data):
    '''將原始字節碼轉為字符串'''
    i = 0;
    length = len(data)
    ret = u''
    while i < length:
        x = data[i] + data[i+1]
        t = unichr(struct.unpack('H',x)[0])
        if t == u'\r':
            ret += u'\n'
        elif t != u' ':
            ret += t
        i += 2
    return ret
#獲取拼音表
def getPyTable(data):

    if data[0:4] != "\x9D\x01\x00\x00":
        return None
    data = data[4:]
    pos = 0
    length = len(data)
    while pos < length:
        index = struct.unpack('H',data[pos]+data[pos+1])[0]
        #print index,
        pos += 2
        l = struct.unpack('H',data[pos]+data[pos+1])[0]
        #print l,
        pos += 2
        py = byte2str(data[pos:pos+l])
        #print py
        GPy_Table[index]=py
        pos += l

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

#獲取一個詞組
def getWord(data):
    pos = 0
    length = len(data)
    ret = u''
    while pos < length:
        
        index = struct.unpack('H',data[pos]+data[pos+1])[0]
        ret += GPy_Table[index]
        pos += 2    
    return ret

#讀取中文表    
def getChinese(data):
    #import pdb
    #pdb.set_trace()
    
    pos = 0
    length = len(data)
    while pos < length:
        #同音詞數量
        same = struct.unpack('H',data[pos]+data[pos+1])[0]
        #print '[same]:',same,
        
        #拼音索引表長度
        pos += 2
        py_table_len = struct.unpack('H',data[pos]+data[pos+1])[0]
        #拼音索引表
        pos += 2
        py = getWordPy(data[pos: pos+py_table_len])

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

            #保存
            GTable.append((count,py,word))
        
            #到下個詞的偏移位置
            pos +=  ext_len


def deal(file_name):
    print '-'*60
    f = open(file_name,'rb')
    data = f.read()
    f.close()
    
    
    if data[0:12] !="\x40\x15\x00\x00\x44\x43\x53\x01\x01\x00\x00\x00":
        print "確認你選擇的是搜狗(.scel)詞庫?"
        sys.exit(0)
    #pdb.set_trace()
    
    print "詞庫名:" ,byte2str(data[0x130:0x338])#.encode('GB18030')
    print "詞庫類型:" ,byte2str(data[0x338:0x540])#.encode('GB18030')
    print "描述信息:" ,byte2str(data[0x540:0xd40])#.encode('GB18030')
    print "詞庫示例:",byte2str(data[0xd40:startPy])#.encode('GB18030')
    
    getPyTable(data[startPy:startChinese])
    getChinese(data[startChinese:])
    
        
if __name__ == '__main__':

    #將要轉換的詞庫添加在這里就可以了
    o = ['計算機詞匯大全【官方推薦】.scel',
    'IT計算機.scel',
    '計算機詞匯大全【官方推薦】.scel',
    '北京市城市信息精選.scel',
    '常用餐飲詞匯.scel',
    '成語.scel',
    '成語俗語【官方推薦】.scel',
    '法律詞匯大全【官方推薦】.scel',
    '房地產詞匯大全【官方推薦】.scel',
    '手機詞匯大全【官方推薦】.scel',
    '網絡流行新詞【官方推薦】.scel',
    '歇后語集錦【官方推薦】.scel',
    '飲食大全【官方推薦】.scel',
    ]

    #for f in o:
    #    deal(f)
    
    print sys.argv[1]
    deal( sys.argv[1] )
    #保存結果  
    f = open('sougou.txt','w')
    for count,py,word in GTable:
        #GTable保存着結果,是一個列表,每個元素是一個元組(詞頻,拼音,中文詞組),有需要的話可以保存成自己需要個格式
        #我沒排序,所以結果是按照上面輸入文件的順序
        f.write( unicode('{%(count)s}' %{'count':count}+py+' '+ word).encode('GB18030') )#最終保存文件的編碼,可以自給改
        f.write('\n')
    f.close()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM