Python收款.计数器把JSON中的东西排除在外

2024-06-26 14:13:50 发布

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

我想在Facebook上创建一个在mymy gf之间频繁使用的单词的可视化。我直接从FB下载了JSON文件中的所有消息,我让计数器工作

但是:

  • 计数器还统计JSON中的元素名,如“sender_name”或是13数字的时间戳
  • JSON文件缺少UTF编码-我有像\u00c5\u0082a\u00c5\u0082a这样的字符串硬编码到单词中

如何排除诸如you, I, a, but等无意义的短词?你知道吗

对于第一个问题,我尝试创建一个要排除的单词词典,但我甚至不知道如何排除它们。此外,问题在于删除时间戳编号,因为它们不是常量。你知道吗

对于第二个问题,我尝试在word编辑器中打开文件并替换符号代码,但每次都会因为文件的大小(超过150万行)而崩溃。你知道吗

下面是我用来打印最常用单词的代码:

import re
import collections
import json

file = open('message.json', encoding="utf8")
a = file.read()

words = re.findall(r'\w+', a)

most_common = collections.Counter(map(str.lower, words)).most_common(50)
print(most_common)

JSON文件结构如下:

{
      "sender_name": "xxxxxx",
      "timestamp_ms": 1540327935616,
      "content": "Podobaj\u00c4\u0085 ci si\u00c4\u0099",
      "type": "Generic"
    },

Tags: 文件代码nameimportjsonmost编码my
2条回答

你试过把json当作字典来阅读并检查类型吗?你也可以在事后寻找不需要的单词并删除它们。你知道吗

import json
from collections import Counter

def get_words(string):
    return [word.lower() for word in string.split() if word.lower()]

def count_words(json_item):
    if isinstance(json_item, dict):
        for key, value in json_item.items():
            return count_words(key) + count_words(value)
    elif isinstance(value, str):
        return get_words(value)
    elif isinstance(value, list):
        return [word for string in value for word in count_words(string)]
    else:
        return []

with open('message.json', encoding="utf-8") as f:
    json_input = json.load(f)
counter = Counter(count_words(json_input))
result = { key: value for key, value in counter.items() if key not in UNWANTED_WORDS}

问题是您在整个文件中使用findall,请执行以下操作:

import re
import collections
import json


def words(s):
    return re.findall('\w+', s, re.UNICODE | re.IGNORECASE)

file = open('message.json', encoding="utf8")
data = json.load(file)

counts = collections.Counter((w.lower() for e in data for w in words(e.get('content', ''))))
most_common = counts.most_common(50)
print(most_common)

输出

[('siä', 1), ('ci', 1), ('podobajä', 1)]

输出用于具有以下内容的文件(JSON对象列表):

[{
      "sender_name": "xxxxxx",
      "timestamp_ms": 1540327935616,
      "content": "Podobaj\u00c4\u0085 ci si\u00c4\u0099",
      "type": "Generic"
}]

解释

使用json.load将文件的内容作为字典列表data加载,然后迭代字典的元素,并使用函数wordsCounter计算'content'字段的单词数

进一步

  1. 要删除I、a和but等词,请参见this

更新

给定文件的格式,您需要将行:data = json.load(file)更改为data = json.load(file)["messages"],用于以下内容:

{
  "participants":[],
  "messages": [
    {
      "sender_name": "xxxxxx",
      "timestamp_ms": 1540327935616,
      "content": "Podobaj\u00c4\u0085 ci si\u00c4\u0099",
      "type": "Generic"
    },
    {
      "sender_name": "aaa",
      "timestamp_ms": 1540329382942,
      "content": "aaa",
      "type": "Generic"
    },
    {
      "sender_name": "aaa",
      "timestamp_ms": 1540329262248,
      "content": "aaa",
      "type": "Generic"
    }
  ]
}

输出为:

[('aaa', 2), ('siä', 1), ('podobajä', 1), ('ci', 1)]

相关问题 更多 >