2011-05-23 52 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。您可能能够从互联网上获取一些数据。