入力として 2 つのファイルを受け取るコードがあります: (1) 辞書/レキシコン (2) テキスト ファイル (1 行に 1 文)
私のコードの最初の部分は、辞書をタプルで読み取るため、次のような出力が得られます。
('mthy3lkw', 'weakBelief', 'U')
('mthy3lkm', 'firmBelief', 'B')
('mthy3lh', 'notBelief', 'A')
コードの 2 番目の部分は、テキスト ファイル内の各文を検索して、これらのタプルの位置 0 にある単語を検索し、文、検索単語、およびそのタイプを出力します。
したがって、文 mthy3lkw ana mesh 3arif が与えられた場合、望ましい出力は次のとおりです。
["mthy3lkw ana mesh 3arif", ' mthy3lkw ', 'weakBelief', 'U'] 強調表示された単語が辞書にある場合。
コードの 2 番目の部分 (マッチング部分) が遅すぎます。どうすれば速くなりますか?
これが私のコードです
findings = []
for sentence in data: # I open the sentences file with .readlines()
for word in tuples: # similar to the ones mentioned above
p1 = re.compile('\\b%s\\b'%word[0]) # get the first word in every tuple
if p1.findall(sentence) and word[1] == "firmBelief":
findings.append([sentence, word[0], "firmBelief"])
print findings