-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.py
135 lines (101 loc) · 3.91 KB
/
utils.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from tqdm import tqdm
import numpy as np
import numpy as np
from functools import partial
from threading import Thread
import json
import numpy as np
# from nltk.tokenize import word_tokenize
# def tokenise( line ):
# return word_tokenize(str(line).lower())
nlp = None
def get_nlp():
global nlp
if nlp is None:
import spacy # See "Installing spaCy"
nlp = spacy.load('en') # You are here.
def spacy_tokenize(line):
get_nlp()
doc = nlp( line )
p = [ str(d).lower() for d in doc]
return p
def tokenise( line , tokenizer_fn=spacy_tokenize , startEndTokens=False ):
p = tokenizer_fn(line)
if startEndTokens:
p = ['<start>'] + p + ['<end>']
return p
class Vocabulary(object):
"""Simple vocabulary wrapper."""
def __init__(self):
self.word2idx = {}
self.idx2word = {}
self.wordFreqs = {}
self.idx = 1 # id 0 is reserved for padding
self.add_word( '<unk>' )
self.add_word( '<start>' )
self.add_word( '<end>' )
def add_word(self, word):
if not word in self.word2idx:
self.word2idx[word] = self.idx
self.idx2word[self.idx] = word
self.idx += 1
self.wordFreqs[ word ] = 0
self.wordFreqs[ word ] += 1
def add_words(self , words ):
for w in words:
self.add_word( w )
def add_sentence(self , sentence ):
words = tokenise( sentence )
for w in words:
self.add_word( w )
def keepTopK( self , k ):
wordFreqs = self.wordFreqs.items()
wordFreqs = sorted( wordFreqs , key=lambda x:x[1] , reverse=True)
wordFreqs = wordFreqs[:k]
words = [ w[0] for w in wordFreqs ]
self.word2idx = {}
self.idx2word = {}
self.idx = 1
for word in ['<unk>' , '<start>' , '<end>' ] + words:
if not word in self.word2idx:
self.word2idx[word] = self.idx
self.idx2word[self.idx] = word
self.idx += 1
def __call__(self, word):
if not word in self.word2idx:
return self.word2idx['<unk>']
return self.word2idx[word]
def __len__(self):
return len(self.word2idx)
def getDicts( self ):
J = { "idx":self.idx , "idx2word":self.idx2word , "word2idx":self.word2idx , "wordFreqs":self.wordFreqs }
return J
def save( self, fname_prefix ):
open( fname_prefix + "word2idx.json" , "wb" ).write( json.dumps( self.word2idx ) )
open( fname_prefix + "idx2word.json" , "wb" ).write( json.dumps( self.idx2word ) )
J = { "idx":self.idx , "idx2word":self.idx2word , "word2idx":self.word2idx , "wordFreqs":self.wordFreqs }
open( fname_prefix + "everything.json" , "wb" ).write( json.dumps( J ) )
def load(self):
d = open( fname_prefix + "everything.json" ).read()
self.idx = d['ids']
self.idx2word = idx2word
self.word2idx = word2idx
self.wordFreqs = wordFreqs
def getSentencesMat( sentences , vocab , maxSentenceL=None , padding='right' , startEndTokens=False , tokenizer_fn=spacy_tokenize ):
tokenised = [ tokenise(s , startEndTokens=startEndTokens ,tokenizer_fn=tokenizer_fn ) for s in sentences ]
if maxSentenceL is None:
maxSentenceL = max([ len(s) for s in tokenised ])
sentencesMat = np.zeros((len(sentences) , maxSentenceL)).astype('int64')
for i , sen in enumerate(tokenised) :
ids = [ vocab(w) for w in sen ]
if padding == 'right':
ids = ids[:maxSentenceL]
else:
ids = ids[-1*maxSentenceL:]
if len(ids) < maxSentenceL:
if padding == 'right':
ids = ids + [0]*(maxSentenceL - len(ids) )
else:
ids = [0]*(maxSentenceL - len(ids) ) + ids
sentencesMat[i] = np.array(ids)
return sentencesMat