0

正規表現でリスト変数を使用するにはどうすればよいですか? 問題はここにあります:

re.search(re.compile(''.format('|'.join(map(re.escape, kand))), corpus.raw(fileid)))

エラーは

TypeError: unsupported operand type(s) for &: 'str' and 'int'

単純な re.search はうまく機能しますが、re.search の最初の属性としてリストが必要です。

for fileid in corpus.fileids():
    if re.search(r'[Чч]естны[й|м|ого].труд(а|ом)', corpus.raw(fileid)):
        dict_features[fileid]['samoprezentacia'] = 1
    else:
        dict_features[fileid]['samoprezentacia'] = 0

if re.search(re.compile('\b(?:%s)\b'.format('|'.join(map(re.escape, kand))), corpus.raw(fileid))):
    dict_features[fileid]['up'] = 1
else:
    dict_features[fileid]['up'] = 0

dict_features を返す

ちなみにカンドはリストです:

kand = [line.strip() for line in open('kand.txt', encoding="utf8")]

出力カンドでは ['apple', 'banana', 'peach', 'plum', 'pineapple', 'kiwi']

編集:私はWindows 7の完全なエラースタックでWinPythonでPython 3.3.2を使用しています:

Traceback (most recent call last):
  File "F:/Python/NLTK packages/agit_classify.py", line 59, in <module>
    print (regexp_features(agit_corpus))
  File "F:/Python/NLTK packages/agit_classify.py", line 53, in regexp_features
    if re.search(re.compile(r'\b(?:{0})\b'.format('|'.join(map(re.escape, kandidats_all))), corpus.raw(fileid))):
  File "F:\WinPython-32bit-3.3.2.0\python-3.3.2\lib\re.py", line 214, in compile
    return _compile(pattern, flags)
  File "F:\WinPython-32bit-3.3.2.0\python-3.3.2\lib\re.py", line 281, in _compile
    p = sre_compile.compile(pattern, flags)
  File "F:\WinPython-32bit-3.3.2.0\python-3.3.2\lib\sre_compile.py", line 494, in compile
    p = sre_parse.parse(p, flags)
  File "F:\WinPython-32bit-3.3.2.0\python-3.3.2\lib\sre_parse.py", line 748, in parse
    p = _parse_sub(source, pattern, 0)
  File "F:\WinPython-32bit-3.3.2.0\python-3.3.2\lib\sre_parse.py", line 360, in _parse_sub
    itemsappend(_parse(source, state))
  File "F:\WinPython-32bit-3.3.2.0\python-3.3.2\lib\sre_parse.py", line 453, in _parse
    if state.flags & SRE_FLAG_VERBOSE:
TypeError: unsupported operand type(s) for &: 'str' and 'int'
4

2 に答える 2

2

実際の例外が発生する理由は、括弧の不一致です。わかりやすくするために分割してみましょう。

re.search(
    re.compile(
        ''.format('|'.join(map(re.escape, kand))), 
        corpus.raw(fileid)))

corpus.raw(fileid)つまり、文字列を の 2 番目の引数re.compileとしてではなく、 の 2番目の引数として渡しますre.search

flagsつまり、整数であるはずの値として使用しようとしています。re.compile文字列に対して演算子を使用して&各フラグ ビットをテストしようとすると、 TypeError.

そして、このエラーを回避した場合は、2 つではなく 1 つの引数しか渡していないため、 re.searchwould 自体が a を発生させます。TypeError

これがまさに、過度に複雑な式を記述してはならない理由です。それらはデバッグするのが非常に面倒です。これを別々のステップで書いた場合、それは明らかです:

escaped_kand = map(re.escape, kand)
alternation = '|'.join(escaped_kand)
whatever_this_was_supposed_to_do = ''.format(alternation)
regexpr = re.compile(whatever_this_was_supposed_to_do, corpus.raw(fileid))
re.search(regexpr)

これにより、あなたが行っている作業の半分がそもそも不要であることが明らかになります。

まず、re.searchコンパイルされた正規表現ではなく、パターンを取ります。コンパイルされた正規表現で動作する場合、それは単なる偶然です。したがって、式のその部分全体が役に立ちません。パターン自体を渡すだけです。

または、正規表現をコンパイルする正当な理由がある場合は、説明されているように、結果の正規表現オブジェクトは「およびメソッドre.compileを使用してマッチングに使用できます」。したがって、トップレベルの関数ではなく、コンパイルされたオブジェクトのメソッドを使用してください。match()search()searchre.search

第二に、あなたが何を期待していたのかわかりませんが''.format(anything)、おそらく何も返すことはできません''.

于 2013-08-08T22:40:04.307 に答える