【问题标题】:A program that counts specific stings in a text, such like "climate finance"计算文本中特定因素的程序,例如“气候金融”
【发布时间】:2023-03-04 02:25:02
【问题描述】:
from collections import Counter
input = 'file.txt'

CounterWords = {}
words = {}
with open(input,'r', encoding='utf-8-sig') as fh:
  for line in fh:
    word_list = line.replace(',','').replace('\'','').replace('.','').lower().split()
    for word in word_list:
      if len(word) < 6
          continue
      elif word not in CounterWords:
          CounterWords[word] = 1
      else:
          CounterWords[word] = CounterWords[word] + 1
N = 50

top_words = Counter(CounterWords).most_common(N)
for word, frequency in top_words:
    print("%s %d" % (word, frequency))

目前,我可以选择两个最常用的单词,其中字符串超过 X 个字符。

程序应筛选文本并计算以下单词:

“气候金融” “市场营销失败” “巴黎 2015”

仍应包含每个字符串的最少字符数,以防止出现“I and”等结果。

【问题讨论】:

  • 获取 word_list[i:i+1] 并使用它。
  • 也许你应该首先删除所有短词——所谓的“停用词”。您可以使用模块 NLTK 中的停用词。见:Stopword removal with NLTK
  • "get word_list[i:i+1]" 看起来很简单。我必须把它放在哪里?在循环内还是在开头?
  • 创建新的for循环for i in range(len(word_list)-1): word_list[i:i+1]
  • 我愿意:for word in range(len(word_list)-1): word_list[word:word+1](而不是上面原始代码中的 for word in word_list:(第 9 行)我在下一行出现错误 if len(word) &lt; 6: -> TypeError: object of type 'int' 没有 len()

标签: python count word


【解决方案1】:

您可以简单地使用your_file_content.count(your_string)

from collections import Counter
input = 'D:\\file.txt'

import itertools
def pairwise(iterable):
    # "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b)  

CounterWords = {}
CounterPairs = {}
words = {}
file_content = ''
with open(input,'r', encoding='utf-8-sig', errors='ignore') as fh:
  file_content = fh.read().replace('\n', ' ')
  word_list = file_content.replace(',','').replace('\'','').replace('.','').lower().split()
  word_list = list(dict.fromkeys(word_list)) # to remove duplicates
  word_pairs_list = pairwise(word_list)
  for word in word_list:
    if len(word) < 6:
      continue
    else:
      CounterWords[word] = file_content.count(word)
  for pair in word_pairs_list:
    CounterPairs[pair] = file_content.count(' '.join(pair))
N = 50

# for all single words :
top_words = Counter(CounterWords).most_common(N)
for word, frequency in top_words:
  print("%s %d" % (word, frequency))

# for all pairs :
top_pairs = Counter(CounterPairs).most_common(N)
for pair, frequency in top_pairs:
  print("%s %d" % (pair, frequency))

# for specific pairs :
print("\n%s %d" % ('climate finance', CounterPairs[('climate', 'finance')]))

pairwise 函数取自:Iterate a list as pair (current, next) in Python

【讨论】:

  • thx 我对通用代码感兴趣。目前,我不知道哪些术语出现严重。你有什么想法吗?
  • 术语是什么意思? “Paris 2015”是一个术语吗?,您在问题中称它们为单词。请澄清。
  • 我筛选了一个100000字的文档,我想知道:首先,分析哪些词(即“climate”),其次是哪些词对(即“climate finance”)在文本。
  • 首先,按照@furas 的建议使用 nltk。其次,确保正确生成单词和成对词。最后用.count()Counter来分析这一切。
  • 一些帮助从单词列表中生成成对单词:stackoverflow.com/a/5434936/4374588
猜你喜欢
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 2012-12-30
  • 1970-01-01
  • 2018-05-05
  • 2020-05-14
  • 2019-05-20
相关资源
最近更新 更多