当前位置: 首页 > 知识库问答 >
问题:

比较不同文件中的单词

李博达
2023-03-14

我在网上找不到如何比较文件之间的单词的任何例子。我需要确定文件之间共享的字数和每个文件(相对于其他文件)唯一的字数。我的最终输出应该包括7个数字:文件1和文件2的总字数,文件1和文件2的唯一字数,文件1和文件2之间共享的字的#,文件1中但不在文件2中的字的#,以及文件2中但不在文件1中的字的#。我知道我必须使用set()来完成此操作,但我不明白如何操作。

import glob
from collections import Counter

path = "c-darwin-chapter-?.txt"

wordcount = {}

for filename in glob.glob(path):
  with open("c-darwin-chapter-1.txt", 'r') as f1, open("c-darwin-chapter-2.txt", 'r') as f2:
      f1_word_list = Counter(f1.read().replace(',','').replace('.','').replace("'",'').replace('!','').replace('&','').replace(';','').replace('(','').replace(')','').replace(':','').replace('?','').lower().split())

      print("Total word count per file: ", sum(f1_word_list.values()))
      print("Total unique word count: ", len(f1_word_list))

      f2_word_list = Counter(f2.read().replace(',','').replace('.','').replace("'",'').replace('!','').replace('&','').replace(';','').replace('(','').replace(')','').replace(':','').replace('?','').lower().split())

      print("Total word count per file: ", sum(f2_word_list.values()))
      print("Total unique word count: ", len(f2_word_list))

#if/main commented out but final code must use if/main and loop
#if __name__ == '__main__':
#   main()

期望输出:

Total word count
   Chapter1 = 11615
   Chapter2 = 4837

Unique word count
   Chapter1 = 1991
   Chapter2 = 1025

Words in Chapter1 and Chapter2: 623
Words in Chapter1 not in Chapter2: 1368
Words in Chapter2 not in Chapter1: 402

共有1个答案

庾和昶
2023-03-14

您可以在这两个文件中读取,并将读取的文本转换为列表/集。使用集合,您可以使用集合运算符来计算它们之间的交点/差点:

s.intersection(t)    s & t    new set with elements common to s and t  
s.difference(t)      s - t    new set with elements in s but not in t

关于集合操作的解释表可以在这里找到:Doku2.x/也适用于3.7

演示:

file1 = "This is some text in some file that you can preprocess as you " +\
        "like. This is some text in some file that you can preprocess as you like."

file2 = "this is other text about animals and flowers and flowers and " +\
        "animals but not animal-flowers that has to be processed as well"

# split into list - no .lower().replace(...) - you solved that already
list_f1 = file1.split() 
list_f2 = file2.split()

# create sets from list (case sensitive)
set_f1 = set( list_f1 )
set_f2 = set( list_f2 )

print(f"Words: {len(list_f1)} vs {len(list_f2)} Unique {len(set_f1)} vs {len(set_f2)}.")
# difference
print(f"Only in 1: {set_f1-set_f2} [{len(set_f1-set_f2)}]")
# intersection
print(f"In both {set_f1&set_f2} [{len(set_f1&set_f2)}]")
# difference the other way round
print(f"Only in 2:{set_f2-set_f1} [{len(set_f2-set_f1)}]")
Words: 28 vs 22 Unique 12 vs 18.
Only in 1: {'like.', 'in', 'you', 'can', 'file', 'This', 'preprocess', 'some'} [8]
In both {'is', 'that', 'text', 'as'} [4]
Only in 2:{'animals', 'not', 'but', 'animal-flowers', 'to', 'processed',
           'has', 'be', 'and', 'well', 'this', 'about', 'other', 'flowers'} [14]
 类似资料:
  • 我想比较两个音频文件,例如mp3和wav。我用Musicg通过指纹进行比较。 musicg只在wav上工作,所以我使用JAVE将mp3转换为wav 现在的问题是,当我尝试比较两个我不转换指纹的wav时,工作完美,但是当我比较两个不同的音频时,我必须转换它总是返回两个音频是相同的。

  • 我正在尝试验证和检查两个XML文件之间的差异。 XML文件1: 我应该如何比较这两个文件包含相同的信息(不关心信息的生成顺序),有什么想法吗?

  • 问题内容: 我需要编写一个比较器,它采用类型A的对象A和类型B的对象B。这两个对象不是公共对象的扩展。它们的确不同,但是我需要通过其中的通用字段来比较这两个对象。我必须使用比较器接口,因为对象存储在Set中,并且在必须对CollectionUtils执行操作之后。我在Google上搜索了一下,发现了Comparator的解决方案,但只有相同的类型。 我试图朝这个方向实施思考,但是我不知道我是否在正

  • 我需要写一个比较器,取一个a类型的对象a和一个B类型的对象B。这两个对象不是一个公共对象的扩展。他们确实是不同的,但我需要比较这两个对象在它的共同领域。我必须使用比较器接口,因为对象存储在Set中,之后我必须使用CollectionUtils进行操作。我搜索了一点点,我用比较器找到了解决方案,但只有相同的类型。 TXS 附注:我在不同的集合中添加两个对象: 之后我会这样想:

  • 问题内容: 我想比较位于两个不同文件夹中的文件。我只希望比较两个不同文件夹中具有相同名称的文件。 我希望做的是比较一个软件的两个不同版本,并发现已更改了多少文件。 问题答案: 这将帮助您获取两个路径的文件: 您将需要添加自己的逻辑进行比较。资源