2011-05-23 151 views
5

我正處於設計一系列簡單文字遊戲的初期階段,希望能幫助我學習新單詞。我擁有的一個關鍵部分是完全可解析的字典;我希望能夠使用正則表達式在字典中搜索給定的單詞並提取某些其他信息(例如,定義,類型(名詞/動詞...),同義詞,反義詞,演示正在使用的單詞的引號等) 。我目前有Wordbook(Mac應用程序),我發現沒關係,但還沒有弄清楚我是否可以使用python腳本解析它。我假設我不能,並想知道是否有人知道一個合理的字典,將允許這樣做。理想情況下,我會做這一切獨立於互聯網。完全可分析字典/辭典

感謝

回答

7

nltk wordnet corpus提供編程接口到「英語單詞大詞彙數據庫」。您可以根據各種關係導航字詞圖。它符合顯示「定義,詞類,同義詞,反義詞,引用」和「從理想上可下載的字典」中顯示的要求。

另一種選擇是下載recent snapshot of Wiktionary data並將其解析爲可以使用的格式,但這可能涉及一些問題(unless a decent Python Wiktionary parser already exists)。

這裏是打印出使用WORDNET一些屬性的例子:

import textwrap 
from nltk.corpus import wordnet as wn 

POS = { 
    'v': 'verb', 'a': 'adjective', 's': 'satellite adjective', 
    'n': 'noun', 'r': 'adverb'} 

def info(word, pos=None): 
    for i, syn in enumerate(wn.synsets(word, pos)): 
     syns = [n.replace('_', ' ') for n in syn.lemma_names] 
     ants = [a for m in syn.lemmas for a in m.antonyms()] 
     ind = ' '*12 
     defn= textwrap.wrap(syn.definition, 64) 
     print 'sense %d (%s)' % (i + 1, POS[syn.pos]) 
     print 'definition: ' + ('\n' + ind).join(defn) 
     print ' synonyms:', ', '.join(syns) 
     if ants: 
      print ' antonyms:', ', '.join(a.name for a in ants) 
     if syn.examples: 
      print ' examples: ' + ('\n' + ind).join(syn.examples) 
     print 

info('near') 

輸出:

sense 1 (verb) 
definition: move towards 
    synonyms: approach, near, come on, go up, draw near, draw close, come near 
    examples: We were approaching our destination 
      They are drawing near 
      The enemy army came nearer and nearer 

sense 2 (adjective) 
definition: not far distant in time or space or degree or circumstances 
    synonyms: near, close, nigh 
    antonyms: far 
    examples: near neighbors 
      in the near future 
      they are near equals 
... 
+0

感謝您的建議和代碼。看起來像我以後的事情,所以會進一步調查。 – 2011-05-27 13:15:15

2

據我所知,dictionary.com提供非商業用途here一個免費的API。您可能能夠從互聯網上獲取一些數據。