Pau*_*son 5 python macos parsing dictionary words
我正处于设计一系列简单文字游戏的早期阶段,我希望能帮助我学习新单词.我所拥有的一个关键部分是完全可解析的字典; 我希望能够使用正则表达式搜索字典中的给定单词并提取某些其他信息(例如定义,类型(名词/动词......),同义词,反义词,演示正在使用的单词的引号等) .我目前有Wordbook(mac app),我发现没问题,但还没弄明白我是否可以使用python脚本解析它.我假设我不能,并且想知道是否有人知道一个合理的字典将允许这个.理想情况下,我会完全独立于互联网.
谢谢
该NLTK WordNet的语料库提供编程接口到"英语单词大词汇数据库".您可以根据各种关系导航字图.它符合显示"定义,词性,同义词,反义词,引号"和"从理想情况下可下载的字典"的要求.
另一种选择是下载最近的维基数据快照并将其解析为您可以使用的格式,但这可能有点涉及(除非已经存在一个像样的Python维基解析器).
以下是使用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')
Run Code Online (Sandbox Code Playgroud)
输出:
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
...
Run Code Online (Sandbox Code Playgroud)