-
Notifications
You must be signed in to change notification settings - Fork 1
/
find-assoc.py
62 lines (56 loc) · 2.04 KB
/
find-assoc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from nltk.corpus import stopwords
from tqdm import tqdm
import json
from nltk.corpus import wordnet as wn
task = "bert"
en_stopwords = set(stopwords.words('english'))
counts = {}
word_freq = {}
with open(f"data/{task}-cooccur.tsv") as f:
for line in tqdm(f):
cue, token, count = line.strip().split("\t")
if token in en_stopwords or not token.isalpha():
continue
if token == cue:
continue
if len(wn.synsets(token.lower())) == 0:
continue
if token in ('<unk>', '<s>', '</s>', '<pad>', '<mask>') or "advertisement" in token:
continue
count = float(count)
if cue not in counts:
counts[cue] = {}
token = token.lower()
if token not in counts[cue]:
counts[cue][token] = 0
counts[cue][token] += count
word_freq[token] = word_freq.get(token, 0) + count
word_freq_sum = sum(word_freq.values())
top_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)
with open(f"data/{task}-top_words.txt", "w") as f:
for word, freq in top_words[:100]:
f.write(f"{word}\t{freq}\n")
top_words = set(word for word, freq in top_words[:100])
assocs = {}
for cue in counts:
z = sum(counts[cue].values())
for k, v in counts[cue].items():
counts[cue][k] = v / z
pairs = sorted(counts[cue].items(), key=lambda x: x[1], reverse=True)
assocs[cue] = []
for word, cond_prob in pairs:
if word in en_stopwords or not word.isalpha() or word in top_words:
continue
pmi = cond_prob / word_freq[word]
if abs(pmi-1) < 0.1:
print("skipped", word)
continue
assocs[cue].append((word, cond_prob, pmi))
if len(assocs[cue]) == 50:
break
assocs[cue].sort(key=lambda x: x[1], reverse=True)
topk = assocs
with open(f"results/assocs/{task}.jsonl", "w") as f:
for cue in topk:
assocs = [{"word": x[0], "score": x[1]} for x in topk[cue]]
f.write(json.dumps({"cue": cue, "assoc": assocs}) + "\n")