4

我想以直观的方式查看以下结果是否是我所需要的:

import nltk 
sentence = [("the", "DT"), ("little", "JJ"), ("yellow", "JJ"), ("dog", "NN"), ("barked","VBD"), ("at", "IN"), ("the", "DT"), ("cat", "NN")]

pattern = """NP: {<DT>?<JJ>*<NN>}
VBD: {<VBD>}
IN: {<IN>}"""
NPChunker = nltk.RegexpParser(pattern) 
result = NPChunker.parse(sentence)

来源:https ://stackoverflow.com/a/31937278/3552975

我不知道为什么我不能漂亮打印result.

result.pretty_print()

错误显示TypeError: not all arguments converted during string formatting. 我使用 Python3.5、nltk3.3。

4

1 回答 1

9

如果您正在寻找带括号的解析输出,您可以使用Tree.pprint()

>>> import nltk 
>>> sentence = [("the", "DT"), ("little", "JJ"), ("yellow", "JJ"), ("dog", "NN"), ("barked","VBD"), ("at", "IN"), ("the", "DT"), ("cat", "NN")]
>>> 
>>> pattern = """NP: {<DT>?<JJ>*<NN>}
... VBD: {<VBD>}
... IN: {<IN>}"""
>>> NPChunker = nltk.RegexpParser(pattern) 
>>> result = NPChunker.parse(sentence)
>>> result.pprint()
(S
  (NP the/DT little/JJ yellow/JJ dog/NN)
  (VBD barked/VBD)
  (IN at/IN)
  (NP the/DT cat/NN))

但很可能你正在寻找

                             S                                      
            _________________|_____________________________          
           NP                        VBD       IN          NP       
   ________|_________________         |        |      _____|____     
the/DT little/JJ yellow/JJ dog/NN barked/VBD at/IN the/DT     cat/NN

让我们深入研究Tree.pretty_print() https://github.com/nltk/nltk/blob/develop/nltk/tree.py#L692中的代码:

def pretty_print(self, sentence=None, highlight=(), stream=None, **kwargs):
    """
    Pretty-print this tree as ASCII or Unicode art.
    For explanation of the arguments, see the documentation for
    `nltk.treeprettyprinter.TreePrettyPrinter`.
    """
    from nltk.treeprettyprinter import TreePrettyPrinter
    print(TreePrettyPrinter(self, sentence, highlight).text(**kwargs),
          file=stream)

它正在创建一个TreePrettyPrinter对象,https://github.com/nltk/nltk/blob/develop/nltk/treeprettyprinter.py#L50

class TreePrettyPrinter(object):
    def __init__(self, tree, sentence=None, highlight=()):
        if sentence is None:
            leaves = tree.leaves()
            if (leaves and not any(len(a) == 0 for a in tree.subtrees())
                    and all(isinstance(a, int) for a in leaves)):
                sentence = [str(a) for a in leaves]
            else:
                # this deals with empty nodes (frontier non-terminals)
                # and multiple/mixed terminals under non-terminals.
                tree = tree.copy(True)
                sentence = []
                for a in tree.subtrees():
                    if len(a) == 0:
                        a.append(len(sentence))
                        sentence.append(None)
                    elif any(not isinstance(b, Tree) for b in a):
                        for n, b in enumerate(a):
                            if not isinstance(b, Tree):
                                a[n] = len(sentence)
                                sentence.append('%s' % b)
        self.nodes, self.coords, self.edges, self.highlight = self.nodecoords(
                tree, sentence, highlight)

看起来引发错误的行是sentence.append('%s' % b) https://github.com/nltk/nltk/blob/develop/nltk/treeprettyprinter.py#L97

问题是它为什么会引发 TypeError

TypeError: not all arguments converted during string formatting

如果我们仔细看,它看起来让我们可以print('%s' % b)用于大多数基本的 python 类型

# String
>>> x = 'abc'
>>> type(x)
<class 'str'>
>>> print('%s' % x)
abc

# Integer
>>> x = 123
>>> type(x)
<class 'int'>
>>> print('%s' % x)
123

# Float 
>>> x = 1.23
>>> type(x)
<class 'float'>
>>> print('%s' % x)
1.23

# Boolean
>>> x = True
>>> type(x)
<class 'bool'>
>>> print('%s' % x)
True

令人惊讶的是,它甚至可以在列表中使用!

>>> x = ['abc', 'def']
>>> type(x)
<class 'list'>
>>> print('%s' % x)
['abc', 'def']

但它被阻碍了tuple

>>> x = ('DT', 123)
>>> x = ('abc', 'def')
>>> type(x)
<class 'tuple'>
>>> print('%s' % x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

所以如果我们回到https://github.com/nltk/nltk/blob/develop/nltk/treeprettyprinter.py#L95的代码

if not isinstance(b, Tree):
    a[n] = len(sentence)
    sentence.append('%s' % b)

由于我们知道sentence.append('%s' % b)无法处理tuple,因此添加对元组类型的检查并以某种方式连接元组中的项目并转换为 astr将产生 nice pretty_print

if not isinstance(b, Tree):
    a[n] = len(sentence)
    if type(b) == tuple:
        b = '/'.join(b)
    sentence.append('%s' % b)

[出去]:

                             S                                      
            _________________|_____________________________          
           NP                        VBD       IN          NP       
   ________|_________________         |        |      _____|____     
the/DT little/JJ yellow/JJ dog/NN barked/VBD at/IN the/DT     cat/NN

在不更改nltk代码的情况下,是否仍然可以获得漂亮的打印?

让我们看看resultieTree对象的样子:

Tree('S', [Tree('NP', [('the', 'DT'), ('little', 'JJ'), ('yellow', 'JJ'), ('dog', 'NN')]), Tree('VBD', [('barked', 'VBD')]), Tree('IN', [('at', 'IN')]), Tree('NP', [('the', 'DT'), ('cat', 'NN')])])

看起来叶子被保存为字符串元组的列表,例如[('the', 'DT'), ('cat', 'NN')],所以我们可以做一些修改,使它成为字符串列表,例如[('the/DT'), ('cat/NN')],这样Tree.pretty_print()会很好玩。

因为我们知道这Tree.pprint()有助于将字符串元组连接到我们想要的形式,即

(S
  (NP the/DT little/JJ yellow/JJ dog/NN)
  (VBD barked/VBD)
  (IN at/IN)
  (NP the/DT cat/NN))

我们可以简单地输出到括号中的解析字符串,然后重新读取解析Tree对象Tree.fromstring()

from nltk import Tree
Tree.fromstring(str(result)).pretty_print()

结局:

import nltk 
sentence = [("the", "DT"), ("little", "JJ"), ("yellow", "JJ"), ("dog", "NN"), ("barked","VBD"), ("at", "IN"), ("the", "DT"), ("cat", "NN")]

pattern = """NP: {<DT>?<JJ>*<NN>}
VBD: {<VBD>}
IN: {<IN>}"""
NPChunker = nltk.RegexpParser(pattern) 
result = NPChunker.parse(sentence)

Tree.fromstring(str(result)).pretty_print()

[出去]:

                             S                                      
            _________________|_____________________________          
           NP                        VBD       IN          NP       
   ________|_________________         |        |      _____|____     
the/DT little/JJ yellow/JJ dog/NN barked/VBD at/IN the/DT     cat/NN
于 2018-08-31T01:49:37.503 回答