Coder Social home page Coder Social logo

emoji-parser's Introduction

emoji-parser

本项目仅用于个人自娱自乐。 您要是乐意拿去用,咱虽然不拦着,但不作任何承诺。

使用说明

构造解析器

EmojiTextParser parser = EmojiTextParser.buildDefaultParser();

字符串解析

EmojiTextParser#parse(String) 方法可以将字符串解析为一个 ParsedSequence 流。

parser.parse("有1️⃣说1️⃣,虽然Java是我的🍚工具,但我并不❤️它🤣👉🤡").forEach(System.out::println);

输出:

NonEmojiSequence[content=有]
EmojiSequence[chars=1️⃣, code=U+0031 U+FE0F U+20E3, name=keycap: 1, group=Symbols, subGroup=keycap]
NonEmojiSequence[content=说]
EmojiSequence[chars=1️⃣, code=U+0031 U+FE0F U+20E3, name=keycap: 1, group=Symbols, subGroup=keycap]
NonEmojiSequence[content=,虽然Java是我的]
EmojiSequence[chars=🍚, code=U+1F35A, name=cooked rice, group=Food & Drink, subGroup=food-asian]
NonEmojiSequence[content=工具,但我并不]
EmojiPresentation[prefix=EmojiSequence[chars=❤, code=U+2764, name=red heart, group=Smileys & Emotion, subGroup=heart]]
NonEmojiSequence[content=它]
EmojiSequence[chars=🤣, code=U+1F923, name=rolling on the floor laughing, group=Smileys & Emotion, subGroup=face-smiling]
EmojiSequence[chars=👉, code=U+1F449, name=backhand index pointing right, group=People & Body, subGroup=hand-single-finger]
EmojiSequence[chars=🤡, code=U+1F921, name=clown face, group=Smileys & Emotion, subGroup=face-costume]

这个方法会将原字符串中的每个 emoji 单独解析为一个 ParsedSequence 对象,而连续的非 emoji 字符则会被视作一个整体。

通过操作这个流,可以实现各类需求,EmojiTextParser 类的其它公有方法实际上都是以此为基础实现的。这里给出几个示例。

直接去除 emoji:

String emojiRemoved = parser.parse("有1️⃣说1️⃣,虽然Java是我的🍚工具,但我并不❤️它🤣👉🤡")
        .filter(sequence -> !sequence.getType().emoji)
        .map(ParsedSequence::getContent)
        .collect(Collectors.joining());
// 有说,虽然Java是我的工具,但我并不它
System.out.println(emojiRemoved);

自定义编码:

String encoded = parser.parse("有1️⃣说1️⃣,虽然Java是我的🍚工具,但我并不❤️它🤣👉🤡")
        .map(sequence -> {
            if (!sequence.getType().emoji) {
                return sequence.getContent();
            }
            return "[emoji:%s]".formatted(URLEncoder.encode(sequence.getContent(), StandardCharsets.UTF_8));
        })
        .collect(Collectors.joining());
// 有[emoji:1%EF%B8%8F%E2%83%A3]说[emoji:1%EF%B8%8F%E2%83%A3],虽然Java是我的[emoji:%F0%9F%8D%9A]工具,但我并不[emoji:%E2%9D%A4%EF%B8%8F]它[emoji:%F0%9F%A4%A3][emoji:%F0%9F%91%89][emoji:%F0%9F%A4%A1]
System.out.println(encoded);

长度计算

EmojiTextParser#getLength(String) 方法可以用于获取含 emoji 字符串的准确长度,每一个 emoji 会被算作一个字符。

// 8
System.out.println("乐了🤣👉🤡".length());
// 5
System.out.println(parser.getLength("乐了🤣👉🤡"));

字符替换

EmojiTextParser#replaceEmoji(String, Function<ParsedSequence, String>) 方法可以简便地将 emoji 转换成其它字符串。

// 你说你*呢,吃*去吧😀
System.out.println(parser.replaceEmoji("你说你🐎呢,吃💩去吧😅", emoji -> {
    String content = emoji.getContent();
    if ("😅".equals(content)) {
        return "😀";
    }
    return "*";
}));

待办事项

  • 完善 emoji 的属性解析,将分组等属性替换为枚举
  • 完善单元测试
  • 使用 tsv 文件替代 json 文件
  • 去掉不必要的依赖
  • 降低 jdk 版本到 java8
  • 完善 modifier、presentation 等特殊情况的处理
  • 兼容 emoji-java 的大部分 api
  • 支持解析行为配置

外部资源

emoji-parser's People

Contributors

yuanbug avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.