我正在尝试检查一个字符串属于哪个文本文件
i'm trying to check a string belongs to which text file
我正在尝试抓取博客的评论并确定它是否具有情感性和信息性。
我找出了最常用的名词(前 10 个)。
经过这个过程,我制作了两个txt文件。
第一个文件包含情感名词。第二个文件包含信息名词。
最后,我想知道一个博客是情感名词多还是信息名词多。最后一道工序需要制作哪些代码?
# This is the file where you have your top 10 nouns
fc = open("words.txt")
list_blog = []
for line in fc:
list_blog.append(line.strip())
f1 = open("file1.txt") # This is your first file of emotional nouns
d1 = {}
c = 0
for line in fc:
c+=1
d1[line] = str(c)
f2 = open("file2.txt") # This is your seconf file of informational nouns
d2 = {}
c = 0
for line in fc:
c+=1
d2[line] = str(c)
count1 = 0
count2 = 0
count3 = 0
for i in list_blog:
if i in d1:
count1+=1
elif i in d2:
count2+=1
else:
count3+=1
print(count1,count2,count3)
可能有更好的写法,但我写得很快,所以它不是最高效的代码
我正在尝试抓取博客的评论并确定它是否具有情感性和信息性。
我找出了最常用的名词(前 10 个)。
经过这个过程,我制作了两个txt文件。
第一个文件包含情感名词。第二个文件包含信息名词。
最后,我想知道一个博客是情感名词多还是信息名词多。最后一道工序需要制作哪些代码?
# This is the file where you have your top 10 nouns
fc = open("words.txt")
list_blog = []
for line in fc:
list_blog.append(line.strip())
f1 = open("file1.txt") # This is your first file of emotional nouns
d1 = {}
c = 0
for line in fc:
c+=1
d1[line] = str(c)
f2 = open("file2.txt") # This is your seconf file of informational nouns
d2 = {}
c = 0
for line in fc:
c+=1
d2[line] = str(c)
count1 = 0
count2 = 0
count3 = 0
for i in list_blog:
if i in d1:
count1+=1
elif i in d2:
count2+=1
else:
count3+=1
print(count1,count2,count3)
可能有更好的写法,但我写得很快,所以它不是最高效的代码