nltk-比较英文文档相似度-完整实例
来源:互联网 发布:淘宝买家提问怎么回答 编辑:程序博客网 时间:2024/06/06 04:20
说明:
* 其中基准数据,可以来自外部,处理过程为:
- 处理为词袋
- 经过数据集的tfidf结果
* 无法处理中文
- 分词器不支持
* 未另外加载语料库
#!/usr/bin/env python#-*-coding=utf-8-*- """原始数据"""#缩水版的courses,实际数据的格式应该为 课程名\t课程简介\t课程详情,并已去除html等干扰因素courses = ['Writing II: Rhetorical Composing', 'Genetics and Society: A Course for Educators', 'General Game Playing', 'Genes and the Human Condition (From Behavior to Biotechnology)', 'A Brief History of Humankind', 'New Models of Business in Society', 'Analyse Numrique pour Ingnieurs', 'Evolution: A Course for Educators', 'Coding the Matrix: Linear Algebra through Computer Science Applications', 'The Dynamic Earth: A Course for Educators']#实际的 courses_name = [course.split('\t')[0] for course in courses]courses_name = ['Writing II: Rhetorical Composing', 'Genetics and Society: A Course for Educators', 'General Game Playing', 'Genes and the Human Condition (From Behavior to Biotechnology)', 'A Brief History of Humankind', 'New Models of Business in Society', 'Analyse Numrique pour Ingnieurs', 'Evolution: A Course for Educators', 'Coding the Matrix: Linear Algebra through Computer Science Applications', 'The Dynamic Earth: A Course for Educators'] """预处理(easy_install nltk)""" #引入nltkimport nltk#nltk.download() #下载要用的语料库等,时间比较长,最好提前准备好 #分词from nltk.corpus import browntexts_lower = [[word for word in document.lower().split()] for document in courses]from nltk.tokenize import word_tokenizetexts_tokenized = [[word.lower() for word in word_tokenize(document)] for document in courses] #去除停用词from nltk.corpus import stopwordsenglish_stopwords = stopwords.words('english')texts_filtered_stopwords = [[word for word in document if not word in english_stopwords] for document in texts_tokenized]#去除标点符号english_punctuations = [',', '.', ':', ';', '?', '(', ')', '[', ']', '&', '!', '*', '@', '#', '$', '%']texts_filtered = [[word for word in document if not word in english_punctuations] for document in texts_filtered_stopwords] #词干化from nltk.stem.lancaster import LancasterStemmerst = LancasterStemmer()texts_stemmed = [[st.stem(word) for word in docment] for docment in texts_filtered] #去除过低频词all_stems = sum(texts_stemmed, [])stems_once = set(stem for stem in set(all_stems) if all_stems.count(stem) == 1)texts = [[stem for stem in text if stem not in stems_once] for text in texts_stemmed] """注意:本例子中只用了course_name字段,大多数word频率过低,造成去除低频词后,有些document可能为空 因此后面的处理结果只做示范""" """引入gensim,正式开始处理(easy_install gensim) 输入: 1.去掉了停用词 2.去掉了标点符号 3.处理为词干 4.去掉了低频词 """from gensim import corpora, models, similarities #为了能看到过程日志import logginglogging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) dictionary = corpora.Dictionary(texts)corpus = [dictionary.doc2bow(text) for text in texts] #doc2bow(): 将collection words 转为词袋,用两元组(word_id, word_frequency)表示tfidf = models.TfidfModel(corpus)corpus_tfidf = tfidf[corpus] #拍脑袋的:训练topic数量为10的LSI模型lsi = models.LsiModel(corpus_tfidf, id2word=dictionary, num_topics=10)index = similarities.MatrixSimilarity(lsi[corpus]) # index 是 gensim.similarities.docsim.MatrixSimilarity 实例 """对具体对象相似度匹配"""#选择一个基准数据ml_course = texts[2]ml_bow = dictionary.doc2bow(ml_course) #在上面选择的模型数据 lsi 中,计算其他数据与其的相似度ml_lsi = lsi[ml_bow] #ml_lsi 形式如 (topic_id, topic_value)sims = index[ml_lsi] #sims 是最终结果了, index[xxx] 调用内置方法 __getitem__() 来计算ml_lsi#排序,为输出方便sort_sims = sorted(enumerate(sims), key=lambda item: -item[1]) #查看结果print sort_sims[0:10] #看下前10个最相似的,第一个是基准数据自身print courses_name[2] #看下实际最相似的数据叫什么
0 0
- nltk-比较英文文档相似度-完整实例
- nltk-比较中文文档相似度-完整实例
- 字符串相似度比较实例
- 使用NLTK计算word的相似度
- gensim 英文文本相似度
- 【python】NLTK-02-相似度 频率 similar common_contexts count
- 文档余弦相似度
- JS比较相似度
- 图像相似度比较
- 图片相似度比较
- 字符串相似度比较
- nltk英文词性分析
- 词语相似度计算:2、使用NLTK和WordNet计算词语相似度
- 用docsim/doc2vec/LSH比较两个文档之间的相似度
- 文档相似度算法 Simhash
- lsi计算文档相似度
- doc2vec计算文档相似度
- 计算文档的相似度
- Java 小例子:按照顺时针或逆时针方向输出一个数字矩阵
- openfire环境搭建并进行测试
- Coherence-Enhancing Shock Filters(附源码)
- 删除或查找特定时间范围的后缀文件(python)
- 右移实现除法中的偏置量问题
- nltk-比较英文文档相似度-完整实例
- Swift -2 字符串与集合
- 欢迎使用CSDN-markdown编辑器
- Tomcat 高性能实现关键点
- 野鸟装备2.0问题一览
- startActivityForResult() 中requestCode 和 resultCode 的作用
- C++的Json解析库:jsoncpp和boost
- pyton_3
- Microsoft Visual C++ Package Server 已停止工作