【发布时间】:2013-09-02 23:03:42
【问题描述】:
我有同样的问题在这个链接Python extract sentence containing word 中讨论过,但不同的是我想在同一个句子中找到 2 个单词。我需要从包含 2 个特定单词的语料库中提取句子。请问有谁可以帮帮我吗?
【问题讨论】:
标签: python regex nltk sentence text-segmentation
我有同样的问题在这个链接Python extract sentence containing word 中讨论过,但不同的是我想在同一个句子中找到 2 个单词。我需要从包含 2 个特定单词的语料库中提取句子。请问有谁可以帮帮我吗?
【问题讨论】:
标签: python regex nltk sentence text-segmentation
将TextBlob 包与Python 的内置sets 一起使用会很简单。
基本上,遍历文本中的句子,并检查句子中的单词集与搜索词之间是否存在交集。
from text.blob import TextBlob
search_words = set(["buy", "apples"])
blob = TextBlob("I like to eat apple. Me too. Let's go buy some apples.")
matches = []
for sentence in blob.sentences:
words = set(sentence.words)
if search_words & words: # intersection
matches.append(str(sentence))
print(matches)
# ["Let's go buy some apples."]
更新: 或者,更 Python 化地,
from text.blob import TextBlob
search_words = set(["buy", "apples"])
blob = TextBlob("I like to eat apple. Me too. Let's go buy some apples.")
matches = [str(s) for s in blob.sentences if search_words & set(s.words)]
print(matches)
# ["Let's go buy some apples."]
【讨论】:
如果这是你的意思:
import re
txt="I like to eat apple. Me too. Let's go buy some apples."
define_words = 'some apple'
print re.findall(r"([^.]*?%s[^.]*\.)" % define_words,txt)
Output: [" Let's go buy some apples."]
你也可以试试:
define_words = raw_input("Enter string: ")
检查句子是否包含定义的单词:
import re
txt="I like to eat apple. Me too. Let's go buy some apples."
words = 'go apples'.split(' ')
sentences = re.findall(r"([^.]*\.)" ,txt)
for sentence in sentences:
if all(word in sentence for word in words):
print sentence
【讨论】:
我想你想用 nltk 来回答。而且我猜这两个词不需要是连续的吧?
>>> from nltk.tokenize import sent_tokenize, word_tokenize
>>> text = 'I like to eat apple. Me too. Let's go buy some apples.'
>>> words = ['like', 'apple']
>>> sentences = sent_tokenize(text)
>>> for sentence in sentences:
... if (all(map(lambda word: word in sentence, words))):
... print sentence
...
I like to eat apple.
【讨论】: