-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecursiveSegmenter.py
More file actions
57 lines (45 loc) · 1.38 KB
/
Copy pathRecursiveSegmenter.py
File metadata and controls
57 lines (45 loc) · 1.38 KB
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
from __future__ import unicode_literals
from gensim import models as md
import codecs
f = codecs.open('datas/titresim.txt', encoding='utf-8')
for line in f:
words = f.read().splitlines()
model = md.Word2Vec.load_word2vec_format('datas/tvec.bin', binary=True)
g_suffixes = []
results = []
g_stem = []
for word in words:
suffixes = []
stem = word
count = 0
for idx in range(len(word)):
try:
if model.similarity(stem[:-count], stem) > 0.3:
suffixes.append(stem[-count:])
g_suffixes.append(stem[-count:])
stem = stem[:-count]
count = 0
except:
pass
count += 1
g_stem.append(stem)
result = stem;
for s in range(len(suffixes)):
result = result + '-' + suffixes.pop()
results.append(result)
# if stem in g_stem:
# g_stem = stem
# print g_stem
# for result in results:
# print result
suffixes = list(set(g_suffixes))
with codecs.open('results/suffixes.txt', 'w', 'utf-8') as suf_file:
for item in suffixes:
suf_file.write("%s\n" % item)
stems = list(set(g_stem))
with codecs.open('results/stems.txt', 'w', 'utf-8') as stem_file:
for item in stems:
stem_file.write("%s\n" % item)
with codecs.open('results/segments.txt', 'w', 'utf-8') as steg_file:
for item in results:
steg_file.write("%s\n" % item)