2018-10-11
閱讀量:
1731
只要2步!將搜狗詞庫(scel)轉(zhuǎn)為Python可讀的文本

將搜狗詞庫(scel)轉(zhuǎn)化為python可讀的文本(text)的方法
方法 1. 利用R語言(方法簡單)
① 載入詞庫(R語言)
library(Rwordseg)
# getwd()
# setwd("F:/project")##設(shè)置目錄,需要讀者手工調(diào)整
installDict("./word_library_scel/程序猿詞庫.scel", dictname ="Coderwords")
# 函數(shù)installDict(dictpath, dictname,dicttype = c("text", "scel"), load = TRUE)
② 讀取文件(Python語言)
import pandas as pd
Coderwords = pd.read_table("D:\\Users\\R\\R-3.4.4\\library\\Rwordseg\\dict\\Coderwords.dic",header=None,usecols=[0])
如果時間比較趕,那么只學(xué)會方法1就可以啦,下面的方法2請忽略~
方法 2 . 利用Python轉(zhuǎn)化(注:Python版本不一致的話,有可能遇到Bug)
實際運(yùn)行時因為python版本不同轉(zhuǎn)換不能成功,最后終于可行,scel轉(zhuǎn)txt抽取詞庫
#!/bin/python
# -*- coding: utf-8 -*-
import struct
import sys
import binascii
import pdb
try:
reload(sys)
sys.setdefaultencoding('utf-8')
except:
pass
# 搜狗的scel詞庫就是保存的文本的unicode編碼,每兩個字節(jié)一個字符(中文漢字或者英文字母)
# 找出其每部分的偏移位置即可
# 主要兩部分
# 1.全局拼音表,貌似是所有的拼音組合,字典序
# 格式為(index,len,pinyin)的列表
# index: 兩個字節(jié)的整數(shù) 代表這個拼音的索引
# len: 兩個字節(jié)的整數(shù) 拼音的字節(jié)長度
# pinyin: 當(dāng)前的拼音,每個字符兩個字節(jié),總長len
#
# 2.漢語詞組表
# 格式為(same,py_table_len,py_table,{word_len,word,ext_len,ext})的一個列表
# same: 兩個字節(jié) 整數(shù) 同音詞數(shù)量
# py_table_len: 兩個字節(jié) 整數(shù)
# py_table: 整數(shù)列表,每個整數(shù)兩個字節(jié),每個整數(shù)代表一個拼音的索引
#
# word_len:兩個字節(jié) 整數(shù) 代表中文詞組字節(jié)數(shù)長度
# word: 中文詞組,每個中文漢字兩個字節(jié),總長度word_len
# ext_len: 兩個字節(jié) 整數(shù) 代表擴(kuò)展信息的長度,好像都是10
# ext: 擴(kuò)展信息 前兩個字節(jié)是一個整數(shù)(不知道是不是詞頻) 后八個字節(jié)全是0
#
# {word_len,word,ext_len,ext} 一共重復(fù)same次 同音詞 相同拼音表
# 拼音表偏移,
startPy = 0x1540;
# 漢語詞組表偏移
startChinese = 0x2628;
# 全局拼音表
GPy_Table = {}
# 解析結(jié)果
# 元組(詞頻,拼音,中文詞組)的列表
GTable = []
def byte2str(data):
'''''將原始字節(jié)碼轉(zhuǎn)為字符串'''
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:
# 同音詞數(shù)量
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])
# 擴(kuò)展數(shù)據(jù)長度
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":
if data[0:12] != bytes(map(ord,"\x40\x15\x00\x00\x44\x43\x53\x01\x01\x00\x00\x00")):
print ("確認(rèn)你選擇的是搜狗(.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__':
# 將要轉(zhuǎn)換的詞庫添加在這里就可以了
o = ['./word_library_scel/編程語言.scel']
for f in o:
deal(f)
# 保存結(jié)果
f = open('code.txt', 'w')
for word in GTable:
# GTable保存著結(jié)果,是一個列表,每個元素是一個元組(詞頻,拼音,中文詞組),有需要的話可以保存成自己需要個格式
# 我沒排序,所以結(jié)果是按照上面輸入文件的順序
#f.write(unicode(word).encode('GB18030')) # 最終保存文件的編碼,可以自給改
f.write(word[2])
f.write('\n')
f.close()






評論(0)


暫無數(shù)據(jù)
推薦帖子
0條評論
0條評論
0條評論