python - 当应用 word2vec 时,只显示字符而不是单词 ؟
问题描述
这是我的代码,您可以看到我将句子转换为单词,但是当我在句子中应用 word2vec 模型时我仍然遇到问题我使用阿拉伯语文本 anaconda 版本 4.7.12
sentences = nltk.sent_tokenize(str(sentences1))
sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
for i in range(len(sentences)):
sentences[i] = [word for word in sentences[i] if word not in stopwords.words('arabic')]
sentences = re.sub(r'[^\w\s]','',(str(sentences)))
sentences = re.sub("\d+", "", sentences)
sentences =sentences.strip()
sentences = nltk.word_tokenize(sentences)
from gensim.models import Word2Vec
model = Word2Vec(sentences, min_count=1)
words1 = model.wv.vocab
在 words1 中,词汇只是显示了字母
解决方案
您的标记化和预处理看起来有点混乱。在顶部进行单词标记后,您再次将其全部变回一条巨大的字符串就行了……
sentences = re.sub(r'[^\w\s]','',(str(sentences)))
对该字符串执行另一个词标记化仍然只留下一个列表,其中每个项目都是简单的词——这意味着每个假设的“句子”现在只是一个词,所以它是每个“句子”的字符列表而不是列表的话。
我建议在词标记化之前对字符串进行基于字符串的清理,并且仅将词标记化作为最后一步。并且,sentences
在传递给Word2Vec
. 例如,如果您要执行:
print(str(iter(sentences).next())
您应该看到 中的第一项sentences
,它应该是一个单词列表,而不仅仅是一个字符串。(如果它只是一个字符串,你会看到你所看到的症状——Word2Vec
只学习了单个字符。)
推荐阅读
- sftp - 我可以为多个本地环境使用相同的集成运行时服务器吗
- openshift - 在openshift中建立pod之间的相互tls连接
- python - 当我使用合并函数在 python 中合并 2 个数据帧时,行数会减少
- java - 找不到 [SaajSoapMessage 的端点映射,SOAP WS 执行期间出现异常
- android - 应用程序正在运行通知一直显示
- algorithm - ROS move_base D* (DStar) 路径规划算法实现
- android - 为什么?android:attr/textColorPrimary 在夜间模式通知中是黑色的?
- javascript - 删除选择器类 jquery
- github - Azure DevOps 管道找不到管道 YAML 文件
- python - 使用 Python 的 FontAwesome