繁体   English   中英

Python 程序,可以计算和识别文本文件中的首字母缩写词的数量

[英]Python program which can count and identify the number of acronyms in a text file

我已经从我这边尝试过这段代码,任何建议和帮助都表示赞赏。 更具体地说,我想创建一个 python 程序,它可以计算和识别文本文件中的首字母缩写词的数量。 并且程序的 output 应该显示指定文本文件中存在的每个首字母缩略词以及每个首字母缩略词在文件中出现的次数。

*注意-以下代码未提供所需的 output。 感谢任何类型的帮助和建议。

文本文件的链接,你们可以看看 - https://drive.google.com/file/d/1zlqsmJKqGIdD7qKicVmF0W6OgF5-g7Qk/view?usp=sharing

此文本文件包含其中使用的各种首字母缩略词。 所以,我基本上想写一个 python 脚本来识别这些首字母缩写词并计算这些首字母缩写词出现的次数。 首字母缩略词有多种类型,可以是 2 个或更多字母,可以是小写字母或大写字母。 有关首字母缩略词的进一步参考,请查看谷歌驱动器上提供的文本文件。

任何更新的代码也很感激。

acronyms = 0 # number of acronyms

#open file File.txt in read mode with name file
with open('Larex_text_file.txt', "r", errors ='ignore') as file:
    text = str(file.read())
    import re

    print(re.sub("([a-zA-Z]\.*){2,}s?", "", text))

    for line in text: # for every line in file
        for word in line.split(' '): # for every word in line
            if word.isupper(): # if word is all uppercase letters
                acronyms+=1

print("Number of acronyms:", acronyms) #print number of acronyms

在构建一个小文本文件然后尝试您的代码时,我对您的代码进行了一些调整以简化它,并且仍然获取文本文件中所有大写字母的单词数。

acronyms = 0 # number of acronyms

#open file File.txt in read mode with name file
with open('Larex_text_file.txt', "r", errors ='ignore') as file:
    text = str(file.read())

    for word in text.split(' '): # for every word in line
        if word.isupper() and word.isalpha(): # if word is all uppercase letters
            acronyms+=1

print("Number of words that are all uppercase:", acronyms) #print number of acronyms

首先,只对从阅读文本中分离出来的单词使用一个简单的循环,然后程序只检查单词是否全是字母并且单词中的所有字母都是大写的。

为了测试,我构建了一个小文本文件,其中一些单词全部大写。

NASA and UCLA have teamed up with the FBI and JPL.
also UNICEF and the WWE have teamed up.  
With that, there should be five words that are all uppercase.

而且,运行时,这是终端上的 output。

@Una:~/Python_Programs/Acronyms$ python3 Acronym.py 
Number of words that are all uppercase: 5

你会注意到我在这里有点迂腐,指的是“大写”单词的数量,而不是称它们为首字母缩略词。 我不确定您是否正在尝试实际推导出真正的首字母缩略词,但如果您是,此链接可能会有所帮助:

首字母缩略词

试一试,看看它是否符合您项目的精神。

回答问题——

#open file File.txt in read mode with name file
with open('Larex_text_file.txt', "r", errors ='ignore') as file:
    text = str(file.read())
    for word in text.split(' '): # for every word in line
        if word.isupper() and word.isalpha(): # if word is all uppercase letters
            acronyms+=1
            if len(word) == 1:  #ignoring the word found in the file of single character as they are not acronyms
              pass
            else:
              index = len(acronym_word)
              acronym_word.insert(index, word)  #storing all the acronyms founded in the file to a list

uniqWords = sorted(set(acronym_word)) #remove duplicate words and sort the list of acronyms
for word in uniqWords:
    print(word, ":", acronym_word.count(word))

从您的评论来看,听起来每个首字母缩略词至少以全大写单词出现一次,然后以小写形式出现多次。

我建议对文本进行两次遍历:第一次收集所有大写单词,第二次遍历搜索您在第一次遍历中收集的单词的每次出现,不区分大小写。

您可以使用collections.Counter快速统计字数。

您可以使用''.join(filter(str.isalpha, word.lower()))一个单词的非字母字符并忽略其大小写。

在下面的代码片段中,我使用io.StringIO来模拟打开文本文件。

from io import StringIO
from collections import Counter

text = '''First we have CR and PU as uppercase words. A word which first
 appeared as uppercase can also appear as lowercase.
For instance, cr and pu appear in lowercase, and pu appears again.
And again: here is a new occurrence of pu.
An acronym might or might not have punctuation or numbers in it: CR-1,
 C.R., cr.
A word that contains only a singly letter will look like an acronym
 if it ever appears as the first word of a sentence.'''

#with open('path/to/file.txt', 'r') as f:
with StringIO(text) as f:
    counts = Counter(''.join(filter(str.isalpha, word.lower()))
                     for line in f for word in line.split())
    f.seek(0)
    uppercase_words = set(''.join(filter(str.isalpha, word.lower()))
                          for line in f
                          for word in line.split() if word.isupper())

acronyms = Counter({w: c for w,c in counts.items() if w in uppercase_words})

print(acronyms)
# Counter({'cr': 5, 'a': 5, 'pu': 4})

暂无
暂无

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

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