【问题标题】:i'm trying to check a string belongs to which text file我正在尝试检查一个字符串属于哪个文本文件
【发布时间】:2016-12-16 07:32:17
【问题描述】:

我正在尝试抓取博客的 cmets 并确定它是否具有情感性和信息性。

我找出了最常用的名词(前 10 名)。

在这个过程之后,我制作了两个 txt 文件。

第一个文件包含情感名词。第二个文件包含信息名词。

最后,我想知道一个博客是否有更多的情感名词或更多的信息名词。最后一个流程需要制作哪些代码?

【问题讨论】:

  • 文件有多大?如果文件很小并且可以轻松处理,您可以将这两个文件作为字典导入并在python中编写带有计数器的for循环,如果字典中的单词,则增加计数器。哪个计数器更高,这就是博客包含的更多
  • 你能给我看看粗略的代码吗?请..TT
  • 自己尝试过什么了吗? SO 不是免费的编码服务,你知道吗?
  • 您是否尝试在插入每个文件时对名词进行计数?

标签: string if-statement for-loop blogs


【解决方案1】:
# 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)

可能有更好的方法来编写它,但我只是写得很快,所以它不是最有效的代码

【讨论】:

    猜你喜欢
    • 2022-11-16
    • 2011-05-17
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2014-12-26
    • 2016-06-24
    • 1970-01-01
    相关资源
    最近更新 更多