-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphrase-prob.py
38 lines (28 loc) · 1.01 KB
/
phrase-prob.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
# -*- coding: utf-8 -*-
import plac
import kenlm
import math
from collections import Counter
lm = kenlm.LanguageModel('corpus.ma.id.tokens.bin')
STOPWORDS = set(['bahwa', 'adalah', 'dan', 'dari', 'yang', 'ke'])
def main(arpa_file, output):
scores = Counter()
for line in open('corpus.ma.id.sents.pos.phrase.arpa'):
if not '\t' in line:
continue
if '<unk>' in line or '<s>' in line or '</s>' in line:
continue
parts = line.strip().split('\t')
phrase = parts[1]
if (phrase.replace(' ', '').isdigit()
or set(phrase.split()) & STOPWORDS
or len(phrase) <= 2
or phrase[0].isdigit()
or phrase[1] == ' '):
continue
scores[phrase] = lm.score(phrase)
with open(output, 'w') as out:
for phrase, score in scores.most_common():
out.write('{0}\t\t{1}\n'.format(round(math.exp(score), 5), phrase))
if __name__ == '__main__':
plac.call(main)