Regex 将Java正则表达式或运算符转换为Scala正则表达式
我正在用Twitter做一个项目,其中一个部分是去掉tweet中的所有表情符号,这样就不会让解析器绊倒。我看了卡内基梅隆大学的Ark Tweet NLP,这非常令人惊讶,他们有一个非常好的Java正则表达式模式来检测表情符号 然而,我并不完全熟悉Java的正则表达式语法(我熟悉基本语法) 我需要转换为Scala的代码如下所示:Regex 将Java正则表达式或运算符转换为Scala正则表达式,regex,scala,Regex,Scala,我正在用Twitter做一个项目,其中一个部分是去掉tweet中的所有表情符号,这样就不会让解析器绊倒。我看了卡内基梅隆大学的Ark Tweet NLP,这非常令人惊讶,他们有一个非常好的Java正则表达式模式来检测表情符号 然而,我并不完全熟悉Java的正则表达式语法(我熟悉基本语法) 我需要转换为Scala的代码如下所示: public static String emoticon = OR( // Standard version :) :( :] :D :P
public static String emoticon = OR(
// Standard version :) :( :] :D :P
"(?:>|>)?" + OR(normalEyes, wink) + OR(noseArea,"[Oo]") +
OR(tongue+"(?=\\W|$|RT|rt|Rt)", otherMouths+"(?=\\W|$|RT|rt|Rt)", sadMouths, happyMouths),
// reversed version (: D: use positive lookbehind to remove "(word):"
// because eyes on the right side is more ambiguous with the standard usage of : ;
"(?<=(?: |^))" + OR(sadMouths,happyMouths,otherMouths) + noseArea + OR(normalEyes, wink) + "(?:<|<)?",
//inspired by http://en.wikipedia.org/wiki/User:Scapler/emoticons#East_Asian_style
eastEmote.replaceFirst("2", "1"), basicface
// iOS 'emoji' characters (some smileys, some symbols) [\ue001-\uebbb]
// TODO should try a big precompiled lexicon from Wikipedia, Dan Ramage told me (BTO) he does this
);
公共静态字符串表情符号=或(
//标准版本:):(:]:D:P
“(?:>)?”+或(正常的眼睛,眨眼)+或(鼻子区域,[Oo]”+
或(舌头+“(?=\\W$$| RT | RT | RT)”,其他嘴+“(?=\\W$| RT | RT | RT)”,悲伤嘴,快乐嘴),
//反向版本(:D:使用正向查找删除“(word)”
//因为右眼的标准用法是:;
“(?您可以使用此功能:
def OR(patterns : String*) = patterns.map{p => s"(?:$p)"}.mkString("|")
没有或运算符。它是Twokenize
类中的静态方法。将公共静态字符串表情更改为val表情:String
,这可能是Scala代码。Scala使用与Java相同的正则表达式引擎,也可以使用arktweetnlp库。