【问题标题】:How to find invalid Link Grammar tokens?如何找到无效的链接语法标记?
【发布时间】:2018-04-07 23:03:15
【问题描述】:

我想将Link Grammar Python3 绑定用于简单的语法检查器。虽然链接 API 的文档相对完善,但似乎没有办法访问所有阻止链接的令牌。

这是我目前所拥有的:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from linkgrammar import Sentence, ParseOptions, Dictionary, __version__
print('Link Grammar Version:', __version__)

for sentence in ['This is a valid sample sentence.', 'I Can Has Cheezburger?']:
    sent = Sentence(sentence, Dictionary(), ParseOptions())
    linkages = sent.parse()
    if len(linkages) > 0:
        print('Valid:', sentence)
    else:
        print('Invalid:', sentence)

(我使用 link-grammar-5.4.3 进行测试。)

当我使用 Link Parser 命令行工具分析无效的例句时,我得到了以下输出:

linkparser> I Can Has Cheezburger?
No complete linkages found.
Found 1 linkage (1 had no P.P. violations) at null count 1
    Unique linkage, cost vector = (UNUSED=1 DIS= 0.10 LEN=7)

    +------------------Xp------------------+
    +------------->Wa--------------+       |
    |            +---G--+-----G----+       |
    |            |      |          |       |
LEFT-WALL [I] Can[!] Has[!] Cheezburger[!] ?

如何使用 Python3 获取所有标有 [!] 或 [?] 的潜在无效标记?

【问题讨论】:

    标签: python-3.x validation token link-grammar


    【解决方案1】:

    bindings/python-examples/sentence-check.py 中查看它是如何完成的。 最好看看最新的repo版本(the current one is here),因为这个demo程序在5.4.3有一个bug。

    具体来说,下面是提取词表:

    words = list(linkage.words())
    

    未链接的单词包含在[] 中。带有[] 的词是猜测词。例如,[!] 表示该词已被正则表达式分类(出现在文件 4.0.regex 中),然后已在字典中查找此分类。如果将解析选项display_morphology 设置为True,则分类正则表达式名称出现在! 之后。

    这里是单词输出格式的完整图例:

     [word]            Null-linked word
     word[!]           word classified by a regex
     word[!REGEX_NAME] word classified by REGEX_NAME (turn on by morphology=1)
     word[~]           word generated by a spell guess (unknown original word)
     word[&]           word run-on separated by a spell guess
     word[?]           word is unknown (looked up in the dict as UNKNOWN-WORD)
     word.POS          word found in the dictionary as word.POS
     word.#CORRECTION  word is probably a typo - got linked as CORRECTION
    
    For dictionaries that support morphology (turn on by morphology=1):
     word=             A prefix morpheme
     =word             A suffix morpheme
     word.=            A stem
    

    将输出单词与原始句子单词匹配可能很有用,尤其是在拼写更正或打开形态学的情况下。上述演示程序sentence-check.py 会在您使用-p 调用它时执行此操作 - 请参阅if arg.position: 下的代码。

    在您的演示句I Can Has Cheezburger? 的情况下,只有单词I 没有链接,其他词已分类为大写单词并作为专有名词链接(G 链接类型)。

    您可以在summarize-links 中找到有关链接类型的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2014-08-25
      • 2021-12-02
      • 2016-10-04
      相关资源
      最近更新 更多