【问题标题】:How to extract the words from the tree structure using Regex pattern如何使用正则表达式从树结构中提取单词
【发布时间】:2013-02-07 13:37:03
【问题描述】:

我需要从树结构中提取名词短语,但我无法使用正则表达式模式从树结构中提取名词。

这是树形结构

(TOP (ADJP (JJ Welcome) (PP (TO to) (NP (NNP Regular) (NNP Expression) (NNS learnings)))))

我需要提取所有 pos 标签的单词,例如 NP、NNP、NNS 等;即;我需要使用正则表达式模式获取正则、表达式、学习等单词。

谁能帮我弄到这个。

【问题讨论】:

  • 我建议将其解析为内存中的实际树结构,然后找到您想要的。
  • 你会为NP提取什么?
  • 不是正则表达式的工作(至少 Java 正则表达式不能支持这一点)。您可以遍历字符串并构建一棵树。
  • 正则表达式会帮助我获得我需要的单词还是我需要切换到其他东西?

标签: java regex regular-language


【解决方案1】:

不确定这是否是您想要的,但这会为您提取这些词:

Pattern regexpPattern = Pattern.compile("([A-Z]?[a-z]+)\\)");
Matcher m = regexpPattern.matcher("your string");
while (m.find()) {
    System.out.println(m.group(1));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 2011-12-31
    相关资源
    最近更新 更多