【发布时间】:2016-03-13 22:36:30
【问题描述】:
我有一个段落数据框,我将其 (*can) 拆分为单词标记和句子标记,并希望在短语“contribute to”或“donate to”的任何实例之后找到所有名词短语发生。
或者实际上是某种形式,所以:
"Contributions are welcome to be made to the charity of your choice."
---> would return: "the charity of your choice"
和
"blah blah blah donations, in honor of Firstname Lastname, can be made to ABC Foundation"
---> would return: "ABC Foundation"
我创建了一个正则表达式变通方案,可以在大约 90% 的时间捕获正确的短语...见下文:
text = nltk.Text(nltk.word_tokenize(x))
donation = TokenSearcher(text).findall(r"<\.> <.*>{,15}? <donat.*|contrib.*> <.*>*? <to> (<.*>+?) <\.|\,|\;> ")
donation = [' '.join(tokens) for tokens in donation]
return donation
我想清理该正则表达式以摆脱“{,15}”要求,因为它缺少一些我需要的值。但是,我对“贪婪”的表达方式不太满意,无法让它正常工作。
所以这句话:
While she lived a full life , had many achievements and made many
**contributions** , FirstName is remembered by most for her cheerful smile ,
colorful track suits , and beautiful necklaces hand made by daughter FirstName .
FirstName always cherished her annual visit home for Thanksgiving to visit
brother FirstName LastName
正在返回:“visit brother FirstName Lastname”,因为之前提到了贡献,即使“to”这个词在 15 个单词之后出现。
【问题讨论】:
-
"即使 'to' 这个词出现在 15 个单词之后。"好吧。这就是
.*所做的。它明确匹配任意数量的字符。 -
我认为后面的 "{,15}" 将其限制为最多 15 个单词。
-
这发生在“contrib.*”匹配之前。
-
投票结束的范围太广了,因为这个不是单一的问题没有单一的答案。
-
@BenPrice 这就是为什么您的“0-15 字”量词没有按您预期的方式工作的原因。 s24.postimg.org/kbgu86crp/words.png
标签: python regex nlp nltk findall