解析鸣叫的列表,以便利用Gensim Word2Vec

问题描述 投票:0回答:1

我工作的一个NLP的问题,我的目标是能够通过Python的Gensim库已经使用Word2Vec后通过我的数据转换成sklearn的交易算法。潜在的问题,我试图解决的是一系列的鸣叫的二元分类。要做到这一点,我修改this混帐回购协议的代码。

下面是与符号化的部分代码:

from nltk.tokenize import RegexpTokenizer
tokenizer = RegexpTokenizer(r'\w+')
input_file["tokens"] = input_file["text"].apply(tokenizer.tokenize)
all_words = [word for tokens in input_file["tokens"] for word in tokens]
sentence_lengths = [len(tokens) for tokens in input_file["tokens"]]
vocabulary = sorted(set(all_words))

现在,这里是我使用Gensim的sklearn-API尝试矢量化我的鸣叫的一部分:

from sklearn.model_selection import train_test_split
from gensim.test.utils import common_texts
from gensim.sklearn_api import W2VTransformer
text = input_file["text"].tolist()
labels = input_file["label"].tolist()
X_train, X_test, y_train, y_test = train_test_split(text, labels, test_size=0.2,random_state=40)
model = W2VTransformer(size=10, min_count=1, seed=1)
X_train_w2v = model.fit(common_texts).transform(X_train)

这将导致以下错误:

KeyError: "word 'Great seeing you again, don't be a stranger!' not in vocabulary"

看来,问题的一部分是Gensim期待在时间内应供给一个字,反而是越来越整个鸣叫。

X_train是类型列表,这里有名单的前三个元素:

["Great seeing you again, don't be a stranger!",
 "Beautiful day here in sunny Prague. Not a cloud in the sky",
 " pfft! i wish I had a laptop like that"]

更新

为了解决这个问题,我已经试过如下:

X_train_list = []
for sentence in X_train:
word_list = sentence.split(' ')
while("" in word_list): 
    word_list.remove("") 
X_train_list.append(word_list)
model = W2VTransformer(size=10, min_count=1, seed=1)
X_train_tfidf = model.fit(common_texts).transform(X_train_list)

这将产生以下错误:

KeyError: "word 'here' not in vocabulary"

说实话,这一次吹拂我的心灵!像“这里”一个共同的词怎么不在词汇是超越我。也想知道如果流浪字母鸣叫会引发错误,我想象的字母经常被看作是的话会导致类似问题的怪异混为一谈。

python nlp gensim word2vec topic-modeling
1个回答
0
投票

该Gensim模型确实希望作为输入,而不仅仅是一个句子列表文字列表的列表。你X_train应该是这样的:

[["Great", "seeing", "you", "again", "..."], 
["Beautiful", "day", "..."], 
...
] 

更新:至于你的问题的新的部分问题是,common_texts是由只有9句,因此它是不那么令人惊讶的是词汇量是很小的一个小数据集。使用transform之前尝试在一个更大的数据集训练。你的情况,你也许可以找到微博的数据集训练上。你也可以考虑,如果你想获得外的词汇向量使用FastText。

© www.soinside.com 2019 - 2024. All rights reserved.