def txt2vec(self, text, vec_type=list):
"""Converts a string to a vector (list of ints).
First runs a sentence tokenizer, then a word tokenizer.
``vec_type`` is the type of the returned vector if the input is a string.
"""
if vec_type == np.ndarray:
res = np.fromiter(
(self[token] for token in self.tokenize(str(text))),
np.int
)
elif vec_type == list or vec_type == tuple or vec_type == set:
res = vec_type((self[token] for token in self.tokenize(str(text))))
else:
raise RuntimeError('Type {} not supported by dict'.format(vec_type))
assert type(res) == vec_type
return res
评论列表
文章目录