字典值计数

Dictionary value Counting

美好的一天, 我正在处理一个项目并从找到的实体中创建一个 dic。

也许有人可以帮助我。我很乐意了解更多相关信息。 我想到了使用 counter.

的计数可能性

再见!!

如果我理解正确的话,你可以通过对你的代码做一些补充来实现这个

您所要做的就是在 perlistloclist 上使用 Counter,并将结果存储在字典中。

...

final_dict = {}  # stores the desired final output in a singe dict
for filepath in files:

    with open(filepath, 'r', encoding='UTF8') as file_to_read:
        some_text = file_to_read.read()
        base_name = os.path.basename(filepath)
        print(base_name)
        doc = nlp(some_text)
        perlist=[]
        loclist=[]
        for ent in doc.ents:
             if ent.label_ == "PER":
                perlist.append(str(ent))
             elif ent.label_ == "LOC":
                loclist.append(str(ent))

        # Count the number of PER/LOC entities and store in final_dict
        final_list = []  # {"1.txt": final_list}

        # Count PER entities
        c = Counter(perlist)
        for p, count in c.most_common():
            final_list.append({
                'name': p,
                'type': 'PER',
                'frequency': count
            })

        # Count LOC entities
        c = Counter(loclist)
        for l, count in c.most_common():
            final_list.append({
                'name': l,
                'type': 'LOC',
                'frequency': count
            })

        # store list of results in final_dict.
        # eg. final_dict['1.txt'] = [{'name': 'Englishmen', 'type': 'PER', 'frequency': 1}, ...]
        final_dict[base_name] = final_list

print('Final result', final_dict)