【发布时间】: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