gensim自然语言处理(续)
上一篇,已经实现了如何将一条语句在一个语料库中比较相似度,
发现运行的时候每次都要编译语料库,通过查找资料,可以一次性编译成预料库,存人文件
编译语料库代码 11_k.py
import sys
import jieba
reload(sys)
sys.setdefaultencoding("utf-8")
from gensim import corpora,models,similarities
alist = [] import json def fenci():
for i_text in open("xaa.json"):
f_json = json.loads(i_text)
kk = f_json["content"]
item_str = jieba.cut(kk.encode("utf-8"),cut_all=True)
a = " ".join(item_str)
alist.append(a) fenci() class MyCorpus(object):
def __iter__(self):
for item_str in alist:
yield item_str.split(' ') Corp = MyCorpus()
dictionary = corpora.Dictionary(Corp) #dictionary = corpora.Dictionary(Corp)
dictionary.save("bbb.dict") #存入本地数据
corpus = [dictionary.doc2bow(text) for text in Corp] corpora.MmCorpus.serialize('deerwester1.mm', corpus) # 存入硬盘,以备后需 编译好了 bbb.dict deerwester1.mm 文件,在一下一个代码中直接调用11_main.py
import jieba
from gensim import corpora, models, similarities
dictionary = corpora.Dictionary.load('bbb.dict') #调用
corpus = corpora.MmCorpus('deerwester1.mm') tfidf = models.TfidfModel(corpus) corpus_tfidf = tfidf[corpus] def read_file(i):
with open("xaa.json","rt") as f:
y = f.readlines()[i]
print y def test_kk(test):
test_data_1 = '请假洛阳牡丹'
test_cut_raw_1 = jieba.cut(test)
doc_new = " ".join(test_cut_raw_1) test_corpus_1 = dictionary.doc2bow(doc_new.split()) vec_tfidf = tfidf[test_corpus_1] index = similarities.MatrixSimilarity(corpus_tfidf) sims = index[vec_tfidf] similarit = list(sims) #print(list(enumerate(sims)))
sims = sorted(enumerate(sims), key=lambda item: -item[1])
for i in range(20): #只读取前20 个数据,
print sims[i] #相似度是与元组的形式存在
k = sims[i]
read_file(k[0]) #将相似文件中相似的语句打印出来 def buss_mian():
while True:
test = raw_input("please input test:")
test_kk(test) if __name__ == "__main__": buss_mian()
gensim自然语言处理(续)的更多相关文章
- gensim自然语言处理
参考代码 ChineseClean_demo1.py: # -*- coding:utf-8 -*- import xlrd import xlwt ''' python3.4 ''' # file ...
- 面试题之python基础
基础语法 输入和输出 代码中要修改不可变的数据会出现什么问题,抛出什么异常? 代码不会征程运行,抛出TypeError异常 a = 1,b = 2,不用中间变量交换a和b的值? # 方法1 a = a ...
- Python2和Python3的差异
之前做Spark大数据分析的时候,考虑要做Python的版本升级,对于Python2和Python3的差异做了一个调研,主要对于语法和第三方工具包支持程度进行了比较. 基本语法差异 核心类差异 Pyt ...
- Python2和Python3比较分析
一直有看到网上有讨论Python2和Python3的比较,最近公司也在考虑是否在spark-python大数据开发环境中升级到python3.通过本篇博文记录Python2.7.13和Pthon3.5 ...
- Python2 和Python3 的差异总结
一.基本语法差异 1.1 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3 ...
- 巨蟒python全栈开发flask1
1.整体把握 (1)路飞学城 - RestAPI 前后端分离开发 Django Vue.js - DRF DjangoRestFromwork - 线上商城的经验 (2)智能玩具 - RestAPI ...
- python2和python3的区别(转)
基本语法差异 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3只支持uni ...
- python 全栈开发,Day133(玩具与玩具之间的对话,基于jieba gensim pypinyin实现的自然语言处理,打包apk)
先下载github代码,下面的操作,都是基于这个版本来的! https://github.com/987334176/Intelligent_toy/archive/v1.6.zip 注意:由于涉及到 ...
- 自然语言处理--jieba和gensim的分词功能
一.jieba分词功能 1.主要模式 支持三种分词模式: 精确模式,试图将句子最精确地切开,适合文本分析: 全模式,把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义: 搜索引擎模 ...
随机推荐
- POJ 1860 Currency Exchange【SPFA判环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)
Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input outp ...
- XPath语法和CSS选择器介绍
XPath语法 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历.XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 ...
- PHP:根据二维数组中的某个字段进行排序
首先了解下以下两个函数: 1.array_column() 返回输入数组中某个单一列的值. 2.array_multisort() 函数返回排序数组.您可以输入一个或多个数组.函数先对第一个数组进行排 ...
- Minimum Height Trees -- LeetCode
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [BZOJ4836]二元运算(分治FFT)
4836: [Lydsy1704月赛]二元运算 Time Limit: 8 Sec Memory Limit: 128 MBSubmit: 578 Solved: 202[Submit][Stat ...
- [BZOJ4819][SDOI2017]新生舞会(分数规划+费用流,KM)
4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1097 Solved: 566[Submit][Statu ...
- [HDU1542]Atlantis(扫描线+线段树)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 【OpenJudge9270】【Pku2440】【递推】DNA
DNA [描述] A kind of virus has attacked the X planet, and many lives are infected. After weeks of stud ...
- C语言读书笔记
1.c语言中一共有32个关键字,分别是: auto.int.double.long.char.short.float.unsigned.signed.sizeof.extern. static.got ...