2013-04-16 9 views
7

テキストから指定された単語を含むすべての文を抽出しようとしています。単語を含むPython抽出文

txt="I like to eat apple. Me too. Let's go buy some apples." 
txt = "." + txt 
re.findall(r"\."+".+"+"apple"+".+"+"\.", txt) 

が、それは私を返している:

[".I like to eat apple. Me too. Let's go buy some apples."] 

の代わり:

[".I like to eat apple., "Let's go buy some apples."] 

すべてのヘルプをしてください?

答えて

9
In [3]: re.findall(r"([^.]*?apple[^.]*\.)",txt)                                
Out[4]: ['I like to eat apple.', " Let's go buy some apples."] 
3

あなたはstr.splitを使用することができ、

>>> txt="I like to eat apple. Me too. Let's go buy some apples." 
>>> txt.split('. ') 
['I like to eat apple', 'Me too', "Let's go buy some apples."] 

>>> [ t for t in txt.split('. ') if 'apple' in t] 
['I like to eat apple', "Let's go buy some apples."] 
7
In [7]: import re 

In [8]: txt=".I like to eat apple. Me too. Let's go buy some apples." 

In [9]: re.findall(r'([^.]*apple[^.]*)', txt) 
Out[9]: ['I like to eat apple', " Let's go buy some apples"] 

しかし、@ jamylakのsplitベースのソリューションが高速であることに注意してください。

In [10]: %timeit re.findall(r'([^.]*apple[^.]*)', txt) 
1000000 loops, best of 3: 1.96 us per loop 

In [11]: %timeit [s+ '.' for s in txt.split('.') if 'apple' in s] 
1000000 loops, best of 3: 819 ns per loop 

速度差が大きくなるため、より少ないが、それでも重要です文字列:

In [24]: txt = txt*10000 

In [25]: %timeit re.findall(r'([^.]*apple[^.]*)', txt) 
100 loops, best of 3: 8.49 ms per loop 

In [26]: %timeit [s+'.' for s in txt.split('.') if 'apple' in s] 
100 loops, best of 3: 6.35 ms per loop 
+0

+1 nice answer! 'txt = txt * 10000'と'%timeit'を実行すると結果はより明確になります – Kent

+0

ありがとうございました。大きな文字列には '%timeit'ベンチマークを追加しました。 – unutbu

16

正規表現は必要ない:

>>> txt = "I like to eat apple. Me too. Let's go buy some apples." 
>>> [sentence + '.' for sentence in txt.split('.') if 'apple' in sentence] 
['I like to eat apple.', " Let's go buy some apples."] 
+0

ありがとうございましたjamylak – user2187202

+0

@ user2187202正規表現の質問としてタグ付けしてから実際に必要だったものがあれば、正規表現の解決を受け入れるか受け入れるかは、私の答えを受け入れることができます。 – jamylak

2
r"\."+".+"+"apple"+".+"+"\." 

この行は少し奇妙です。なぜ非常に多くの別々の文字列を連結するのですか?あなたは単にr '.. + apple。+。'を使うことができます。

とにかく、あなたの正規表現の問題は、欲張りです。デフォルトでは、x+はできるだけ多くの場合xと一致します。したがって、.+は可能な限り多くの文字(任意の文字)と一致します。ドットとappleが含まれます。

代わりに使用したいのは、貪欲でない表現です。通常は末尾に?を追加してこれを行うことができます:.+?。あなたは両方のリンゴ文章得るが、それでもMe too.もはやあなたを見ることができるように

['.I like to eat apple. Me too.'] 

これは、以下の結果を得ないでしょう。これはappleの後にまだ.と一致しているため、次の文もキャプチャできないためです。ここでは、任意の文字を見ていませんが、自分自身をドットしていない文字のみr'\.[^.]*?apple[^.]*?\.'

作業正規表現はこれだろう。また、最初の文のappleの後にドット以外の文字がないため、文字をまったく一致させないようにします。この中でその式の結果を使用して:

明らか
['.I like to eat apple.', ". Let's go buy some apples."] 
0

、問題のサンプルはextract sentence containing substring代わりの
extract sentence containing wordです。どのようにextract sentence containing word問題をPythonで解決するかは次のようになります。

単語は、文章の始め|中|最後に置くことができます。問題の例に限定されるものではなく、私は文章中の単語を検索する一般的な機能を提供します:

txt="I like to eat apple. Me too. Let's go buy some apples." 
word = "apple" 
print [ t for t in txt.split('. ') if searchWordofSentence(word,t)] 

:問題の例に限定されるもので

def searchWordinSentence(word,sentence): 
    pattern = re.compile(' '+word+' |^'+word+' | '+word+' $') 
    if re.search(pattern,sentence): 
     return True 

を、我々は次のように解決することができます対応する出力は次のとおりです。

['I like to eat apple'] 
関連する問題