【问题标题】:I get 'single' characters as learned vocabulary on word2vec genism as an output我在 word2vec gensim 上将“单个”字符作为学习词汇作为输出
【发布时间】:2020-01-03 19:02:11
【问题描述】:

我是 word2vec 的新手,我已经通过 word2vec 训练了一个文本文件以进行特征提取,而不是当我查看训练的单词时,我发现它是单个字符而不是单词,我在这里错过了什么?谁来帮忙

我尝试将标记而不是原始文本输入模型

import nltk

from pathlib import Path
data_folder = Path("")
file_to_open = data_folder / "test.txt"
#read the file
file = open(file_to_open , "rt")
raw_text = file.read()
file.close()

#tokenization
token_list = nltk.word_tokenize(raw_text)

#Remove Punctuation
from nltk.tokenize import punkt
token_list2 = list(filter(lambda token : punkt.PunktToken(token).is_non_punct,token_list))
#upper to lower case
token_list3 = [word.lower() for word in token_list2]
#remove stopwords
from nltk.corpus import stopwords
token_list4 = list(filter(lambda token: token not in stopwords.words("english"),token_list3))

#lemmatization
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
token_list5 = [lemmatizer.lemmatize(word) for word in token_list4]
print("Final Tokens are :")
print(token_list5,"\n")
print("Total tokens : ", len(token_list5))

#word Embedding
from gensim.models import Word2Vec
# train model
model = Word2Vec(token_list5, min_count=2)
# summarize the loaded model

    print("The model is :")
    print(model,"\n")`enter code here`

# summarize vocabulary

    words = list(model.wv`enter code here`.vocab)
    print("The learned vocabulary words are : \n",words)

Output- ['p', 'o', 't', 'e', 'n', 'i', 'a', 'l', 'r', 'b', 'u', 'm', 'h', 'd', 'c', 's', 'g', 'q', 'f', 'w', '-']
Expected -[ 'potenial', 'xyz','etc']

【问题讨论】:

    标签: nlp gensim word2vec feature-extraction text-classification


    【解决方案1】:

    Word2Vec 需要它的训练语料库是一个序列,其中每个项目(文本/句子)都是一个 list-of-string-tokens

    如果您改为传递原始字符串文本,则每个文本都将显示为 一个字符标记列表,这将产生您所看到的最终词汇表,其中所有学到的“单词”只是单个字符。

    因此,请仔细查看您的 token_list5 变量。因为它是一个列表,所以token_list5[0] 是什么? (它是一个字符串列表吗?)token_list5[0][0] 是什么? (是一个完整的单词吗?)

    【讨论】:

    • 谢谢,我只是列了一个列表,现在有意义了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2016-06-06
    • 2016-09-08
    • 1970-01-01
    • 2017-07-19
    • 2021-06-26
    相关资源
    最近更新 更多