-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
31 lines (24 loc) · 826 Bytes
/
__init__.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
import marisa_trie
from indexer import Indexer
class W2VMMap(object):
def __init__(self,
source_file,
index_file,
trie_file):
self.source_file = source_file
self.index_file = index_file
trie = marisa_trie.Trie().mmap(trie_file)
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))