2017-04-19 96 views
1

我是NLTK(http://www.nltk.org/)和python的新手。我希望使用NLTK python库,但使用BNC作为语料库。我不相信这个语料库是通过NLTK数据下载分发的。有没有办法导入NLTK使用的BNC语料库。如果是这样,怎么样?我确实找到了一个名为BNCCorpusReader的函数,但不知道如何使用它。另外,在BNC站点,我能够下载语料库(http://ota.ox.ac.uk/desc/2554)。在NLTK中使用英国国家语料库

http://www.nltk.org/api/nltk.corpus.reader.html?highlight=bnc#nltk.corpus.reader.BNCCorpusReader.word

更新

我已经试过entrophy的建议,但得到以下错误:

raise IOError('No such file or directory: %r' % _path) 
OSError: No such file or directory: 'C:\\Users\\jason\\Documents\\NetBeansProjects\\DemoCollocations\\src\\Corpora\\bnc\\A\\A0\\A00.xml' 

我的代码在语料阅读:

bnc_reader = BNCCorpusReader(root="Corpora/bnc", fileids=r'[A-K]/\w*/\w*\.xml') 

而语料库则是l ocated在: C:\用户\杰森\文件\的NetBeansProjects \ DemoCollocations \ SRC \语料库\ BNC \

+0

你的目的是什么?你必须使用NLTK吗?我不太熟悉Python并且从不使用NLTK,但是我使用Stanford Core NLP在Java中处理了BNC。我的目标是建立一个正确的语料库来解析以获得单词对之间的依赖关系。所以,从BNC的xml文件开始,我用xml解析器重新创建了每个句子。然后我用Core NLP处理每个句子。 如果你的目标只是导入语料库,老实说我不能回应你,但在最后的例子中,你仍然可以创建XML文本的txt格式,并将其传递给python,并最终通过字符串处理它。 –

+0

@ s.dallapalma你好。我不需要使用NLTK,但我确实需要能够使用某些库来查找单词的“搭配”。我看着斯坦福核心NLP,但被告知它没有一个Collocations功能。 – jason

回答

2

在问候NLTK为搭配提取的例子的使用,看看下面的指南:A how-to guide by nltk on collocations extraction

就BNC语料库读者而言,所有的信息都在文档中。

from nltk.corpus.reader.bnc import BNCCorpusReader 
from nltk.collocations import BigramAssocMeasures, BigramCollocationFinder 

# Instantiate the reader like this 
bnc_reader = BNCCorpusReader(root="/path/to/BNC/Texts", fileids=r'[A-K]/\w*/\w*\.xml') 

#And say you wanted to extract all bigram collocations and 
#then later wanted to sort them just by their frequency, this is what you would do. 
#Again, take a look at the link to the nltk guide on collocations for more examples. 

list_of_fileids = ['A/A0/A00.xml', 'A/A0/A01.xml'] 
bigram_measures = BigramAssocMeasures() 
finder = BigramCollocationFinder.from_words(bnc_reader.words(fileids=list_of_fileids)) 
scored = finder.score_ngrams(bigram_measures.raw_freq) 

print(scored) 

的输出将是这个样子:

[(('of', 'the'), 0.004902261167963723), (('in', 'the'),0.003554139346773699), 
(('.', 'The'), 0.0034315828175746064), (('Gift', 'Aid'), 0.0019609044671854894), 
((',', 'and'), 0.0018996262025859428), (('for', 'the'), 0.0018383479379863962), ... ] 

如果你想用分数来排序,你可以尝试这样的事情

sorted_bigrams = sorted(bigram for bigram, score in scored) 

print(sorted_bigrams) 

由于:

[('!', 'If'), ('!', 'Of'), ('!', 'Once'), ('!', 'Particularly'), ('!', 'Raising'), 
('!', 'YOU'), ('!', '‘'), ('&', 'Ealing'), ('&', 'Public'), ('&', 'Surrey'), 
('&', 'TRAINING'), ("'", 'SPONSORED'), ("'S", 'HOME'), ("'S", 'SERVICE'), ... ] 
+0

感谢您的回复。我尝试了您提供的代码,但是我在加载语料库时遇到问题。我相信这可能是由于我缺乏Python经验。我将编辑我的代码并添加错误详细信息,如果您可以提供帮助,我将不胜感激。 – jason

+0

根目录是 /文本。所以你应该改变代码以将读者实体化到这个'bnc_reader = BNCCorpusReader(root =“Corpora/bnc/Texts”,fileids = r'[AK]/\ w */\ w * \。xml')' – entrophy

+0

啊,那是做的。我认为这是因为我下载的文件夹结构不同。我使用: bnc_reader = BNCCorpusReader(root =“Corpora/bnc/2554/download/Texts”,fileids = r'[A-K]/\ w */\ w * \。xml') – jason

相关问题