如何查找和计算列表和文本之间的多个交叉点?

2024-04-26 10:01:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在用Python编写一个程序来计算德语文本中的英语成分。我想知道整篇课文中有多少次使用英语。为此,我列出了德语中的所有英语,如下所示:

abchecken
abchillen
abdancen
abdimmen
abfall-container
abflug-terminal

名单还在继续。。。 然后我检查了这个列表和要分析的文本之间的交叉点,但是这只给出了出现在两个文本中的所有单词的列表,例如:Anglicisms : 4:{'abdancen', 'abchecken', 'terminal'}

我真的希望porgram输出这些单词出现的次数(最好按频率排序),例如:

Anglicisms: abdancen(5), abchecken(2), terminal(1)

这是我目前掌握的代码:

 #counters to zero
 lines, blanklines, sentences, words = 0, 0, 0, 0

 print ('-' * 50)

 while True:
     try:
       #def text file
       filename = input("Please enter filename: ")
       textf = open(filename, 'r')
       break
     except IOError:
       print( 'Cannot open file "%s" ' % filename )

 #reads one line at a time
 for line in textf:
   print( line, )  # test
   lines += 1

   if line.startswith('\n'):
     blanklines += 1
   else:
     #sentence ends with . or ! or ?
    #count these characters
     sentences += line.count('.') + line.count('!') + line.count('?')

     #create a list of words
     #use None to split at any whitespace regardless of length
     tempwords = line.split(None)
     print(tempwords)

     #total words
     words += len(tempwords)

 #anglicisms
     words1 = set(open(filename).read().split())
     words2 = set(open("anglicisms.txt").read().split())

     duplicates  = words1.intersection(words2)


 textf.close()
 print( '-' * 50)
 print( "Lines       : ", lines)
 print( "Blank lines : ", blanklines)
 print( "Sentences   : ", sentences)
 print( "Words       : ", words)
 print( "Anglicisms  :  %d:%s"%(len(duplicates),duplicates))

我的第二个问题是,这不包括那些英语,换句话说。例如,如果“big”在英语列表中,而“bigfoot”在文本中,则忽略此事件。我该怎么修?你知道吗

来自瑞士的问候!你知道吗


Tags: 文本列表countlineopenfilenameterminalsplit
2条回答

我会这样做:

from collections import Counter
anglicisms = open("anglicisms.txt").read().split()

matches = []
for line in textf:
    matches.extend([word for word in line.split() if word in anglicisms])

anglicismsInText = Counter(matches)

关于第二个问题,我觉得有点难。以你的例子来说,“big”是一种英语,而“bigfoot”应该匹配,但是“Abigail”呢?还是“过大”?每次在字符串中发现英语时,它是否应该匹配?一开始?最后?一旦知道了这一点,就应该构建一个与之匹配的正则表达式

编辑:要匹配以英语开头的字符串,请执行以下操作:

def derivatesFromAnglicism(word):
    return any([word.startswith(a) for a in anglicism])

matches.extend([word for word in line.split() if derivatesFromAnglicism(word)])

这将解决您的第一个问题:

anglicisms = ["a", "b", "c"]
words = ["b", "b", "b", "a", "a", "b", "c", "a", "b", "c", "c", "c", "c"]

results = map(lambda angli: (angli, words.count(angli)), anglicisms)
results.sort(key=lambda p:-p[1])

结果如下:

[('b', 5), ('c', 5), ('a', 3)]

对于第二个问题,我认为正确的方法是使用正则表达式。你知道吗

相关问题 更多 >