2016-11-01 03:51:54 +03:00
|
|
|
from keras.models import model_from_json
|
2016-11-12 20:43:37 +03:00
|
|
|
import numpy
|
2016-11-18 15:32:12 +03:00
|
|
|
import numpy.random
|
2017-01-31 22:27:13 +03:00
|
|
|
import json
|
|
|
|
from spacy.tokens.span import Span
|
|
|
|
|
|
|
|
try:
|
|
|
|
import cPickle as pickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle
|
2016-11-01 03:51:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
class KerasSimilarityShim(object):
|
|
|
|
@classmethod
|
2017-01-31 22:27:13 +03:00
|
|
|
def load(cls, path, nlp, get_features=None, max_length=100):
|
2016-11-01 03:51:54 +03:00
|
|
|
if get_features is None:
|
2017-01-31 22:27:13 +03:00
|
|
|
get_features = get_word_ids
|
2016-11-01 03:51:54 +03:00
|
|
|
with (path / 'config.json').open() as file_:
|
2017-01-31 22:27:13 +03:00
|
|
|
model = model_from_json(file_.read())
|
2016-11-01 03:51:54 +03:00
|
|
|
with (path / 'model').open('rb') as file_:
|
|
|
|
weights = pickle.load(file_)
|
|
|
|
embeddings = get_embeddings(nlp.vocab)
|
|
|
|
model.set_weights([embeddings] + weights)
|
2017-01-31 22:27:13 +03:00
|
|
|
return cls(model, get_features=get_features, max_length=max_length)
|
2016-11-01 03:51:54 +03:00
|
|
|
|
2017-01-31 22:27:13 +03:00
|
|
|
def __init__(self, model, get_features=None, max_length=100):
|
2016-11-01 03:51:54 +03:00
|
|
|
self.model = model
|
|
|
|
self.get_features = get_features
|
2017-01-31 22:27:13 +03:00
|
|
|
self.max_length = max_length
|
2016-11-01 03:51:54 +03:00
|
|
|
|
|
|
|
def __call__(self, doc):
|
|
|
|
doc.user_hooks['similarity'] = self.predict
|
|
|
|
doc.user_span_hooks['similarity'] = self.predict
|
2017-01-31 22:27:13 +03:00
|
|
|
|
2016-11-01 03:51:54 +03:00
|
|
|
def predict(self, doc1, doc2):
|
2017-01-31 22:27:13 +03:00
|
|
|
x1 = self.get_features([doc1], max_length=self.max_length, tree_truncate=True)
|
|
|
|
x2 = self.get_features([doc2], max_length=self.max_length, tree_truncate=True)
|
2016-11-01 03:51:54 +03:00
|
|
|
scores = self.model.predict([x1, x2])
|
|
|
|
return scores[0]
|
|
|
|
|
|
|
|
|
2016-11-18 15:32:12 +03:00
|
|
|
def get_embeddings(vocab, nr_unk=100):
|
|
|
|
nr_vector = max(lex.rank for lex in vocab) + 1
|
|
|
|
vectors = numpy.zeros((nr_vector+nr_unk+2, vocab.vectors_length), dtype='float32')
|
2016-11-01 03:51:54 +03:00
|
|
|
for lex in vocab:
|
|
|
|
if lex.has_vector:
|
2016-11-18 15:32:12 +03:00
|
|
|
vectors[lex.rank+1] = lex.vector / lex.vector_norm
|
2016-11-01 03:51:54 +03:00
|
|
|
return vectors
|
|
|
|
|
|
|
|
|
2016-11-18 15:32:12 +03:00
|
|
|
def get_word_ids(docs, rnn_encode=False, tree_truncate=False, max_length=100, nr_unk=100):
|
2016-11-01 03:51:54 +03:00
|
|
|
Xs = numpy.zeros((len(docs), max_length), dtype='int32')
|
|
|
|
for i, doc in enumerate(docs):
|
2016-11-13 17:52:20 +03:00
|
|
|
if tree_truncate:
|
2017-01-31 22:27:13 +03:00
|
|
|
if isinstance(doc, Span):
|
|
|
|
queue = [doc.root]
|
|
|
|
else:
|
|
|
|
queue = [sent.root for sent in doc.sents]
|
2016-11-13 17:52:20 +03:00
|
|
|
else:
|
|
|
|
queue = list(doc)
|
2016-11-12 20:43:37 +03:00
|
|
|
words = []
|
|
|
|
while len(words) <= max_length and queue:
|
|
|
|
word = queue.pop(0)
|
2016-11-18 15:32:12 +03:00
|
|
|
if rnn_encode or (not word.is_punct and not word.is_space):
|
2016-11-12 20:43:37 +03:00
|
|
|
words.append(word)
|
2016-11-13 17:52:20 +03:00
|
|
|
if tree_truncate:
|
2016-11-12 20:43:37 +03:00
|
|
|
queue.extend(list(word.lefts))
|
|
|
|
queue.extend(list(word.rights))
|
|
|
|
words.sort()
|
|
|
|
for j, token in enumerate(words):
|
2016-11-18 15:32:12 +03:00
|
|
|
if token.has_vector:
|
|
|
|
Xs[i, j] = token.rank+1
|
|
|
|
else:
|
|
|
|
Xs[i, j] = (token.shape % (nr_unk-1))+2
|
2016-11-12 20:43:37 +03:00
|
|
|
j += 1
|
|
|
|
if j >= max_length:
|
|
|
|
break
|
2016-11-18 15:32:12 +03:00
|
|
|
else:
|
|
|
|
Xs[i, len(words)] = 1
|
2016-11-01 03:51:54 +03:00
|
|
|
return Xs
|
|
|
|
|
|
|
|
|
2017-04-05 14:18:35 +03:00
|
|
|
def create_similarity_pipeline(nlp, max_length=100):
|
2017-01-31 22:27:13 +03:00
|
|
|
return [
|
|
|
|
nlp.tagger,
|
|
|
|
nlp.entity,
|
|
|
|
nlp.parser,
|
2017-04-05 14:18:35 +03:00
|
|
|
KerasSimilarityShim.load(nlp.path / 'similarity', nlp, max_length)
|
2017-01-31 22:27:13 +03:00
|
|
|
]
|