filename = "information.txt"
library = {"lecture": 0, "tutor": 0, "exam": 0}
with open(filename) as f_obj:
content = f_obj.read() # read text into contents
words = (content.lower()).split() # create list of all words in content
for k, v in library.items():
for i in words:
if k in i:
v += 1
library[k] = v # without this line code count will not update
for k, v in library.items():
print(k.title() + ": " + str(v))
keywords = {"lecture": 0, "tutor": 0, "exam": 0}
with open("file.txt", "r") as f:
for line in f:
for key, value in keywords.items():
if key in line.lower():
value += 1
print(keywords)
这是一个很好的例子,可以使用比string matching=更聪明的方法)
让我们考虑一下:
有没有办法把每个单词都转换成向量形式(即浮点数组)?
有没有一种方法可以将每个句子转换成相同的向量形式(例如,一个与单词向量形式大小相同的浮点数组)?
首先,让我们从你的句子列表中找出所有可能的单词(我们称之为语料库):
现在让我们用词汇表中单词的索引将4个关键词表示为向量:
^{pr2}$类似地,我们循环每个句子并将其转换为向量形式:
对所有句子重复相同的内容:
现在,给定句子和单词的向量形式,您可以使用相似函数,例如cosine similarity:
现在,更系统地进行:
我想你明白了。但我们看到,s4实验室和s4考试的分数仍然持平。所以问题就变成了,“有没有办法让他们分道扬镳?”你会跳进兔子洞里:
如何最好地将句子/单词表示为固定大小的向量?
比较“主题”和“主题”的相似性?
什么是“话题”?向量实际上代表什么?
上面的答案是通常所说的一个热向量来表示单词/句子。比简单地比较字符串来“识别与主题相关的句子”要复杂得多(也称为文档聚类/分类)。E、 一个文件/句子可以有多个主题吗?在
查找这些关键词,进一步了解“自然语言处理”、“文档分类”、“机器学习”等问题。同时,如果你不介意的话,我想这个问题已经很接近了,因为“太宽泛”。在
解决方案
输出
此方法将为您计算重复次数
享受吧!在
我想你是在读文本文件或其他什么的。下面是我要怎么做的。在
这将在关键字词典中的每一行中搜索任何单词,如果找到匹配项,则会增加该键的值。在
你不应该为此需要任何外部库或任何东西。在
相关问题 更多 >
编程相关推荐