2

文に単語のリストが含まれていることを検出する方法 (パターン、Python、NLTK など) はありますか。

すなわち

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

これは文字列処理される可能性がありますが、より一般的なリストがある場合があります。

すなわち

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

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

これは、段落の途中または文の終わりにある可能性があり、事態をさらに複雑にします.

私はしばらく Python 用のパターンを使用してきましたが、これを行う方法が見当たらず、パターンまたは nltk (自然言語ツール キット) を使用する方法があるかどうかに興味がありました。

4

4 に答える 4

3

あなたの質問から私が得たのは、リストにあるすべての単語が 1 つの文に含まれているかどうかを検索したいということだと思います。

一般に、リスト要素を検索するには、文内でall関数を使用できます。その中のすべての引数が true の場合、true を返します。

listOfWords = ['word1', 'word2', 'word3', 'two words']
sentence = "word1 as word2 a fword3 af two words"

if all(word in sentence for word in listOfWords):
    print "All words in sentence"
else:
    print "Missing"

出力: -

"All words in sentence"

これはあなたの目的に役立つと思います。そうでない場合は、明確にすることができます。

于 2012-10-26T19:48:12.887 に答える
1

を使用するのはfrom nltk.tokenize import sent_tokenizeどうですか?

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

次に、その文のリストを次のように使用できます。

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

詳細はこちら

于 2012-10-26T19:48:40.793 に答える
0
all(word in sentence for word in listOfWords)
于 2012-10-26T19:55:25.943 に答える