【问题标题】:convert Java regex OR operator to Scala Regex将 Java 正则表达式 OR 运算符转换为 Scala 正则表达式
【发布时间】:2014-08-24 15:33:54
【问题描述】:

我正在使用 Twitter 做一个项目,其中一部分是取出推文中的所有表情符号,这样它就不会使解析器出错。我看了一下 Carnegie Mellon 的 Ark Tweet NLP,它非常棒,他们有这个非常好的 Java 正则表达式模式来检测表情符号!

但是,我对 Java 的正则表达式语法并不十分熟悉(我熟悉基本的)

https://github.com/brendano/ark-tweet-nlp/blob/master/src/cmu/arktweetnlp/Twokenize.java

我需要转换为 Scala 的代码如下所示:

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) + "(?:<|&lt;)?",


        //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
);

OR 运算符有点混乱。

那么谁能告诉我如何进行转换?同样在转换之后,我需要做的就是快速将推文分成单词并查看word.contains(emoticon) 对吗?谢谢!


上面的问题似乎很愚蠢。但是,还有最后一点我不知道的任务:

我要从我的句子中去掉那些表情符号。如果我只是将我的句子按空格分成单词并为(word &lt;- words if !word.contains(regexpattern)) 做,它会起作用吗?

【问题讨论】:

  • 没有OR 运算符。它是Twokenize 类中的静态方法。
  • public static String emoticon 更改为val emoticon: String,这可能是Scala 代码。 Scala 使用与 Java 相同的正则表达式引擎,也可以使用 arktweetnlp 库。

标签: regex scala


【解决方案1】:

你可以使用这个功能:

def OR(patterns : String*) = patterns.map{p => s"(?:$p)"}.mkString("|")

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多