我使用NLTK查找单词的一致性,但我不知道如何获取所有结果并将其放在list
或set
中。
例如:
text.concordance(word)
仅打印前25个结果。
答案 0 :(得分:4)
<强> TL; DR 强>
text.concordance(lines=100)
从代码中https://github.com/nltk/nltk/blob/develop/nltk/text.py#L323:
def concordance(self, word, width=79, lines=25):
"""
Print a concordance for ``word`` with the specified context window.
Word matching is not case-sensitive.
:seealso: ``ConcordanceIndex``
"""
if '_concordance_index' not in self.__dict__:
#print("Building index...")
self._concordance_index = ConcordanceIndex(self.tokens,
key=lambda s:s.lower())
self._concordance_index.print_concordance(word, width, lines)