python - 如果手动加载 wordnet,如何在 synsets (nltk) 中使用语言选项?

标签 python path nlp nltk wordnet

出于特定目的,我必须使用 Wordnet 1.6 而不是 nltk 包中实现的当前版本。然后我下载了旧版本here并尝试使用法语选项运行简单的代码提取。

from collections import defaultdict
import nltk
#nltk.download() 
import os
import sys
from nltk.corpus import WordNetCorpusReader

cwd = os.getcwd()
nltk.data.path.append(cwd)
wordnet16_dir="wordnet-1.6/"
wn16_path = "{0}/dict".format(wordnet16_dir)
wn = WordNetCorpusReader(os.path.abspath("{0}/{1}".format(cwd, wn16_path)), nltk.data.find(wn16_path))

senses=wn.synsets('gouvernement',lang=u'fre')

看来我手动下载的wordnet无法链接到nltk外语处理模块的文件,报错如下:

Traceback (most recent call last):
File "C:/Users/Stephanie/Test/temp.py", line 16, in <module>
senses=wn.synsets('gouvernement',lang=u'fre')
File "C:\Python27\lib\site-packages\nltk\corpus\reader\wordnet.py", line 1419, in synsets
self._load_lang_data(lang)
File "C:\Python27\lib\site-packages\nltk\corpus\reader\wordnet.py", line 1064, in _load_lang_data
if lang not in self.langs():
File "C:\Python27\lib\site-packages\nltk\corpus\reader\wordnet.py", line 1088, in langs
fileids = self._omw_reader.fileids()
AttributeError: 'FileSystemPathPointer' object has no attribute 'fileids'

使用英文单词不会产生任何错误(所以不是我没有很好地加载字典):

senses=wn.synsets('government')
print senses

[Synset('government.n.01'), Synset('government.n.02'), Synset('government.n.03'), Synset('politics.n.02')]

如果我使用加载了 nltk 模块的当前版本的 Wordnet,我在使用法语时没有任何问题(所以这不是可选参数的语法问题)

from nltk.corpus import wordnet as wn
senses=wn.synsets('gouvernement',lang=u'fre')
print senses
[Synset('government.n.02'), Synset('opinion.n.05'), Synset('government.n.03'), Synset('rule.n.01'), Synset('politics.n.02'), Synset('government.n.01'), Synset('regulation.n.03'), Synset('reign.n.03')]

但是,准确地说,我真的必须使用旧版本。我想这可能是一个路径问题。我一直在尝试阅读 WordNetCorpusReader 函数的代码,但我对 python 还很陌生,到目前为止我真的看不出问题出在哪里,只是它找不到特殊文件。

所需的文件似乎是位于\nltk_data\corpora\omw\fre 中的wn-data-fre.tab。我很确定我必须使用与 wordnet 1.6 兼容的版本更改文件,但是为什么 WordNetCorpusReader 函数找不到它?

最佳答案

简答:

没有带语言参数的 WordNet 1.6。通过 NLTK 加载不同的 WordNet 时,无法使用 lang='fre'


长答案:

lang=... 参数是使用 Open Multilingual WordNet(OMW:http://compling.hss.ntu.edu.sg/omw/)添加的,它将不同语言的 wordnet 链接到 Princeton WordNet 3.0 版。参见 https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/wordnet.py#L1050 .

lang=...参数调用函数:

def langs(self):
    ''' return a list of languages supported by Multilingual Wordnet '''
    import os
    langs = []
    fileids = self._omw_reader.fileids()
    for fileid in fileids:
        file_name, file_extension = os.path.splitext(fileid)
        if file_extension == '.tab':
            langs.append(file_name.split('-')[-1])

    return langs

查找文件,请参阅 https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/wordnet.py#L1070 :

 f = self._omw_reader.open('{0:}/wn-data-{0:}.tab'.format(lang))

所以 如果 lang == 'fre',则 self._omw_reader = wn-data-fre.tab

而 omw 在 nltk_data/corpora/omw/ 中找不到 wn-data-fre.tab 的主要原因是你设置了 omw_readerwn16_path 初始化 WordNetCorpusReader 对象时,参见 https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/wordnet.py#L1006 .

然后加载法语数据时,找不到self._omw_reader.open('{0:}/wn-data-{0:}.tab'.format(lang))。 (参见 https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/wordnet.py#L1419https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/wordnet.py#L1070)


您可以尝试加载 2 个 WordNet 实例:

import os
from nltk.corpus import wordnet as wn
from nltk.corpus import WordNetCorpusReader

cwd = os.getcwd()
nltk.data.path.append(cwd)
wordnet16_dir="wordnet-1.6/"

wn16_path = "{0}/dict".format(wordnet16_dir)
wn16 = WordNetCorpusReader(os.path.abspath("{0}/{1}".format(cwd, wn16_path)), nltk.data.find(wn16_path))

def synset2offset(ss):
    return str(ss.offset()).zfill(8) + '-' + ss.pos()


wn16_ids = [synset2offset(ss) for ss in wn16.all_synsets()]
wn30_ids = [synset2offset(ss) for ss in wn.all_synsets()]


senses30 = wn.synsets('gouvernement',lang=u'fre')
senses16 = [ss for ss in wn.synsets('gouvernement',lang=u'fre') if synset2offset(ss) in wn16_ids]

关于python - 如果手动加载 wordnet,如何在 synsets (nltk) 中使用语言选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31478152/

相关文章:

vbscript - 如何找到文件路径

java - 在 Java 中将 Windows 路径转换为 ​​URI?

prolog - 普林斯顿 wordnet prolog 文件 - 如何使用 sense key

machine-learning - Google Cloud AutoML 中的身份验证问题

python - 使用 cmd 模块实时更改用户输入

python - 试图做简单的 UDP 服务器客户端时 Argparse 错误

python - Pip freeze 给我这个与 git 相关的错误

macos - 找不到 Bash 命令——$PATH 已损坏

python - Scikit SGDClassifier 使用字母而不是单词作为特征

python - 在 Pandas DataFrame 中计算多个综合分数