Gensim 快速文本获取词汇或单词索引
Gensim fast text get vocab or word index
尝试使用 gensim's fasttext
,测试 gensim
中的示例代码,将参数替换为 corpus_iterable
https://radimrehurek.com/gensim/models/fasttext.html
gensim_version == 4.0.1
from gensim.models import FastText
from gensim.test.utils import common_texts # some example sentences
print(common_texts[0])
['human', 'interface', 'computer']
print(len(common_texts))
9
model = FastText(vector_size=4, window=3, min_count=1) # instantiate
model.build_vocab(corpus_iterable=common_texts)
model.train(corpus_iterable=common_texts, total_examples=len(common_texts), epochs=10)
它有效,但有什么方法可以 get the vocab
模型。例如,在 Tensorflow Tokenizer
中有一个 word_index
将 return all the words
。这里有类似的东西吗?
该模型将词向量存储在 .wv
对象中。我不知道您使用的是哪个 gensim 版本,但是对于 Gensim 4,您可以通过调用 model.wv.key_to_index
来获取键控向量。你会得到一个包含单词及其索引的字典
from gensim.models import FastText
from gensim.test.utils import common_texts # some example sentences
print(common_texts[0])
# ['human', 'interface', 'computer']
print(len(common_texts))
# 9
model = FastText(vector_size=4, window=3, min_count=1) # instantiate
model.build_vocab(corpus_iterable=common_texts)
model.train(corpus_iterable=common_texts, total_examples=len(common_texts), epochs=10)
# get vocab keys with indices
vocab = model.wv.key_to_index
print(vocab)
# output
# {'system': 0, 'graph': 1, 'trees': 2, 'user': 3, 'minors': 4, 'eps': 5, 'time': 6,
# 'response': 7, 'survey': 8, 'computer': 9, 'interface': 10, 'human': 11}
尝试使用 gensim's fasttext
,测试 gensim
中的示例代码,将参数替换为 corpus_iterable
https://radimrehurek.com/gensim/models/fasttext.html
gensim_version == 4.0.1
from gensim.models import FastText
from gensim.test.utils import common_texts # some example sentences
print(common_texts[0])
['human', 'interface', 'computer']
print(len(common_texts))
9
model = FastText(vector_size=4, window=3, min_count=1) # instantiate
model.build_vocab(corpus_iterable=common_texts)
model.train(corpus_iterable=common_texts, total_examples=len(common_texts), epochs=10)
它有效,但有什么方法可以 get the vocab
模型。例如,在 Tensorflow Tokenizer
中有一个 word_index
将 return all the words
。这里有类似的东西吗?
该模型将词向量存储在 .wv
对象中。我不知道您使用的是哪个 gensim 版本,但是对于 Gensim 4,您可以通过调用 model.wv.key_to_index
来获取键控向量。你会得到一个包含单词及其索引的字典
from gensim.models import FastText
from gensim.test.utils import common_texts # some example sentences
print(common_texts[0])
# ['human', 'interface', 'computer']
print(len(common_texts))
# 9
model = FastText(vector_size=4, window=3, min_count=1) # instantiate
model.build_vocab(corpus_iterable=common_texts)
model.train(corpus_iterable=common_texts, total_examples=len(common_texts), epochs=10)
# get vocab keys with indices
vocab = model.wv.key_to_index
print(vocab)
# output
# {'system': 0, 'graph': 1, 'trees': 2, 'user': 3, 'minors': 4, 'eps': 5, 'time': 6,
# 'response': 7, 'survey': 8, 'computer': 9, 'interface': 10, 'human': 11}