PageRenderTime 45ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/python/engine/PinYin/tools/annotate.py

http://scim-python.googlecode.com/
Python | 252 lines | 145 code | 13 blank | 94 comment | 13 complexity | 83c1c0d156533738830cb41ec971362c MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import traceback
  4. dic = {}
  5. for l in file ("../pinyin_table.txt"):
  6. ll = unicode(l,"utf8").strip("\n\r")
  7. p = (ll).split(u"\t")
  8. if p[0] not in dic:
  9. dic[p[0]] = [(p[1],int(p[2]))]
  10. else:
  11. q = dic[p[0]]
  12. q.append((p[1],int(p[2])))
  13. q.sort(key = lambda x: x[1], reverse = True)
  14. if q[-1][1] < 20 * q[-2][1]:
  15. del q[-1]
  16. dic[u"的"] = [("de",)]
  17. dic[u"地"] = [("de",)]
  18. #~ import re
  19. #~ output = open("duoyin_phrase","w")
  20. #~ ex = re.compile(ur"[a-zA-Z]+|\w",re.UNICODE)
  21. #~ ex_py = re.compile(ur"[a-z\']+",re.UNICODE)
  22. #~ for l in file("QQ_pinyin_ck1.txt"):
  23. #~ ll = unicode(l[:-1],"utf8").strip("\n\r")
  24. #~ if ex_py.match(ll):
  25. #~ py = ll
  26. #~ else:
  27. #~ for i in ll:
  28. #~ if len( dic[i] ) >1:
  29. #~ print >> output, ll.encode ("utf-8"), py
  30. #~ break
  31. #~ close(output)
  32. #~ dic_duoyin = {}
  33. #~ output = open("duoyin_phrase","w")
  34. #~ for l in file ("duoyin_phrase_manual.txt"):
  35. #~ ll = unicode(l,"utf8")[:-1]
  36. #~ if not ll:
  37. #~ continue
  38. #~ p = (ll).split(" ")
  39. #~ if p[0] in dic_duoyin:
  40. #~ print >> output, p[0].encode ("utf-8"), dic_duoyin[p[0]]
  41. #~ print >> output, p[0].encode ("utf-8"), p[1]
  42. #~ dic_duoyin[p[0]] = p[1]
  43. #~ close(output)
  44. import bz2
  45. dic_duoyin = {}
  46. #~ for l in bz2.BZ2File("duoyin_phrase_manual.txt.bz2","r"):
  47. for l in file("duoyin_phrase_manual.txt"):
  48. ll = unicode(l,"utf8")[:-1]
  49. if not ll:
  50. continue
  51. p = (ll).split(" ")
  52. try:
  53. if p[0] in dic_duoyin:
  54. dic_duoyin[p[0]].append(p[1])
  55. else:
  56. dic_duoyin[p[0]] = [ p[1] ]
  57. except:
  58. print >>sys.stderr, "error on load duoyin_phrase_manual"
  59. sys.exit (1)
  60. #~ import re
  61. #~ ex = re.compile(ur"[a-zA-Z]+|\w",re.UNICODE)
  62. #~ ex_py = re.compile(ur"[a-zA-Z]+",re.UNICODE)
  63. #~ for l in file("qq_pinyin_1.0.txt"):
  64. #~ ll = unicode(l[:-1],"utf8")
  65. #~ if
  66. #~ p = ex.findall(ll)
  67. #~ phrase = u""
  68. #~ py = []
  69. #~ tag = 0
  70. #~ while p:
  71. #~ phrase += p[0]
  72. #~ if len(p) > 1 and ex_py.match(p[1]):
  73. #~ py.append(p[1])
  74. #~ del p[:2]
  75. #~ tag = 1
  76. #~ else:
  77. #~ py.append(dic[p[0]][0][0])
  78. #~ del p[0]
  79. #~ if tag:
  80. #~ print phrase.encode ("utf-8"), "'".join(py)
  81. #~ dic_duoyin = {}
  82. #~ import ZhengJu
  83. #~ editor = ZhengJu.Editor()
  84. #~ i=0
  85. #~ count = 0
  86. #~ for l in file ("google_annotated_uniq.txt"):
  87. #~ ll = unicode(l,"utf8")
  88. #~ p = (ll).split(u"\t")
  89. #~ print p[0]
  90. #~ if len(p[0])==1:
  91. #~ count += 1
  92. #~ i += int(p[2])
  93. #~ try:
  94. #~ editor.reverse(p[0])
  95. #~ pinyin = "'".join (map (str, editor.wordlist))
  96. #~ print (p[0] + "\t" + pinyin + "\t" + p[1]).encode ("utf-8"),
  97. #~ except:
  98. #~ pass
  99. #~ print i,count
  100. #~ for l in file ("google.txt"):
  101. #~ ll = unicode(l,"utf8")
  102. #~ p = (ll).split(u"\t")
  103. #~ print p[0]
  104. #~ if len(p[0])==1:
  105. #~ count += 1
  106. #~ i += int(p[1])
  107. #~ print p[0].encode ("utf-8")
  108. #~ try:
  109. #~ editor.reverse(p[0])
  110. #~ pinyin = "'".join (map (str, editor.wordlist))
  111. #~ print (p[0] + "\t" + pinyin + "\t" + p[1]).encode ("utf-8"),
  112. #~ except:
  113. #~ pass
  114. #~ print i,count
  115. #~
  116. #~ import sets as Set
  117. #~ i=0
  118. #~ count = 0
  119. #~ s = Set.Set()
  120. #~ for l in file ("google_annotated.txt"):
  121. #~ ll = unicode(l,"utf8")
  122. #~ p = (ll).split(u"\t")
  123. #~ if p[0] not in s:
  124. #~ s.add(p[0])
  125. #~ print ll.encode ("utf-8"),
  126. traditional = u"\
  127. 準陞於術萬與專業叢東絲丟兩嚴喪個臨為麗舉義烏樂喬習鄉書買亂爭虧亙亞產畝親褻億僅從侖\
  128. 倉儀們價眾優會傴傘偉傳傷倀倫傖偽佇體僉俠侶僥偵側僑儈儕儂俁儔儼倆儷儉債傾傯僂僨償儻儐儲儺\
  129. 兒兌兗黨蘭關興茲養獸囅內岡冊寫軍農馮沖決況凍淨淒涼減湊凜鳳鳧憑凱擊鑿芻劉則剛創刪別剄剎劊劌\
  130. 剴劑剮劍剝劇勸辦務勱動勵勁勞勢勛勻匭匱區醫華協單賣盧鹵臥衛卻巹廠廳歷厲壓厭厙廁廂厴廈廚廄\
  131. 縣叁參雙發變敘疊葉號嘆嘰嚇呂嗎噸聽啟吳吶嘸囈嘔嚦唄員咼嗆嗚詠嚨嚀響啞噠嘵嗶噦嘩噲嚌噥喲嘜嘮\
  132. 嗩喚嘖嗇囀嚙嘯噴嘍嚳囁噯噓嚶囑嚕囂團園囪圍圇國圖圓聖壙場壞塊堅壇壢壩塢墳墜壟壚壘墾堊墊埡塏\
  133. 塒塤堝塹墮牆壯聲殼壺處備復夠頭夾奪奩奐奮獎奧妝婦媽嫵嫗媯姍婁婭嬈嬌孌娛媧嫻嬰嬋嬸媼嬡嬪嬙嬤\
  134. 孫學孿寧寶實寵審憲宮寬賓寢對尋導壽將爾塵嘗堯尷盡層屜屆屬屢屨嶼歲豈嶇崗峴嵐島嶺巋嶧峽嶠崢巒\
  135. 嶗崍嶄嶸崳嶁巔鞏胇幣帥師幃帳幟帶幀幫幬幘幗冪廣慶廬廡庫應廟龐廢廩開異棄弒張彌弳彎彈強歸當錄\
  136. 彥徹徑徠憶懺憂愾懷態慫憮慪悵愴憐總懟懌戀恆懇惡慟懨愷惻惱惲悅愨懸慳憫驚懼慘懲憊愜慚憚慣慍憤\
  137. 憒懾懣懶懍戇戔戲戧戰戩戶執擴捫掃揚擾撫拋摶摳掄搶護報擔擬攏揀擁攔擰撥擇摯攣撾撻挾撓擋撟掙擠\
  138. 揮撈損撿換搗據擄摑擲撣摻摜攬撳攙擱摟攪攜攝攄擺搖擯攤攖撐攆擷擼攛擻攢敵斂數齋斕斬斷無舊時曠\
  139. 曇晝顯晉曉曄暈暉暫曖朮機殺雜權條來楊榪極構樅樞棗櫪棖槍楓梟檸檉梔柵標棧櫛櫳棟櫨櫟欄樹棲樣欒\
  140. 椏橈楨檔榿橋樺檜槳樁夢檢櫺槨櫝槧欏橢樓欖櫬櫚櫸檻檳櫧橫檣櫻櫫櫥櫓櫞檁歡歟歐殲歿殤殘殞殮殫殯\
  141. 毆毀轂畢斃氈毿氌氣氫氬氳汆匯漢湯洶溝沒灃漚瀝淪滄溈滬濘淚澩瀧瀘濼瀉潑澤涇潔灑窪浹淺漿澆湞濁\
  142. 測澮濟瀏渾滸濃潯濤澇淶漣潿渦渙滌潤澗漲澀澱淵淥漬瀆漸澠漁瀋滲溫灣濕潰濺漵潷滾滯灩灄滿瀅濾濫\
  143. 灤濱灘瀠瀟瀲濰潛瀦瀾瀨瀕灝滅燈靈災燦煬爐燉煒熗點煉熾爍爛烴燭煙煩燒燁燴燙燼熱煥燜燾愛爺牘牽\
  144. 犧犢狀獷猶狽獰獨狹獅獪猙獄猻獫獵獼玀豬貓獻獺璣瑪瑋環現璽玨琺瓏琿璉瑣瓊瑤璦瓔瓚甌電畫暢畬疇\
  145. 癤療瘧癘瘍瘡瘋癰痙癢癆瘓癇痺癉瘞癟癱癮癭癩癬癲皚皺皸盞鹽監蓋盜盤瞇著睜睞瞼睪瞞矚矯磯礬礦碭\
  146. 碼磚硨硯礪礱礫礎碩硤磽確鹼礙磧磣鹼礡禮檷禎禱禍稟祿禪離禿稈種祕積稱穢稅穌穩穡窮竊竅窯竄窩窺\
  147. 竇窶豎競篤筍筆筧箋籠籩篳篩箏籌簽簡簀篋籜籮簞簫簣簍籃籬籪籟糴類糶糲粵糞糧糝緊縶糾紆紅紂纖紇\
  148. 約級紈纊紀紉緯紜純紕紗綱納縱綸紛紙紋紡紐紓線紺紲紱練組紳細織終縐絆紼絀紹繹經紿綁絨結繞絎繪\
  149. 給絢絳絡絕絞統綆綃絹繡綏絛繼綈績緒綾續綺緋綽緄繩維綿綬繃綢綹綣綜綻綰綠綴緇緙緗緘緬纜緹緲緝\
  150. 繢緦綞緞緶緱縋緩締縷編緡緣縉縛縟縝縫縞纏縭縊縑繽縹縵縲纓縮繆繅纈繚繕繒繾繰繯繳纘罌網羅罰罷\
  151. 羆羈羥羨翹耬聳恥聶聾職聹聯聵聰肅腸膚骯腎腫脹脅冑膽勝朧臚脛膠脈膾臟臍腦膿臠腳脫腡臉臘膩膃騰\
  152. 臏輿艤艦艙艫艱艷藝節羋薌蕪蘆蓯葦藶莧萇蒼苧蘇蘋莖蘢蔦塋煢繭荊莢蕘蓽蕎薈薺蕩榮葷滎犖熒蕁藎蓀\
  153. 蔭葒藥蒞萊蓮蒔萵薟獲蕕瑩鶯蘿螢營縈蕭薩蔥蕆蕢蔣蔞藍薊蘺蕷鎣驀薔蘞藺藹蘄蘊藪蘚虜慮虛虯雖蝦蠆\
  154. 蝕蟻螞蠶蜆蠱蠣蟶蠻蟄蛺蟯螄蠐蛻蝸蠟蠅蟈蟬蠍螻蠑蠷釁銜補襯袞襖裊襪襲裝襠褳襝褲襉褸襤見觀規覓\
  155. 視覘覽覺覬覡覿覦覯覲覷觴觸觶譽謄計訂訃認譏訐訌討讓訕訖訓議訊記講諱謳詎訝訥許訛論訟諷設訪訣\
  156. 証詁訶評詛識詐訴診詆謅詞詘詔譯詒誆誄試詿詩詰詼誠誅詵話誕詬詮詭詢詣諍該詳詫諢詡誡誣語誚誤誥\
  157. 誘誨誑說誦誒請諸諏諾讀諑誹課諉諛誰諗調諂諒諄誶談誼謀諶諜謊諫諧謔謁謂諤諭諼讒諮諳諺諦謎諞謨\
  158. 讜謖謝謠謗謚謙謐謹謾謫譾謬譚譖譙讕譜譎讞譴譫讖貝貞負貢財責賢敗賬貨質販貪貧貶購貯貫貳賤賁貰\
  159. 貼貴貺貸貿費賀貽賊贄賈賄貲賃賂贓資賅贐賕賑賚賒賦賭贖賞賜賡賠賧賴贅賻賺賽賾贗贊贈贍贏贛趙趕\
  160. 趨趲躉躍蹌躒踐蹺蹕躚躋踴躊蹤躓躑躡蹣躕躥躪躦軀車軋軌軒軔轉軛輪軟轟軻轤軸軹軼軫轢軺輕軾載輊\
  161. 轎輇輅較輒輔輛輦輩輝輥輞輟輜輳輻輯輸轡轅轄輾轆轍轔辭辯辮邊遼達遷過邁運還這進遠違連遲邇逕跡\
  162. 適選遜遞邐邏遺遙鄧鄺鄔郵鄒鄴鄰郟鄶鄭鄆酈鄖鄲醞醬釅釃釀釋鑑鑾鏨釓釔針釘釗釙釕釷釧釤釩釣鍆釹\
  163. 釵鈣鈦鉅鈍鈔鐘鈉鋇鋼鈑鈐鑰欽鈞鎢鉤鈧鈁鈥鈄鈕鈀鈺錢鉦鉗鈷缽鈳鈽鈸鉞鑽鉬鉭鉀鈿鈾鐵鉑鈴鑠鉛鉚\
  164. 鈰鉉鉈鉍鈮鈹鐸銬銠鉺銪鋮鋏鐃鐺銅鋁銦鎧鍘銖銑鋌銩鏵銓鎩鉿銚鉻銘錚銫鉸銥鏟銃鐋銨銀銣鑄鐒鋪錸\
  165. 鋱鏈鏗銷鎖鋰鋤鍋鋯鋨鏽銼鋝鋒鋅銳銻鋃鋟鋦錒錆鍺錯錨錛錁錕錫錮鑼錘錐錦錈錟錠鍵鋸錳錙鍥鍇鏘鍶\
  166. 鍔鍤鍬鍾鍛鎪鍰鍍鎂鏤鐨鏌鎮鎘鑷鐫鎳鎦鎬鎊鎰鎵鑌鎔鏢鏜鏝鏍鏞鏡鏑鏃鏇鐔鐐鏷鐓鑭鐠鏹鐙鑊鐳鐲鐮\
  167. 鐿鑣鑲長門閂閃閆閉問闖閏闈閑閎間閔閌悶閘鬧閨聞闥閩閭閥閣閡閫鬮閱閬閾閹閶鬩閿閽閻閼闡闌闃闊\
  168. 闋闔闐闕闞隊陽陰陣階際陸隴陳陘陝隉隕險隨隱隸雋難雛讎靂霧霽靄靚靜靨韃韉韋韌韓韙韞韜韻頁頂頃\
  169. 頇項順須頊頑顧頓頎頒頌頏預顱領頗頸頡頰頜潁頦頤頻頹頷穎顆題顎顓顏額顳顢顛顙顥顫顰顴風颮颯颶\
  170. 颼飄飆飛饗饜飢餳飩餼飪飫飭飯飲餞飾飽飼飴餌饒餉餃餅餑餓餘餒餛餡館饋餿饞饃餾饈饉饅饌馬馭馱馴\
  171. 馳驅駁驢駔駛駟駙駒騶駐駝駑駕驛駘驍罵驕驊駱駭駢驪騁驗駿騏騎騍騅驂騙騭騷騖驁騮騫騸驃騾驄驏驟\
  172. 驥驤髏髖髕鬢魘魎魚魷魯魴鱍鱸鮒鮑鱟鮐鮭鮚鮪鮞鱭鮫鮮鯗鱘鯁鱺鰱鰹鯉鰣鰷鯀鯊鯇鯽鯖鯪鯫鯡鯤鯧鯢\
  173. 鯰鯛鯨鯔鰈鰓鱷鰍鰒鰉鰲鰭鰨鰥鰩鰳鰾鱈鱉鰻鱖鱔鱗鱒鱧鳥鳩雞鳶鳴鷗鴉鴇鴆鴣鶇鸕鴨鴦鴟鴝鴛鴕鷥鷙\
  174. 鴯鴰鵂鴿鸞鴻鵓鸝鵑鵠鵝鵒鷴鵜鵡鵲鵪鵯鵬鶉鶘鶚鶻鶩鷂鶼鶴鸚鷓鷚鷯鷦鷲鷸鷺鷹鸛鹺麥麩麼黃黌黷黲\
  175. 黽黿鼉鼴齊齏齒齔齟齡齙齠齜齦齬齪齲齷龍龔龕龜"
  176. def filter(phrase):
  177. for i in phrase:
  178. if i in traditional:
  179. return False
  180. else:
  181. return True
  182. def annotate(phrase):
  183. if len(phrase) == 0:
  184. yield ""
  185. elif len(phrase) == 1:
  186. for i in dic[phrase]:
  187. yield i[0]
  188. else:
  189. for i in range(len(phrase),1, -1):
  190. if phrase[:i] in dic_duoyin:
  191. for q in dic_duoyin[phrase[:i]]:
  192. for p in annotate(phrase[i:]):
  193. yield q + "'" + p
  194. break
  195. else:
  196. for i in dic[phrase[0]]:
  197. for q in annotate(phrase[1:]):
  198. yield i[0] + "'" + q
  199. def annotate_single(phrase):
  200. if len(phrase) == 0:
  201. yield ""
  202. elif len(phrase) == 1:
  203. yield dic[phrase][0]
  204. else:
  205. for i in range(len(phrase),1, -1):
  206. if phrase[:i] in dic_duoyin:
  207. for q in dic_duoyin[phrase[:i]]:
  208. for p in annotate(phrase[i:]):
  209. yield q + "'" + p
  210. break
  211. else:
  212. for q in annotate(phrase[1:]):
  213. yield dic[phrase[0]][0][0] + "'" + q
  214. out_put = open("phrase_freq_annotated.txt", "w")
  215. out_put2 = open("phrase_freq_ambiguous.txt", "w")
  216. out_put3 = open("phrase_freq_guessed.txt", "w")
  217. for l in file("phrase_freq.txt"):
  218. ll = unicode(l[:-1],"utf8")
  219. p = (ll).split(u"\t")
  220. if not filter(p[0]):
  221. continue
  222. if int(p[1]) < 10000:
  223. continue
  224. try:
  225. py = list(annotate(p[0]))
  226. if len(py) == 1:
  227. print >> out_put, u"\t".join ([p[0], py[0], p[1]]).encode ("utf8")
  228. else:
  229. for i in annotate_single(p[0]):
  230. print >> out_put3, u"\t".join ([p[0], i, p[1]]).encode ("utf8")
  231. for i in py:
  232. print >> out_put2, u"\t".join ([p[0], i, p[1]]).encode ("utf8")
  233. except KeyboardInterrupt, e:
  234. print >> sys.stderr, "Exit"
  235. sys.exit (1)
  236. except:
  237. print >>sys.stderr, p[0].encode("utf-8")
  238. traceback.print_exc()