python 对一篇文章,按逗号分成一句一句的,然后在这篇文章中找到与某个句子类似的句子(包含相同的词)
2018-03-04 07:49
525 查看
#-*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("gbk")
#code:myhaspl@qq.com
#12-4.py
import numpy as np
import jieba
import copy
def get_cossimi(x,y):
myx=np.array(x)
myy=np.array(y)
cos1=np.sum(myx*myy)
cos21=np.sqrt(sum(myx*myx))
cos22=np.sqrt(sum(myy*myy))
return cos1/float(cos21*cos22)
f1_text='瑞典税务局改称台湾为中国一省:按国际惯例修正'
if __name__ == '__main__':
f1 = file('testk.txt','r')
lines = f1.read()
#lines=lines.split(u'。')
lines=lines.split(u',')
#lines=re.split(',', lines)
#for
for i in lines :
#print i
#print "ok"
if not len(i) ==1 :
f1_seg_list = jieba.cut(f1_text)
#第一个待测试数据
ftest1_seg_list = jieba.cut(i)
#读取样本文本
#去除停用词,同时构造样本词的字典
f_stop = open('stopwords.txt')
try:
f_stop_text = f_stop.read( )
f_stop_text=unicode(f_stop_text,'utf-8')
finally:
f_stop.close( )
f_stop_seg_list=f_stop_text.split('\n')
test_words={}
all_words={}
for myword in f1_seg_list:
#print ".",
if not(myword.strip() in f_stop_seg_list):
test_words.setdefault(myword,0)
all_words.setdefault(myword,0)
all_words[myword]+=1
#读取待测试文本
mytest1_words=copy.deepcopy(test_words)
for myword in ftest1_seg_list:
#print ".",
if not(myword.strip() in f_stop_seg_list):
if mytest1_words.has_key(myword):
mytest1_words[myword]+=1
#计算样本与待测试文本的余弦相似度
sampdata=[]
test1data=[]
for key in all_words.keys():
sampdata.append(all_words[key])
test1data.append(mytest1_words[key])
test1simi=get_cossimi(sampdata,test1data)
print "%s %f %s "%(chr(10)+i+u'。'+chr(10),test1simi,chr(10))
else:
continue
f1.close()
import sys
reload(sys)
sys.setdefaultencoding("gbk")
#code:myhaspl@qq.com
#12-4.py
import numpy as np
import jieba
import copy
def get_cossimi(x,y):
myx=np.array(x)
myy=np.array(y)
cos1=np.sum(myx*myy)
cos21=np.sqrt(sum(myx*myx))
cos22=np.sqrt(sum(myy*myy))
return cos1/float(cos21*cos22)
f1_text='瑞典税务局改称台湾为中国一省:按国际惯例修正'
if __name__ == '__main__':
f1 = file('testk.txt','r')
lines = f1.read()
#lines=lines.split(u'。')
lines=lines.split(u',')
#lines=re.split(',', lines)
#for
for i in lines :
#print i
#print "ok"
if not len(i) ==1 :
f1_seg_list = jieba.cut(f1_text)
#第一个待测试数据
ftest1_seg_list = jieba.cut(i)
#读取样本文本
#去除停用词,同时构造样本词的字典
f_stop = open('stopwords.txt')
try:
f_stop_text = f_stop.read( )
f_stop_text=unicode(f_stop_text,'utf-8')
finally:
f_stop.close( )
f_stop_seg_list=f_stop_text.split('\n')
test_words={}
all_words={}
for myword in f1_seg_list:
#print ".",
if not(myword.strip() in f_stop_seg_list):
test_words.setdefault(myword,0)
all_words.setdefault(myword,0)
all_words[myword]+=1
#读取待测试文本
mytest1_words=copy.deepcopy(test_words)
for myword in ftest1_seg_list:
#print ".",
if not(myword.strip() in f_stop_seg_list):
if mytest1_words.has_key(myword):
mytest1_words[myword]+=1
#计算样本与待测试文本的余弦相似度
sampdata=[]
test1data=[]
for key in all_words.keys():
sampdata.append(all_words[key])
test1data.append(mytest1_words[key])
test1simi=get_cossimi(sampdata,test1data)
print "%s %f %s "%(chr(10)+i+u'。'+chr(10),test1simi,chr(10))
else:
continue
f1.close()
相关文章推荐
- python 对一篇文章,按逗号和句号分成一句一句的,然后在这篇文章中找到与某个句子类似的句子(包含相同的词)
- python 对一篇文章,按句号分成一句一句的,然后在这篇文章中找到与某个句子类似的句子(包含相同的词)
- 关于python decorator找到的一篇比较好的文章
- wordpress用户体验优化:当搜索结果只包含一篇文章时则直接转跳到文章
- 发现一篇python 读写文件的文章 不错
- [转贴]找到一篇有关Java RMI和.NET Remoting的分析文章,大家不妨一读。
- python获取CSDN个人收藏的文章链接和标题,然后发送到指定邮件
- 一篇文章入门Python生态系统
- python遍历文件夹找到包含某字符串的文件
- 一篇文章让你彻底搞清楚Python中self的含义
- 非常励志的一篇文章,请慢慢细读,品读,从中找到你想要的答案
- 写一篇md文章然后发表再保存我们看看
- 一篇文章入门Python生态系统(Python新手入门指导)
- 网上找到的一篇java笔试文章
- 一篇文章让你彻底搞清楚Python中self的含义
- 它是Python造物主,一篇文章理解最难懂的概念
- 正当对棋盘覆盖和染色迷惑时,从网上找到一篇小学数学竞赛的文章,看了后明白了不少.
- 找了许久,终于找到一篇关于GStreamer架构的说的比较详细和底层的文章
- spark 官网例子 统计一篇文章包含字母a的个数
- python选取文件夹,然后计算该文件夹下所有文件的md5值,并列出md5值相同的文件到log中