3 つの列を持つファイルがあります (\t で区切られています。最初の列は単語、2 番目は見出し語、3 番目はタグです)。一部の行は、ドットまたはコンマのみで構成されています。
<doc n=1 id="CMP/94/10">
<head p="80%">
Customs customs tag1
union union tag2
in in tag3
danger danger tag4
of of tag5
the the tag6
</head>
<head p="80%">
New new tag7
restrictions restriction tag8
in in tag3
the the tag6
.
Hi hi tag8
ユーザーがレンマ "in" を検索するとします。「in」の頻度と「in」の前後の補題の頻度が欲しい。そこで、コーパス全体での「結合」、「危険」、「制限」、「ザ」の頻度が必要です。結果は次のようになります。
union 1
danger 1
restriction 1
the 2
それ、どうやったら出来るの?使ってみましたがうまくいきlemma_counter = {}
ません。
私はPython言語の経験がありませんので、何か間違っている場合は修正してください。
c = open("corpus.vert")
corpus = []
for line in c:
if not line.startswith("<"):
corpus.append(line)
lemma = raw_input("Lemma you are looking for: ")
counter = 0
lemmas_before_after = []
for i in range(len(corpus)):
parsed_line = corpus[i].split("\t")
if len(parsed_line) > 1:
if parsed_line[1] == lemma:
counter += 1 #this counts lemma frequency
new_list = []
for j in range(i-1, i+2):
if j < len(corpus) and j >= 0:
parsed_line_with_context = corpus[j].split("\t")
found_lemma = parsed_line_with_context[0].replace("\n","")
if len(parsed_line_with_context) > 1:
if lemma != parsed_line_with_context[1].replace("\n",""):
lemmas_before_after.append(found_lemma)
else:
lemmas_before_after.append(found_lemma)
print "list of lemmas ", lemmas_before_after
lemma_counter = {}
for i in range(len(corpus)):
for lemma in lemmas_before_after:
if parsed_line[1] == lemma:
if lemma in lemma_counter:
lemma_counter[lemma] += 1
else:
lemma_counter[lemma] = 1
print lemma_counter
fA = counter
print "lemma frequency: ", fA