【问题标题】:How to make if-else statement in JAPE right hand side?如何在 JAPE 右侧制作 if-else 语句?
【发布时间】:2016-10-19 03:54:01
【问题描述】:

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

我已经在 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,假设a强于b

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

你能给我一个线索或什么吗?

谢谢。

问候。

【问题讨论】:

    标签: java annotations gate java-annotations


    【解决方案1】:

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

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

    1. RecordFinder,返回文档内的记录,即Record注解。
    2. TagFinder,在文档中返回标签ab
    3. 交集:在记录中搜索标签ab

    文件 Main.jape

    MultiPhase: Main
    Phases: 
    RecordFinder
    TagFinder
    Intersection
    

    文件 RecordFinder.jape

    此阶段能够对文档中的记录进行注释。这个JAPE文件的唯一规则是读取tokens(即tokeniser返回的Token注解)作为输入,并在文档中查找记录(即标签record),最后返回@987654330 @注解。

    注意,在Options中,control被设置为first,因为目的是找到序列的第一个出现包含一个令牌&lt;record&gt;,后跟一个或多个其他令牌,然后是一个令牌&lt;/record&gt;

    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,最后返回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" }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 2021-01-27
      • 2020-11-28
      相关资源
      最近更新 更多