笔记 | gensim的simple_preprocess

在使用word2vec时涉及gensim.utils.simple_preprocess(),用于将句子分割为单词列表,效果等同于

	sen = "my daddy is a ab good man man"
	sp= gensim.utils.simple_preprocess(sen)
    print(sentence,type(sp))
    print(list(sen.split()))

结果如下:
在这里插入图片描述
两者不同之处在于,simple_preprocess自动过滤了单词长度小于2的词汇。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在使用 `simple_preprocess` 函数时遇到了 `NameError: name 'simple_preprocess' is not defined` 错误,这意味着你的代码中没有正确导入 `gensim` 库中的 `simple_preprocess` 函数。 要解决这个问题,你可以在代码中添加以下导入语句: ```python from gensim.utils import simple_preprocess ``` 然后,你就可以使用 `simple_preprocess` 函数来进行简单的文本预处理和分词。 在更新了导入语句后,你的代码应该类似于: ```python from gensim import corpora from gensim.models import LdaModel from gensim.utils import simple_preprocess # 准备文本数据 documents = [ "This is the first document.", "This document is the second document.", "And this is the third one.", "Is this the first document?" ] # 分词处理 texts = [simple_preprocess(document) for document in documents] # 为文本数据创建字典 dictionary = corpora.Dictionary(texts) # 将文本转换为词袋表示 corpus = [dictionary.doc2bow(text) for text in texts] # 建立LDA模型 lda_model = LdaModel(corpus=corpus, id2word=dictionary, num_topics=2, passes=10) # 输出主题分布 for idx, topic in lda_model.print_topics(-1): print(f"Topic: {idx}\nWords: {topic}\n") # 推断新文档的主题 new_document = "This is a new document." new_text = simple_preprocess(new_document) new_bow = dictionary.doc2bow(new_text) new_topics = lda_model.get_document_topics(new_bow) print(f"New Document Topics: {new_topics}") ``` 现在,你应该能够正常使用 `simple_preprocess` 函数了。如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值