使用Stanford CoreNLP提取电子邮件地址和电话号码

时间:2015-11-03 18:41:05

标签: nlp stanford-nlp

我一直在寻找使用Stanford CoreNLP(RegexNERAnnotator)从文本中提取电子邮件地址,电话号码等的解决方案。任何人都可以提供任何例子吗?

更新时间:04/11/2015: 实际上我应该问一下Stanford RegexNERAnnotator是否可以支持Java正则表达式。

示例用法:

       final String EMAIL_PATTERN = 
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

       List<CoreLabel> tokens = ...;
       TokenSequencePattern pattern = TokenSequencePattern.compile(EMAIL_PATTERN);
       TokenSequenceMatcher matcher = pattern.getMatcher(tokens);

       while (matcher.find()) {
         String matchedString = matcher.group();
         List<CoreMap> matchedTokens = matcher.groupNodes();
         ...
       }

它似乎不支持Java正则表达式:

Exception in thread "main" edu.stanford.nlp.ling.tokensregex.parser.TokenMgrError: Lexical error at line 1, column 1.  Encountered: "^" (94), after : ""
    at edu.stanford.nlp.ling.tokensregex.parser.TokenSequenceParserTokenManager.getNextToken(TokenSequenceParserTokenManager.java:1029)
    at edu.stanford.nlp.ling.tokensregex.parser.TokenSequenceParser.jj_ntk(TokenSequenceParser.java:3228)
    at edu.stanford.nlp.ling.tokensregex.parser.TokenSequenceParser.SeqRegexBasic(TokenSequenceParser.java:784)
    at edu.stanford.nlp.ling.tokensregex.parser.TokenSequenceParser.SeqRegexDisjConj(TokenSequenceParser.java:973)
    at edu.stanford.nlp.ling.tokensregex.parser.TokenSequenceParser.SeqRegex(TokenSequenceParser.java:743)
    at edu.stanford.nlp.ling.tokensregex.parser.TokenSequenceParser.SeqRegexWithAction(TokenSequenceParser.java:1596)
    at edu.stanford.nlp.ling.tokensregex.parser.TokenSequenceParser.parseSequenceWithAction(TokenSequenceParser.java:37)
    at edu.stanford.nlp.ling.tokensregex.TokenSequencePattern.compile(TokenSequencePattern.java:186)
    at edu.stanford.nlp.ling.tokensregex.TokenSequencePattern.compile(TokenSequencePattern.java:169)

1 个答案:

答案 0 :(得分:5)

StackOverflow不是教程甚至示例的地方。但是,即使不需要RegexNER,它似乎也可以正常使用正则表达式。从谷歌搜索中,请参阅Using a regular expression to validate an email address以获取电子邮件。电话号码应该像以下一样简单,但直截了当的正则表达式:

(\+[0-9]{1,2}(\s*|-)?)?(\(?[0-9]{3}\)?)?(\s*|-)[0-9]{3}(\s*|-)[0-9]{4}

我的猜测是,Stanford Tokenizer的标记化会使这更难,也不容易。