确定句子中是否包含单词列表? [英] Determine if a list of words is in a sentence?

查看:132
本文介绍了确定句子中是否包含单词列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法(模式,Python或NLTK等)来检测句子中是否包含单词列表.

Is there a way (Pattern or Python or NLTK, etc) to detect of a sentence has a list of words in it.

The cat ran into the hat, box, and house. | The list would be hat, box, and house

这可能是字符串处理的,但我们可能有更多的通用列表:

This could be string processed but we may have more generic lists:

The cat likes to run outside, run inside, or jump up the stairs. |

List=run outside, run inside, or jump up the stairs.

这可能在段落的中间或句子的结尾,这使事情变得更加复杂.

This could be in the middle of a paragraph or the end of the sentence which further complicates things.

我使用Python的Pattern已有一段时间了,我没有找到解决办法,并且很好奇是否可以使用pattern或nltk(自然语言工具包).

I've been working with Pattern for python for awhile and I'm not seeing a way to go about this and was curious if there is a way with pattern or nltk (natural language tool kit).

推荐答案

使用from nltk.tokenize import sent_tokenize怎么样?

sent_tokenize("Hello SF Python. This is NLTK.")
["Hello SF Python.", "This is NLTK."]

然后,您可以通过以下方式使用该句子列表:

Then you can use that list of sentences in this way:

for sentence in my_list:
  # test if this sentence contains the words you want
  # using all() method 

更多信息此处

这篇关于确定句子中是否包含单词列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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