PHP, Elasticsearch, Symfony, Emoji
…Poney, Guinness and animated gif.
I propose the following character sequence for joke markers:
:-)
In 1998, Japanese operators introduced 172 12x12 pixels pictograms for messaging!
๐ข ๐ค ๐ฅ ๐ฏ ๐ ๐ ๐ป ๐
๐ ๐ฌ ๐ฉ ๐ฆ ๐ ๐ ๐ฆ ๐ธ
Each vendor has to design it's own image (and it's hard):
๐ท synonym token_filter can add Emoji annotations
PUT demo
{
"settings": {
"analysis": {
"filter": {
"english_emoji": {
"type": "synonym",
"synonyms": ["๐ => ๐, cheese, pizza, slice"]
},
}
}
}
}
GET _analyze
{
"analyzer": "standard",
"text": "Pizza"
}
{
"tokens": [
{
"token": "pizza",
"start_offset": 0,
"end_offset": 5,
"type": "<ALPHANUM>",
"position": 0
}
]
}
๐
GET _analyze
{
"analyzer": "standard",
"text": "๐"
}
{
"tokens": []
}
๐
๐
GET _analyze
{
"tokenizer": "whitespace",
"text": "Pineapple ๐!"
}
{
"tokens": [
{
"token": "Pineapple",
"type": "word"
},
{
"token": "๐!",
"type": "word"
}
]
}
"punctuation_filter": {
"type": "pattern_replace",
"pattern": "\\p{Punct}",
"replace": ""
},
"remove_empty_filter": {
"type": "length",
"min": 1
}
\\p{Punct}
catch punctuation:
!"#$%&'()*+,-./:;<=>?@[]^_`{|}~
"punctuation_filter": {
"type": "pattern_replace",
"pattern": "\\p{Punct}|\\uFE0E|\\uFE0F|\\uD83C\\uDFFB|\\uD83C\\uDFFC|\\uD83C\\uDFFD|\\uD83C\\uDFFE|\\uD83C\\uDFFF",
"replace": ""
}
"zwj_char_filter": {
"type": "mapping",
"mappings": [
"\\u200D=> "
]
}
{
"settings": {
"analysis": {
"analyzer": {
"english_with_emoji": {
"char_filter": "zwj_char_filter",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"punctuation_filter",
"remove_empty_filter",
"english_emoji"
]
}
},
"char_filter": {
"zwj_char_filter": {
"type": "mapping",
"mappings": [
"\\u200D=>"
]
}
},
"filter": {
"remove_empty_filter": {
"type": "length",
"min": 1
},
"english_emoji": {
"type": "synonym",
"synonyms": ["๐ => ๐, cheese, pizza, slice"]
},
"punctuation_filter": {
"type": "pattern_replace",
"pattern": """\p{Punct}|\uFE0E|\uFE0F|\uD83C\uDFFB|\uD83C\uDFFC|\uD83C\uDFFD|\uD83C\uDFFE|\uD83C\uDFFF""",
"replace": ""
}
}
}
}
}
GET /demo/_analyze
{
"analyzer": "english_with_emoji",
"text": "A ๐ please!"
}
a ๐ cheese pizza slice please
One emoji done, 2776 to go!
๐ฑ
๐ป => ๐ป, bear, bear face, face
๐จ => ๐จ, bear, koala
๐ผ => ๐ผ, face, panda, panda face
๐พ => ๐พ, feet, paw, paw prints, print
๐ฆ => ๐ฆ, bird, turkey
๐ => ๐, bird, chicken
๐ => ๐, bird, rooster
๐ฃ => ๐ฃ, baby, bird, chick, hatching, hatching chick
๐ค => ๐ค, baby, baby chick, bird, chick
๐ฅ => ๐ฅ, baby, bird, chick, front-facing baby chick
๐ด => ๐ด, palm, palm tree, tree
๐ต => ๐ต, cactus, plant
๐พ => ๐พ, ear, grain, rice, sheaf of rice
๐ฟ => ๐ฟ, herb, leaf
โ => โ, plant, shamrock
๐ => ๐, 4, clover, four, four leaf clover, leaf
๐ => ๐, falling, leaf, maple, maple leaf
๐ => ๐, fallen leaf, falling, leaf
๐ => ๐, blow, flutter, leaf, leaf fluttering in wind, wind
๐ => ๐, fruit, grape, grapes
GET /demo/_analyze
{
"tokenizer": "icu_tokenizer",
"text": "A ๐ please!"
}
A please
ICUTokenizer.java, status 0 means ignored:
// find the next set of boundaries,
// skipping over non-tokens (rule status 0)
int end = breaker.next();
while (end != BreakIterator.DONE
&& breaker.getRuleStatus() == 0) {
start = end;
end = breaker.next();
}
Unicode use SVN ๐ฑ
!!chain;
$CR = [\p{Word_Break = CR}];
$LF = [\p{Word_Break = LF}];
$Newline = [\p{Word_Break = Newline} ];
$Extend = [\p{Word_Break = Extend}];
$ZWJ = [\p{Word_Break = ZWJ}];
$Regional_Indicator = [\p{Word_Break = Regional_Indicator}];
$Format = [\p{Word_Break = Format}];
$Katakana = [\p{Word_Break = Katakana}];
$Hebrew_Letter = [\p{Word_Break = Hebrew_Letter}];
$ALetter = [\p{Word_Break = ALetter}];
$Single_Quote = [\p{Word_Break = Single_Quote}];
$Double_Quote = [\p{Word_Break = Double_Quote}];
$MidNumLet = [\p{Word_Break = MidNumLet}];
$MidLetter = [\p{Word_Break = MidLetter}];
$MidNum = [\p{Word_Break = MidNum}];
$Numeric = [\p{Word_Break = Numeric}];
$ExtendNumLet = [\p{Word_Break = ExtendNumLet}];
$E_Base = [\p{Word_Break = EB}\U0001F3C2\U0001F3C7\U0001F3CC\U0001F46A-\U0001F46D\U0001F46F\U0001F574\U0001F6CC];
$E_Modifier = [\p{Word_Break = EM}];
$Extended_Pict = [\U0001F774-\U0001F77F\u2700-\u2701\u2703-\u2704\u270E\u2710-\u2711\u2765-\u2767\U0001F030-\U0001F093\U0001F094-\U0001F09F\U0001F10D-\U0001F10F\U0001F12F\U0001F16C-\U0001F16F\U0001F1AD-\U0001F1E5\U0001F203-\U0001F20F\U0001F23C-\U0001F23F\U0001F249-\U0001F24F\U0001F252-\U0001F2FF\U0001F7D5-\U0001F7FF\U0001F000-\U0001F003\U0001F005-\U0001F02B\U0001F02C-\U0001F02F\U0001F322-\U0001F323\U0001F394-\U0001F395\U0001F398\U0001F39C-\U0001F39D\U0001F3F1-\U0001F3F2\U0001F3F6\U0001F4FE\U0001F53E-\U0001F548\U0001F54F\U0001F568-\U0001F56E\U0001F571-\U0001F572\U0001F57B-\U0001F586\U0001F588-\U0001F589\U0001F58E-\U0001F58F\U0001F591-\U0001F594\U0001F597-\U0001F5A3\U0001F5A6-\U0001F5A7\U0001F5A9-\U0001F5B0\U0001F5B3-\U0001F5BB\U0001F5BD-\U0001F5C1\U0001F5C5-\U0001F5D0\U0001F5D4-\U0001F5DB\U0001F5DF-\U0001F5E0\U0001F5E2\U0001F5E4-\U0001F5E7\U0001F5E9-\U0001F5EE\U0001F5F0-\U0001F5F2\U0001F5F4-\U0001F5F9\u2605\u2607-\u260D\u260F-\u2610\u2612\u2616-\u2617\u2619-\u261C\u261E-\u261F\u2621\u2624-\u2625\u2627-\u2629\u262B-\u262D\u2630-\u2637\u263B-\u2647\u2654-\u265F\u2661-\u2662\u2664\u2667\u2669-\u267A\u267C-\u267E\u2680-\u2691\u2695\u2698\u269A\u269D-\u269F\u26A2-\u26A9\u26AC-\u26AF\u26B2-\u26BC\u26BF-\u26C3\u26C6-\u26C7\u26C9-\u26CD\u26D0\u26D2\u26D5-\u26E8\u26EB-\u26EF\u26F6\u26FB-\u26FC\u26FE-\u26FF\u2388\U0001FA00-\U0001FFFD\U0001F0A0-\U0001F0AE\U0001F0B1-\U0001F0BF\U0001F0C1-\U0001F0CF\U0001F0D1-\U0001F0F5\U0001F0AF-\U0001F0B0\U0001F0C0\U0001F0D0\U0001F0F6-\U0001F0FF\U0001F80C-\U0001F80F\U0001F848-\U0001F84F\U0001F85A-\U0001F85F\U0001F888-\U0001F88F\U0001F8AE-\U0001F8FF\U0001F900-\U0001F90F\U0001F91F\U0001F928-\U0001F92F\U0001F931-\U0001F932\U0001F93F\U0001F94C-\U0001F94F\U0001F95F-\U0001F97F\U0001F992-\U0001F9BF\U0001F9C1-\U0001F9FF\U0001F6C6-\U0001F6CA\U0001F6E6-\U0001F6E8\U0001F6EA\U0001F6F1-\U0001F6F2\U0001F6D3-\U0001F6DF\U0001F6ED-\U0001F6EF\U0001F6F7-\U0001F6FF];
$EBG = [\p{Word_Break = EBG}];
$EmojiNRK = [[\p{Emoji}] - [\p{Word_Break = Regional_Indicator}\u002a\u00230-9ยฉยฎโขใฐใฝ]];
$Han = [:Han:];
$Hiragana = [:Hiragana:];
$Control = [\p{Grapheme_Cluster_Break = Control}];
$HangulSyllable = [\uac00-\ud7a3];
$ComplexContext = [:LineBreak = Complex_Context:];
$KanaKanji = [$Han $Hiragana $Katakana];
$dictionaryCJK = [$KanaKanji $HangulSyllable];
$dictionary = [$ComplexContext $dictionaryCJK];
$ALetterPlus = [$ALetter-$dictionaryCJK [$ComplexContext-$Extend-$Control]];
$KatakanaEx = $Katakana ($Extend | $Format | $ZWJ)*;
$Hebrew_LetterEx = $Hebrew_Letter ($Extend | $Format | $ZWJ)*;
$ALetterEx = $ALetterPlus ($Extend | $Format | $ZWJ)*;
$Single_QuoteEx = $Single_Quote ($Extend | $Format | $ZWJ)*;
$Double_QuoteEx = $Double_Quote ($Extend | $Format | $ZWJ)*;
$MidNumLetEx = $MidNumLet ($Extend | $Format | $ZWJ)*;
$MidLetterEx = $MidLetter ($Extend | $Format | $ZWJ)*;
$MidNumEx = $MidNum ($Extend | $Format | $ZWJ)*;
$NumericEx = $Numeric ($Extend | $Format | $ZWJ)*;
$ExtendNumLetEx = $ExtendNumLet ($Extend | $Format | $ZWJ)*;
$Regional_IndicatorEx = $Regional_Indicator ($Extend | $Format | $ZWJ)*;
$Ideographic = [\p{Ideographic}];
$HiraganaEx = $Hiragana ($Extend | $Format | $ZWJ)*;
$IdeographicEx = $Ideographic ($Extend | $Format | $ZWJ)*;
!!forward;
$CR $LF;
$ZWJ ($Extended_Pict | $EmojiNRK);
[^$CR $LF $Newline]? ($Extend | $Format | $ZWJ)+;
$NumericEx {100};
$ALetterEx {200};
$HangulSyllable {200};
$Hebrew_LetterEx{200};
$KatakanaEx {400};
$HiraganaEx {400};
$IdeographicEx {400};
$E_Base ($Extend | $Format | $ZWJ)*;
$E_Modifier ($Extend | $Format | $ZWJ)*;
$Extended_Pict ($Extend | $Format | $ZWJ)*;
($ALetterEx | $Hebrew_LetterEx) ($ALetterEx | $Hebrew_LetterEx) {200};
($ALetterEx | $Hebrew_LetterEx) ($MidLetterEx | $MidNumLetEx | $Single_QuoteEx) ($ALetterEx | $Hebrew_LetterEx) {200};
$Hebrew_LetterEx $Single_QuoteEx {200};
$Hebrew_LetterEx $Double_QuoteEx $Hebrew_LetterEx {200};
$NumericEx $NumericEx {100};
($ALetterEx | $Hebrew_LetterEx) $NumericEx {200};
$NumericEx ($ALetterEx | $Hebrew_LetterEx) {200};
$NumericEx ($MidNumEx | $MidNumLetEx | $Single_QuoteEx) $NumericEx {100};
$KatakanaEx $KatakanaEx {400};
$ALetterEx $ExtendNumLetEx {200};
$Hebrew_LetterEx $ExtendNumLetEx {200};
$NumericEx $ExtendNumLetEx {100};
$KatakanaEx $ExtendNumLetEx {400};
$ExtendNumLetEx $ExtendNumLetEx {200};
$ExtendNumLetEx $ALetterEx {200};
$ExtendNumLetEx $Hebrew_Letter {200};
$ExtendNumLetEx $NumericEx {100};
$ExtendNumLetEx $KatakanaEx {400};
($E_Base | $EBG) ($Format | $Extend | $ZWJ)* $E_Modifier;
^$Regional_IndicatorEx $Regional_IndicatorEx;
$HangulSyllable $HangulSyllable {200};
$KanaKanji $KanaKanji {400};
!!reverse;
$BackHebrew_LetterEx = ($Format | $Extend | $ZWJ)* $Hebrew_Letter;
$BackALetterEx = ($Format | $Extend | $ZWJ)* $ALetterPlus;
$BackSingle_QuoteEx = ($Format | $Extend | $ZWJ)* $Single_Quote;
$BackDouble_QuoteEx = ($Format | $Extend | $ZWJ)* $Double_Quote;
$BackMidNumLetEx = ($Format | $Extend | $ZWJ)* $MidNumLet;
$BackNumericEx = ($Format | $Extend | $ZWJ)* $Numeric;
$BackMidNumEx = ($Format | $Extend | $ZWJ)* $MidNum;
$BackMidLetterEx = ($Format | $Extend | $ZWJ)* $MidLetter;
$BackKatakanaEx = ($Format | $Extend | $ZWJ)* $Katakana;
$BackHiraganaEx = ($Format | $Extend | $ZWJ)* $Hiragana;
$BackExtendNumLetEx = ($Format | $Extend | $ZWJ)* $ExtendNumLet;
$BackRegional_IndicatorEx = ($Format | $Extend | $ZWJ)* $Regional_Indicator;
$LF $CR;
($Extended_Pict | $EmojiNRK) $ZWJ;
($Format | $Extend | $ZWJ)* [^$CR $LF $Newline]?;
($BackALetterEx | $BackHebrew_LetterEx) ($BackALetterEx | $BackHebrew_LetterEx);
($BackALetterEx | $BackHebrew_LetterEx) ($BackMidLetterEx | $BackMidNumLetEx | $BackSingle_QuoteEx) ($BackALetterEx | $BackHebrew_LetterEx);
$BackSingle_QuoteEx $BackHebrew_LetterEx;
$BackHebrew_LetterEx $BackDouble_QuoteEx $BackHebrew_LetterEx;
$BackNumericEx $BackNumericEx;
$BackNumericEx ($BackALetterEx | $BackHebrew_LetterEx);
($BackALetterEx | $BackHebrew_LetterEx) $BackNumericEx;
$BackNumericEx ($BackMidNumEx | $BackMidNumLetEx | $BackSingle_QuoteEx) $BackNumericEx;
$BackKatakanaEx $BackKatakanaEx;
$BackExtendNumLetEx ($BackALetterEx | $BackHebrew_LetterEx | $BackNumericEx | $BackKatakanaEx | $BackExtendNumLetEx);
($BackALetterEx | $BackHebrew_LetterEx | $BackNumericEx | $BackKatakanaEx) $BackExtendNumLetEx;
$HangulSyllable $HangulSyllable;
$KanaKanji $KanaKanji;
$E_Modifier ($Format | $Extend | $ZWJ)* ($E_Base | $EBG);
^$BackRegional_IndicatorEx / ($BackRegional_IndicatorEx $BackRegional_IndicatorEx)* ($Format | $Extend | $ZWJ)* [[^$Regional_Indicator $Format $Extend $ZWJ] {eof}];
^$BackRegional_IndicatorEx $BackRegional_IndicatorEx / ($BackRegional_IndicatorEx $BackRegional_IndicatorEx)* ($Format | $Extend | $ZWJ)* [[^$Regional_Indicator $Format $Extend $ZWJ] {eof}];
($Extended_Pict | $EmojiNRK) $ZWJ $BackRegional_IndicatorEx / ($BackRegional_IndicatorEx $BackRegional_IndicatorEx)* ($Format | $Extend | $ZWJ)* [[^$Regional_Indicator $Format $Extend $ZWJ] {eof}];
($Extended_Pict | $EmojiNRK) $ZWJ $BackRegional_IndicatorEx $BackRegional_IndicatorEx / ($BackRegional_IndicatorEx $BackRegional_IndicatorEx)* ($Format | $Extend | $ZWJ)* [[^$Regional_Indicator $Format $Extend $ZWJ] {eof}];
!!safe_reverse;
($Extend | $Format | $ZWJ)+ .?;
($MidLetter | $MidNumLet | $Single_Quote) ($BackALetterEx | $BackHebrew_LetterEx);
$Double_Quote $BackHebrew_LetterEx;
($MidNum | $MidNumLet | $Single_Quote) $BackNumericEx;
$BackRegional_IndicatorEx*;
$dictionary $dictionary;
!!safe_forward;
($Extend | $Format | $ZWJ)+ .?;
($MidLetterEx | $MidNumLetEx | $Single_QuoteEx) ($ALetterEx | $Hebrew_LetterEx);
$Double_QuoteEx $Hebrew_LetterEx;
($MidNumEx | $MidNumLetEx | $Single_QuoteEx) $NumericEx;
$Regional_IndicatorEx*;
$dictionary $dictionary;
!!forward;
$CR $LF;
$ZWJ ($Extended_Pict | $EmojiNRK);
$NumericEx {100};
$ALetterEx {200};
$E_Base ($Extend | $Format | $ZWJ)*;
$E_Modifier ($Extend | $Format | $ZWJ)*;
There is no "status" for Emoji
"tokenizer" : {
"my_emoji_tokenizer" : {
"type" : "icu_tokenizer",
"rule_files" : "Latn:Emoji.rbbi"
}
}
{
"tokenizer": {
"icu_emoji_tokenizer": {
"type": "icu_tokenizer",
"rule_files": "Latn:Emoji.rbbi,Zyyy:Emoji.rbbi"
}
}
}
Emoji.rbbi will be used for
undetermined script (Zyyy) and Latin (Latn).
$E_Base ($Extend | $Format | $ZWJ)* {200};
$E_Modifier ($Extend | $Format | $ZWJ)* {200};
$Extended_Pict ($Extend | $Format | $ZWJ)* {200};
Based on the Default from Lucene.
โ Will fallback on the default for all others "scripts"! ๐
gradle idea
๐ for IntelliJ
gradle tasks
gradle clean check
to build
gradle clean check assemble
to zip
The final zip for the plugin is done:
analysis-emoji-5.0.0.zip
โโโ elasticsearch
โโโ analysis-emoji-5.0.0.jar
โโโ icu4j-58.1.jar
โโโ lucene-analyzers-icu-6.2.0.jar
โโโ plugin-descriptor.properties
description=Add support for emoji in Elasticsearch
version=5.0.0
name=analysis-emoji
classname=org.elasticsearch.plugin.analysis.emoji.EmojiPlugin
java.version=1.8
elasticsearch.version=5.0.0
I use TokenizerFactory, and extend the DefaultICUTokenizerConfig:
defaultRules = defaultRules.replace(
"!!forward;",
"!!forward;\n$EmojiNRK {200};"
);
defaultRules = defaultRules.replace(
"| $ZWJ)*;",
"| $ZWJ)* {200};"
);
"analyzer": {
"english_with_emoji": {
"tokenizer": "emoji_tokenizer",
"filter": [
"lowercase",
"english_emoji"
]
}
}
GET /en-emoji/_analyze
{
"analyzer": "english_with_emoji",
"text": "I live in ๐ซ๐ท and I'm ๐ฉโ๐"
}
i live in ๐ซ๐ท france and i'm astronaut rocket woman
Because ๐ฉ and ZWJ and ๐ is
If we still have time ๐
:-) :D :-( ^_^ ;-) <3
Just map them to their emoji representation before indexation!
:P => ๐
8-) => ๐
:s => ๐
:| => ๐
O:-D => ๐
</3 => ๐
:'-| => ๐ข
"char_filter": {
"emoticons_char_filter": {
"type": "mapping",
"mappings_path": "analysis/emoticons.txt"
}
},
The file is here: https://github.com/jolicode/emoji-search/blob/master/emoticons.txt
"analyzer": {
"english_with_emoji": {
"char_filter": ["emoticons_char_filter"],
"tokenizer": "emoji_tokenizer",
"filter": [
"lowercase",
"english_emoji"
]
}
}
GET /en-emoji/_analyze
{
"analyzer": "english_with_emoji",
"text": "Hi mom :)"
}
hi mom ๐ face mouth open smile