调用NLTK的一致性-如何在使用的单词之前/之后获取文本? [英] Calling NLTK's concordance - how to get text before/after a word that was used?

查看:57
本文介绍了调用NLTK的一致性-如何在使用的单词之前/之后获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出concordace返回的实例之后是什么文本.因此,例如,如果您看一个示例,它们在搜索文本"部分中给出,他们得到怪异"一词的一致.在出现一个可怕的事件之后,您将如何获得紧随其后的单词?

I'm would like to find out what text comes after the instance that concordace returns. So for instance, if you look at an example they give in 'Searching Text' section, they get concordance of word 'monstrous'. How would you get words that come right after an instance of monstrous?

推荐答案

import nltk
import nltk.book as book
text1 = book.text1
c = nltk.ConcordanceIndex(text1.tokens, key = lambda s: s.lower())
print([text1.tokens[offset+1] for offset in c.offsets('monstrous')])

收益

['size', 'bulk', 'clubs', 'cannibal', 'and', 'fable', 'Pictures', 'pictures', 'stories', 'cabinet', 'size']


我通过查找concordance方法的定义方式发现了这一点.


I found this by looking up how the concordance method is defined.

这表明text1.concordance是在/usr/lib/python2.7/dist-packages/nltk/text.py中定义的:

In [107]: text1.concordance?
Type:       instancemethod
Base Class: <type 'instancemethod'>
String Form:    <bound method Text.concordance of <Text: Moby Dick by Herman Melville 1851>>
Namespace:  Interactive
File:       /usr/lib/python2.7/dist-packages/nltk/text.py

在该文件中您会找到

def concordance(self, word, width=79, lines=25):
    ... 
        self._concordance_index = ConcordanceIndex(self.tokens,
                                                   key=lambda s:s.lower())
    ...            
    self._concordance_index.print_concordance(word, width, lines)

这显示了如何实例化ConcordanceIndex对象.

This shows how to instantiate ConcordanceIndex objects.

在同一文件中,您还会找到:

And in the same file you'll also find:

class ConcordanceIndex(object):
    def __init__(self, tokens, key=lambda x:x):
        ...
    def print_concordance(self, word, width=75, lines=25):
        ...
        offsets = self.offsets(word)
        ...
        right = ' '.join(self._tokens[i+1:i+context])

在IPython解释器中进行了一些实验,这表明self.offsets('monstrous')给出了一个数字(偏移量)列表,可以在其中找到单词monstrous.您可以使用self._tokens[offset]访问与text1.tokens[offset]相同的实际单词.

With some experimentation in the IPython interpreter, this shows self.offsets('monstrous') gives a list of numbers (offsets) where the word monstrous can be found. You can access the actual words with self._tokens[offset], which is the same as text1.tokens[offset].

所以monstrous之后的下一个单词由text1.tokens[offset+1]给出.

So the next word after monstrous is given by text1.tokens[offset+1].

这篇关于调用NLTK的一致性-如何在使用的单词之前/之后获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆