如何将表情符号 unicode 转换为表情符号?

How to convert emoji unicode to emoji?

我有一个 .json 文件,其中包含超过 70,000 条推文,每条推文都包含表情符号。但是,我不确定如何将 Unicode 转换为实际的表情符号,以便将其用于情感分析。

这是我的 .json 文件中 5 条推文的样本:

{"text":"The morning is going so fast Part 2 of #DiscoveryDay is in full swing \ud83d\ude01\n\nGreat Atmosphere in the room \n\n#BIGSocial\u2026 https:\/\/t.co\/P08qBoH6tv"}
{"text":"Double kill! #XiuKai lives! I died. \ud83d\ude0c https:\/\/t.co\/QCyk3r2JCb"}
{"text":"ALLTY \ud83d\udc94"}
{"text":"Shouldn\u2019t be normal for a 24 year old to be this tiered \ud83d\udca4"}
{"text":"@TheNames_BrieX Trust me! \ud83d\udcaf"}

现在,我如何将所有推文的 unicode 转换为实际的表情符号?例如,如何将 \ud83d\ude0c 转换为实际的表情符号?

可以使用什么方法将unicode转换成实际的表情符号?

表情符号是 unicode 的一个子集。因此,没有必要或不可能从 unicode 转换为表情符号。只需将数组更改为

var data = ["\u{1F642}", "\u{1F603}"]

如果您输入的是十六进制数,您可以使用

String.fromCodePoint(parseInt ("1F929", 16))

在HTML中你也可以使用HTML十六进制实体

"&#x" + "1F618" + ";"

\ud83d\udcaf 这样的字符串是由于处理不当造成的,可以通过 data['text'].encode('utf-16', 'surrogatepass').decode('utf-16') 修复。 .

如果您尝试按规则进行情感分析,上面的代码可以在您的终端中显示实际的表情符号图标,您可以为其构建标签映射,无需转换原始文本。

如果您正在尝试基于统计或深度学习模型的情感分析,它们可以通过统计特征或监督学习来捕获语义信息,并且这些表情符号标记可能会自动识别为重要特征。

如果这是您实际的 JSON 文件内容:

{"text":"The morning is going so fast Part 2 of #DiscoveryDay is in full swing \ud83d\ude01\n\nGreat Atmosphere in the room \n\n#BIGSocial\u2026 https:\/\/xxx\/P08qBoH6tv"}
{"text":"Double kill! #XiuKai lives! I died. \ud83d\ude0c https:\/\/xxx\/QCyk3r2JCb"}
{"text":"ALLTY \ud83d\udc94"}
{"text":"Shouldn\u2019t be normal for a 24 year old to be this tiered \ud83d\udca4"}
{"text":"@TheNames_BrieX Trust me! \ud83d\udcaf"}

那就是JSON Lines格式,其中每一行都是一个完整的JSON结构,而不是一个有效的JSON文件。

像这样一次读一行:

import json
with open('test.json') as f:
    for line in f:
        print(json.loads(line))

输出:

{'text': 'The morning is going so fast Part 2 of #DiscoveryDay is in full swing \n\nGreat Atmosphere in the room \n\n#BIGSocial… https://xxx/P08qBoH6tv'}
{'text': 'Double kill! #XiuKai lives! I died.  https://xxx/QCyk3r2JCb'}
{'text': 'ALLTY '}
{'text': 'Shouldn’t be normal for a 24 year old to be this tiered '}
{'text': '@TheNames_BrieX Trust me! '}

请注意,我必须更改原始网址中的小网址,因为 SO 不允许包含它们的内容。

如果正如您所说,那只是 JSON 行的示例,并且它是一个完整的、正确的 JSON 文件,那么只需使用 json.load 阅读它:

import json
with open('test.json') as f:
    print(json.load(f))