首页 > 解决方案 > 余弦相似度和句子

问题描述

所以我试图用我拥有的文本文件做余弦相似度。https://lms.uwa.edu.au/bbcswebdav/pid-1143173-dt-content-rid-16133365_1/courses/CITS1401_SEM-2_2018/CITS1401_SEM-2_2018_ImportedContent_20180713092326/CITS1401_SEM-1_2018/Unit%20Content/sample2_2Project/Resources.文本文件

我想知道如何逐句打印这个句子,而不是 readline() 逐行阅读。我正在尝试创建句子变量。例如

s1 = "the mississippi is well worth reading about"
s2 = "it is not a commonplace river, but on the contrary is in all ways remarkable"

这是第一个解决方法吗?如果是这样,我知道该怎么做的下一步是从句子中删除常用词,只留下唯一词进行比较。

如何在句号处停止,然后将该句子存储到循环遍历文本的变量中?

谢谢

标签: pythonpython-3.x

解决方案


你是这个意思吗:

with open("file.txt",'r') as in_f:
  sentences = in_f.read().replace('\n','').split('.')
  for each s in sentences:
     #your code

推荐阅读