如何在 JAPE 右侧制作 if-else 语句?

How to make if-else statement in JAPE right hand side?

我是 JAPE(Java 注释模式引擎)的新手,它是 GATE 的一部分。

我已经在 LHS 中制定了一些规则,这些规则会在文本中生成一些标签(比如标签 a、b 和 c)。

我的文本由几个部分组成,我想根据生成的标签对每个部分进行分类。

如图:

<record id=001>
lorem <a>ipsum</a> dolor sit amet
</record>
<record id=002>
consectetur <b>adipiscing</b> elit, sed do eiusmod <a>tempor</a> incididunt ut labore et dolore magna aliqua
</record>
<record id=003>
Ut enim ad minim veniam, quis <a>nostrud</a> exercitation <c>ullamco</c> laboris nisi ut aliquip ex ea commodo consequat.
</record>

如您所见,每条记录可以包含多个在 LHS 中生成的标签。

我想根据其中的标签对每条记录进行分类。

说,如果一条记录包含标签a,则将其分类为A。如果包含ab,则将其分类为A,假设ab.

我注意到我应该在 RHS 中操作它,但我不知道如何编写它。

你能给我一些线索吗?

谢谢。

此致。

为了使用 JAPE 语法构建 if-else 语句,并不总是需要在 RHS 中使用 Java。

在编写规则时,将处理分成多个阶段通常会很方便:每个阶段都会产生一些结果,然后可以将这些结果传递给下一个阶段。所以,根据你刚才的描述,数据处理可以分为以下三个阶段。

  1. RecordFinder,其中returns文档内的记录,即Record个注释。
  2. TagFinder,文档中的 returns 标签 ab
  3. Intersection:它在记录中搜索标签ab

文件Main.jape

MultiPhase: Main
Phases: 
RecordFinder
TagFinder
Intersection

文件RecordFinder.jape

此阶段可以对文档中的记录进行注释。此 JAPE 文件的唯一规则是读取 tokens(即标记器返回的 Token 注释)作为输入,并在文档中查找记录(即标签 record) ,最后是 returns Record 注释。

注意,在Options中,control设置为first,因为目的是找到包含标记 <record> 的序列的第一次出现,然后是一个或多个其他标记,然后是标记 </record>.

Phase: RecordFinder
Input: Token
Options: control = first debug = true


// The following rule is able to find the sentence within a record
Rule: RuleToFindRecord
(
    ({Token.string == "<"} {Token.string == "record"} ({Token})* {Token.string == ">"})
    ({Token})*
    ({Token.string == "<"} {Token.string == "/"} {Token.string == "record"} {Token.string == ">"})
):match
-->
:match.Record = { rule = "RuleToFindRecord" }

文件TagFinder.jape

这个阶段读取 tokens 作为输入并在文本中找到标签 ab,最后它 returns ab 注释。

Phase: TagFinder
Input: Token
Options: control = first debug = true


// The following rule is able to find the tag "a" within the document.
Rule: RuleToFindTag_a
(
    (
        ({Token.string == "<"} {Token.string == "a"} {Token.string == ">"})
        ({Token})*
        ({Token.string == "<"} {Token.string == "/"} {Token.string == "a"} {Token.string == ">"})
    )
    |
    ({Token.string == "<"} {Token.string == "a"} {Token.string == "/"} {Token.string == ">"})
):match
-->
:match.a = { rule = "RuleToFindTag_a" }


// The following rule is able to find the tag "b" within the document.
Rule: RuleToFindTag_b
(
    (
        ({Token.string == "<"} {Token.string == "b"} {Token.string == ">"})
        ({Token})*
        ({Token.string == "<"} {Token.string == "/"} {Token.string == "b"} {Token.string == ">"})
    )
    |
    ({Token.string == "<"} {Token.string == "b"} {Token.string == "/"} {Token.string == ">"})
):match
-->
:match.b = { rule = "RuleToFindTag_b" }

文件Intersection.jape

此阶段读取注释 Recordab 作为输入并在 Record 中搜索标签 ab .阅读 this 作为关于 containswithin 运算符的参考(我在以下规则中使用了这些运算符之一)。

Phase: Intersection
Input: Record a b
Options: control = first debug = true


// A record matches with this rule if it contains both tag a and tag b.
Rule: Rule_1
(
    {Record contains a, Record contains b}
):match
-->
:match.Record_with_both_tags = { rule = "Rule_1" }


// A record matches with this rule if it contains tag a.
Rule: Rule_2
(
    {Record contains a}
):match
-->
:match.Record_with_tag_a = { rule = "Rule_2" }