繁体   English   中英

如何使用python在文本文件中查找单词的频率? 但用户应该给出输入词

[英]How to find the frequency of a word in a text file using python? But user should give the input word

我的目标:计算用户在文本文件中输入单词的频率。(在 python 中)我试过这个。但它给出了文件中所有单词的频率。我如何修改它以给出一个单词的频率由用户输入?

from collections import Counter
word=input("Enter a word:")
def word_count(test6):
        with open('test6.txt') as f:
                return Counter(f.read().split())

print("Number of input words in the file :",word_count(word))

这可能是一个幼稚的问题,但我才刚刚开始编码。所以请尝试回答。 提前致谢。

要查找文件中某个单词的频率,您可以使用str.join连接文件中的所有行,然后使用str.count

def word_count(word):
    with open('test6.txt') as f:
            return ''.join(f).count(word)


print("Number of words in the file :", word_count(input('give me a word')))

您也可以用于文本中的字数统计:

def word_count(word):
        with open('test6.txt') as f:
                return f.read().count(word)

虽然 str.count 在您只查找一个单词时效果很好,但是当您想要获得多个单词计数并有更大的文本要搜索时(比如一整本书,几兆字节的数据),使用Counter的方法更好。

在这种情况下,您可以将您的书读入Counter并多次查询。 但是,您需要清理数据,必须删除换行符和标点符号,以便您在'Is that so?'找到'so' 'Is that so?'

import string
from collections import Counter
# fixed text, normally you read your file here
text = """This text has punctation
as well as newlines. No -word- will
be unaccounted for. This text should do
as test text for now. no no no"""

# make a translation to remove punctuation and newlines from the text
# before splitting it into words that you then count once
punct = string.punctuation
p = str.maketrans(punct + "\n\r", ' '*(len(punct)+2))

wc = Counter(text.translate(p).split())

# repeatedly ask your counter how often the word occured
while True:
    word=input("Enter a word: ") 
    if not word.strip():
        break 
    print(f"'{word}' occures {wc.get(word,0)} times.") 

输出:

Enter a word: text
'text' occures 3 times.
Enter a word: has
'has' occures 1 times.
Enter a word: do
'do' occures 1 times.
Enter a word: no
'no' occures 3 times.
Enter a word: 

如果你可以做正则表达式,你也可以通过正则表达式提取单词,更多在这里: 从字符串中提取单词,删除标点符号并返回带有分隔单词的列表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM