2016-05-13 10 views
0

私は、記録する単語の数に柔軟性を持たせるためのパラメータとしてleftとrightを使って、テキスト中の特定の単語(w)のコンテキストを検索する関数を作成しました。正規表現による単語コンテキストの検索

テキスト= [ 'ある' 'Pythonの'、 'ダイナミック'、 '入力された'、 ':

import re 
def get_context (text, w, left, right): 
    text.insert (0, "*START*") 
    text.append ("*END*") 

    all_contexts = [] 

    for i in range(len(text)): 

     if re.match(w,text[i], 0): 

      if i < left: 
       context_left = text[:i] 

      else: 
       context_left = text[i-left:i] 

      if len(text) < (i+right): 
       context_right = text[i:] 

      else: 
       context_right = text[i:(i+right+1)] 

      context = context_left + context_right 

      all_contexts.append(context) 
    return all_contexts 

だから例えば、このようなリストの形式でテキストを持っている場合 'them'、 'but'、 '' '' '' '' '' '' '' '' '' '' '' '' '' '' ' 'want'、 'to'、 'pass'、 'one'、 'thousand'のように、 'パラメータ'、 ''、 '引数'、 'to'、 'your'、 'function'、 'then'、 'can'、 'explicit'、 'define'、 'every' '、'あなた '、'関数 'all'、 'the'、 'the'、 'the'、 'the'、 'and'、 ' 」、 '引数'、 'あなた'、 'パス'、 'に'、 '彼ら'、 'の'、 'あなたは']

機能は、例えば正常に動作します:

get_context(text, "function",2,2) 
[['language', 'python', 'functions', 'really', 'care'], ['to', 'your', 'function', 'then', 'you'], ['in', 'your', 'function', 'definition', 'and'], ['and', 'your', 'function', 'will', 'be']] 

今、私は次のことをしているテキスト内のすべての単語のコンテキストを使って辞書を構築しようとしています:しかし、私は得ていますこのエラー。

Traceback (most recent call last): 
    File "<pyshell#32>", line 2, in <module> 
    d[w] = get_context(text,w,2,2) 
    File "<pyshell#20>", line 9, in get_context 
    if re.match(w,text[i], 0): 
    File "/usr/lib/python3.4/re.py", line 160, in match 
    return _compile(pattern, flags).match(string) 
    File "/usr/lib/python3.4/re.py", line 294, in _compile 
    p = sre_compile.compile(pattern, flags) 
    File "/usr/lib/python3.4/sre_compile.py", line 568, in compile 
    p = sre_parse.parse(p, flags) 
    File "/usr/lib/python3.4/sre_parse.py", line 760, in parse 
    p = _parse_sub(source, pattern, 0) 
    File "/usr/lib/python3.4/sre_parse.py", line 370, in _parse_sub 
    itemsappend(_parse(source, state)) 
    File "/usr/lib/python3.4/sre_parse.py", line 579, in _parse 
    raise error("nothing to repeat") 
sre_constants.error: nothing to repeat 

このエラーは分かりません。誰もこれで私を助けることができますか?

答えて

1

"* START *"と "* END *"は正規表現として解釈されるという問題があります。また、関数の呼び出し中に "* START *"と "* END *"を挿入すると問題が発生することに注意してください。あなたは一度だけそれをするべきです。ここで

は、作業コードの完全なバージョンです:

import re 

def get_context(text, w, left, right): 
    all_contexts = [] 
    for i in range(len(text)): 
     if re.match(w,text[i], 0): 
      if i < left: 
       context_left = text[:i] 
      else: 
       context_left = text[i-left:i] 
      if len(text) < (i+right): 
       context_right = text[i:] 
      else: 
       context_right = text[i:(i+right+1)] 
      context = context_left + context_right 
      all_contexts.append(context) 
    return all_contexts 

text = ['Python', 'is', 'dynamically', 'typed', 'language', 
     'Python', 'functions', 'really', 'care', 'about', 'what', 
     'you', 'pass', 'to', 'them', 'but', 'you', 'got', 'it', 'the', 
     'wrong', 'way', 'if', 'you', 'want', 'to', 'pass', 'one', 
     'thousand', 'arguments', 'to', 'your', 'function', 'then', 
     'you', 'can', 'explicitly', 'define', 'every', 'parameter', 
     'in', 'your', 'function', 'definition', 'and', 'your', 
     'function', 'will', 'be', 'automagically', 'able', 'to', 'handle', 
     'all', 'the', 'arguments', 'you', 'pass', 'to', 'them', 'for', 'you'] 

text.insert(0, "START") 
text.append("END") 

d = {} 
for w in set(text): 
    d[w] = get_context(text,w,2,2) 

たぶん、あなたはw == text[i]re.match(w,text[i], 0)を置き換えることができます。

+0

さて、それは問題でした。私はこれら2つの* START *と* END *を考えなかった。私はw == text [i]と考えましたが、なぜこれがうまくいかないのか知りたかったのです。ありがとうございます – Wunter

0

textの少なくとも1つの要素には、正規表現で特殊な文字が含まれています。あなただけちょうどstr.startswithを使用し、単語が文字列であるかどうかを見つけるためにしようとしている場合、すなわち

if text[i].startswith(w): # instead of re.match(w,text[i], 0): 

しかし、あなたが平等のためにとにかくそれをチェックし、されていない理由を私は理解していません。

+0

私は 're.match'を使うと柔軟性が増したと思います。たとえば、関数と関数を同時に探して' functions? 'とマッチさせるなどです。とにかくあなたの提案をありがとう – Wunter

1

全体事は非常に簡潔に次の再書き込みすることができ、

text = 'Python is dynamically typed language Python functions really care about what you pass to them but you got it the wrong way if you want to pass one thousand arguments to your function then you can explicitly define every parameter in your function definition and your function will be automagically able to handle all the arguments you pass to them for you' 

context = 'function',

pat = re.compile(r'(\w+\s\w+\s)functions?(?=(\s\w+\s\w+))') 
pat.findall(text) 
[('language Python ', ' really care'), 
('to your ', ' then you'), 
('in your ', ' definition and'), 
('and your ', ' will be')] 

を想定し、strそれを維持、マイナーなカスタマイズに正規表現に必要とされるであろう例えば、functionalまたはfunctioningのような言葉がfunctionまたはfunctionsのように許可されるだけです。しかし、重要なアイデアは、にインデックスを作成して機能させることです。

バルクで適用すると、これがうまく行かない場合は、ご意見ください。

+0

私は単語の左右の数を変更したい場合は、リストを扱う方が簡単だろうと思った。私は正規表現の使用を考えましたが、私は側面の単語の数を設定する方法を考えることができませんでした。あなたの提案をありがとう – Wunter

+0

@Wunter 'list'を使っているなら、' inset'と '+'は赤いフラグであることを常に知っています。彼らは遅いです。 'append'はOKです。 –

+0

アドバイスをいただきありがとうございます。私はプログラミングを学び始めています。私はそれを心に留めておく:) – Wunter

関連する問題