/
w2vmmap.py
76 lines (50 loc) · 1.89 KB
/
w2vmmap.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import marisa_trie
from indexer import Indexer
import os
class W2VMMap(object):
def __init__(self,
source_file,
index_file = None,
trie_file = None):
self.source_file = source_file
if not index_file:
index_file = self.source_file + ".idx"
if not trie_file:
trie_file = self.source_file + ".trie"
self.index_file = index_file
self.trie_file = trie_file
def make_index(self):
idxr = Indexer(self.source_file, self.index_file)
idxr.make_index()
self.make_trie()
def make_trie(self):
key_file = self.source_file + '.kidx'
trie_file = self.trie_file
if os.path.exists(self.trie_file):
os.remove(self.trie_file)
if os.path.exists(key_file):
os.remove(key_file)
with open(self.source_file) as f ,open(key_file,'a') as t:
for index, line in enumerate(f):
token = line.split('\t')
t.write(token[0] + ';' + str(index) + "\n")
os.system('marisa-build {key_file} > {trie_file}'.format(**locals()))
def __enter__(self):
self.trie = marisa_trie.Trie().mmap(self.trie_file)
return self
def __exit__(self, type, value, traceback):
pass
def token_to_line(self, token):
items = self.trie.items(unicode(token+';'))
if len(items) == 1:
item = items[0][0]
line_num = item.split(';')[1]
return int(line_num)
def get_line(self, line_num):
idxr = Indexer(self.source_file, self.index_file)
line = None
with idxr as i:
line = i.read(line_num)
return line
def get_word(self, word):
return self.get_line(self.token_to_line(word))