1

段落があるとします:

text = '''Darwin published his theory of evolution with compelling evidence in his 1859 book On the Origin of Species, overcoming scientific rejection of earlier concepts of transmutation of species.[4][5] By the 1870s the scientific community and much of the general public had accepted evolution as a fact. However, many favoured competing explanations and it was not until the emergence of the modern evolutionary synthesis from the 1930s to the 1950s that a broad consensus developed in which natural selection was the basic mechanism of evolution.[6][7] In modified form, Darwin's scientific discovery is the unifying theory of the life sciences, explaining the diversity of life.[8][9]'''

単語 (優先) を入力した場合、その単語が含まれる文全体を削除するにはどうすればよいでしょうか。以前に使用した方法は面倒でした。sent_tokenize を使用してパラ (13000 語以上) を分割し、1000 語以上をチェックする必要があったため、ループを実行して各文の各単語をチェックしました。400以上の文章があるので、これには多くの時間がかかります。

代わりに、パラでこれらの 1000 語をチェックしたいと思います。単語が見つかったら、前のすべての単語をピリオドまで選択し、後のすべての単語をピリオドまで選択します。

4

3 に答える 3

0

あなたの質問を理解できるかどうかはわかりませんが、次のようなことができます:

text = 'whatever....'
sentences = text.split('.')
good_sentences = [e for e in sentences if 'my_word' not in e]

それはあなたが探しているものですか?

于 2013-09-25T10:55:50.517 に答える
0

.これにより、どこかにその単語を含むすべての文 ( で囲まれたもの) が削除されます。

def remove_sentence(input, word):
    return ".".join((sentence for sentence in input.split(".")
                    if word not in sentence))

>>>> remove_sentence(text, "published")
"[4][5] By the 1870s the scientific community and much of the general public had accepted evolution as a fact. However, many favoured competing explanations and it was not until the emergence of the modern evolutionary synthesis from the 1930s to the 1950s that a broad consensus developed in which natural selection was the basic mechanism of evolution.[6][7] In modified form, Darwin's scientific discovery is the unifying theory of the life sciences, explaining the diversity of life.[8][9]"
>>>
>>> remove_sentence(text, "favoured")
"Darwin published his theory of evolution with compelling evidence in his 1859 book On the Origin of Species, overcoming scientific rejection of earlier concepts of transmutation of species.[4][5] By the 1870s the scientific community and much of the general public had accepted evolution as a fact.[6][7] In modified form, Darwin's scientific discovery is the unifying theory of the life sciences, explaining the diversity of life.[8][9]"
于 2013-09-25T10:56:39.520 に答える