The current mainstream Linux distributions such as Fedora and Ubuntu default Chinese input method are ibus-pinyin, but is not a taste of the ibus-pinyin of the thesaurus too few, input Chinese phrases to one of the selected Chinese characters, envy of windows with Sogou Pinyin classmate, in There are good people on the Internet to propose solutions.
Usage: first to the Sogou Pinyin official website Download the thesaurus file you think is more commonly used, note that these thesaurus files are usually named after the. scel suffix, and then copy the following code into a Python script, save it to the folder where the thesaurus file is located, run the script, and get sougou.txt;
Open ibus Pinyin "preferences" user dictionary to import just sougou.txt.
#!/usr/bin/python#-*-coding:utf-8-*-import structimport sysimport binasciiimport pdb# Sogou's scel thesaurus is the Unicode encoding of the saved text, One character per two bytes (Chinese characters or English letters) # Find the offset of each part # The main two parts # 1. Global phonetic table, seemingly is all phonetic combination, dictionary order # Format (index,len,pinyin) List # index: two-byte integer The index that represents this phonetic # len: two bytes of integer pinyin byte length # pinyin: the current pinyin, two bytes per character, total length len## 2. The Chinese phrase Table # format is (same,py_table_len,py_table, {Word_len,word,ext_len,ext}) A list of # same: Two-byte integer homonyms number # Py_table_len: Two-byte integer # py_table: integer list, two bytes per integer, each integer representing a phonetic index # # word_l En: Two byte integers for Chinese phrase byte number length # Word: Chinese characters, two bytes per Chinese character, total length word_len# Ext_len: Two-byte integer representing the length of the extended information, as if it were 10# ext: Extended Information The first two bytes is an integer (do not know is the word frequency) after eight bytes are all 0## {word_len,word,ext_len,ext} altogether repeats same times homonyms the same phonetic table # Pinyin table offset, startpy = 0x1540# Chinese phrase table offset start Chinese = 0x2628# Global phonetic Table gpy_table = {}# Parse result # tuple (word frequency, pinyin, Chinese phrase) list gtable = []def byte2str (data): ' ' Convert raw bytecode to string ' ' i = 0 length = Len (data) ret = U ' while i < length:x = Data[i] + data[i + 1] t = UNICHR (STRUCT.UNPA CK (' H ', x) [0]) If t = = U ' \ r ': ret + = U ' \ n ' elif t! = U ': ret + = t i + = 2 RE Turn ret# get pinyin table 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# get a phrase of Pinyin def ge Twordpy (data): pos = 0 length = len (data) ret = [] While pos < Length:index = Struct.unpack (' H ', Da Ta[pos] + Data[pos + 1]) [0] Ret.append (Gpy_table[index]) pos + = 2 return ' \ '. Join (ret) # get a phrase def Getwor D (data): pos = 0 length = len (data) ret = U ' while pos < Length:index = Struct.unpack (' H ', Data[po S] + Data[pos + 1]) [0] RET + = Gpy_table[index] pos + = 2 return ret# read Chinese table def getchinese (data): #import PDB # Pdb.set_trace ( ) pos = 0 length = len (data) while Pos < length: # homonyms Number same = Struct.unpack (' H ', Data[pos] + D Ata[pos + 1]) [0] # print ' [same]: ', same, # Pinyin Index Table length pos + = 2 Py_table_len = struct.unpack (' H ', Data[pos] + Data[pos + 1]) [0] # Pinyin Index Table pos + 2 py = getwordpy (Data[pos:pos + Py_table_len]) # Chinese phrase pos + = Py_table_len for i in Xrange (same): # Chinese phrase length C_len = struct.unpack (' H ', D Ata[pos] + Data[pos + 1]) [0] # Chinese pos + + 2 Word = byte2str (Data[pos:pos + C_len]) # Extended Data Length pos + = C_len Ext_len = struct.unpack (' H ', Data[pos] + Data[pos + 1]) [0] # Word frequency pos + = 2 count = Struct.unpack (' H ', Data[pos] + Data[pos + 1]) [0] # Save GTa Ble.append ((Count, Py, WORD) # to the next word offset position pos + = Ext_lendef Deal (file_name): print '-' * 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 "Confirm your choice The choice is Sogou (. scel) thesaurus? " Sys.exit (0) # pdb.set_trace () print "Thesaurus name:", Byte2str (data[0x130:0x338]) #. Encode (' GB18030 ') print "Thesaurus type:", byte 2str (data[0x338:0x540]) #. Encode (' GB18030 ') print "description information:", Byte2str (Data[0x540:0xd40]) #. Encode (' GB18030 ') print "Thesaurus Example:", Byte2str (Data[0xd40:startpy]) #. Encode (' GB18030 ') getpytable (Data[startpy:startchinese]) Getchinese (data [Startchinese:]) if __name__ = = ' __main__ ': # The Thesaurus to be converted is added here on the o = [] import os for F in Os.listdir ("./"): If F.find ('. S Cel ')! = -1:o.append (f) for F in O:deal (f) # Save result f = open (' Sougou.txt ', ' W ') for count, p Y, Word in GTable: # GTable holds the result, is a list, each element is a tuple (word frequency, pinyin, Chinese phrases), and if necessary can be saved to a format that you need # I don't sort, so the result is pressAccording to the order of the input files above # F.write (Unicode (' {% (count) s} '% {' count ': count} + py + ' + word). Encode (' UTF-8 ') # finally save the encoding of the file, can be self-sufficient Change F.write (Unicode ('%s%s '% (Word, py, count)). Encode (' UTF-8 ') f.write (' \ n ') F.close ()
IBUs Pinyin Sogou Thesaurus