Exemplo n.º 1
0
    def setup_words(self, words):
        """
        Instantiate the source of words.
        """
        if self.using_features:
            words = [icat.from_string(w) for w in words]


        return self.input_source(words)
Exemplo n.º 2
0
def parse(sentence, verbose=False, topcat='S', grammar=GRAMMAR,sep=' ', input_source=LinearWords, 
            use_features=False,show_chart=False,print_trees=True,return_chart=False,
            trace_edges=True,
            return_trees = False):
    """
    Print out the parses of a sentence


    Parameters
    ----------

    sentence: list<string>
        the words to be parsed.

    Examples
    --------

    >>> parse(["the","pigeons",'are','punished','and','they','suffer'])
    ['the', 'pigeons', 'are', 'punished', 'and', 'they', 'suffer']
    Parse 1:
    S
     S
      Np
       det the
       Nn
        n pigeons
      cop are
      ppart punished
     conj and
     S
      Np
       pn they
      Vp
       v suffer
    1 parses

     >>> parse(["the","pigeons",'are','punished','and','they','blink'])
     ['the', 'pigeons', 'are', 'punished', 'and', 'they', 'blink']
     No parse

    """
    if use_features:
        grammar = features.make_feature_grammar()
        topcat = icat.from_string(topcat)


    v = Chart(sentence, verbose=verbose,grammar=grammar,input_source=input_source, using_features=use_features)
    v.topcat = topcat
    sols = v.solutions(topcat)

    res = v.results(show_chart=show_chart,
                    print_trees=print_trees, 
                    trace_edges=trace_edges)



    silent = not (print_trees or show_chart)
    if not silent:
        print sentence

    if show_chart:
        v.show()

    if print_trees:
        i = 0
        for e in sols:
            for tree in v.trees(e):
                i += 1
                if print_trees:
                    print "Parse %d:" % i
                    print treestring(tree, tab=0, sep=sep),

    if not silent:
        if res['n_trees'] == 0:
            print "No parse"
        else:
            print res['n_trees'], "parses"

    if return_chart:
        return v
    else:
        return None