【问题标题】:Parsing combinatorial emojis解析组合表情符号
【发布时间】:2019-07-14 00:53:13
【问题描述】:

我有一个充满消息的数据库,我正在尝试查看每个用户最常用的表情符号。

我找到了this regex,它“深受lodash’s implementation of split.的启发”

let regex = new RegExp([
    '(?:[\\u2700-\\u27bf]|(?:\\ud83c[\\udde6-\\uddff]){2}|',
    '[\\ud800-\\udbff][\\udc00-\\udfff]|[\\u0023-\\u0039]\\ufe0f?\\u20e3|',
    '\\u3299|\\u3297|\\u303d|\\u3030|\\u24c2|\\ud83c[\\udd70-\\udd71]|',
    '\\ud83c[\\udd7e-\\udd7f]|\\ud83c\\udd8e|\\ud83c[\\udd91-\\udd9a]|',
    '\\ud83c[\\udde6-\\uddff]|[\\ud83c[\\ude01-\\ude02]|\\ud83c\\ude1a|',
    '\\ud83c\\ude2f|[\\ud83c[\\ude32-\\ude3a]|[\\ud83c[\\ude50-\\ude51]|',
    '\\u203c|\\u2049|[\\u25aa-\\u25ab]|\\u25b6|\\u25c0|[\\u25fb-\\u25fe]|',
    '\\u00a9|\\u00ae|\\u2122|\\u2139|\\ud83c\\udc04|[\\u2600-\\u26FF]|',
    '\\u2b05|\\u2b06|\\u2b07|\\u2b1b|\\u2b1c|\\u2b50|\\u2b55|\\u231a|',
    '\\u231b|\\u2328|\\u23cf|[\\u23e9-\\u23f3]|[\\u23f8-\\u23fa]|\\ud83c\\udccf|\\u2934|\\u2935|[\\u2190-\\u21ff])'
].join(''), 'g')

所以我使用这个查询提取消息:

alltext_query = "select sender, mtext from Messages;"

并像这样解析每条消息:

pool.query(alltext_query, (err, result) => {
    if(err) {
        console.error(err)
    } else {
        result.rows.forEach(row => {
            console.log(row['mtext'].match(regex))
        })
    }
})

这给了我这样的输出:

[ '????', '????', '????' ]
[ '????' ]
[ '????', '????', '????', '????', '????', '????' ]
[ '????', '????' ]
[ '????', '????' ]
null # if there's no emoji in the message

这非常适合查找最常用的表情符号。但是,对于具有肤色和/或性别的表情符号,输出会变成这样:

[ '????', '????', '????', '♀' ]
[ '????', '♂' ]
[ '????', '????', '????', '????', '????', '????' ]

如您所见,肤色和性别已经分开,这是不可取的。

对于以上三个数组,我期望的输出是这样的:

[ '????', '????????‍♀️' ]
[ '????‍♂️' ]
[ '????????', '????????', '????????' ]

我该如何解决这个问题?

【问题讨论】:

  • @Emma 不只是 ????????‍♀️ 之类的。很多表情都是这样的;他们中有些人有性别,有些人有肤色,有些人两者兼有。手动检查所有这些对我来说似乎不合适。我想可以修改正则表达式来处理这个问题。

标签: javascript node.js regex emoji


【解决方案1】:

有一个名为Emoji Test Patterns 的Node 模块,它基于emoji-test.txt 数据文件构建表情符号正则表达式模式。您可以尝试一下,看看它是否满足您的需求。

这样的事情可能会奏效:

const emojiTestPatterns = require ('emoji-test-patterns');
let regex = new RegExp (emojiTestPatterns["Emoji_Test_All"], 'gu');
// [...]
console.log(row['mtext'].match(regex));

【讨论】:

    猜你喜欢
    • 2015-12-22
    • 2021-11-04
    • 2017-08-24
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多