Ejemplo n.º 1
0
def arity_parse_demo():
    """
    A demonstration showing the creation of a ``DependencyGrammar``
    in which a specific number of modifiers is listed for a given
    head.  This can further constrain the number of possible parses
    created by a ``ProjectiveDependencyParser``.
    """
    print()
    print("A grammar with no arity constraints. Each DependencyProduction")
    print("specifies a relationship between one head word and only one")
    print("modifier word.")
    grammar = DependencyGrammar.fromstring(
        """
    'fell' -> 'price' | 'stock'
    'price' -> 'of' | 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    """
    )
    print(grammar)

    print()
    print("For the sentence 'The price of the stock fell', this grammar")
    print("will produce the following three parses:")
    pdp = ProjectiveDependencyParser(grammar)
    trees = pdp.parse(["the", "price", "of", "the", "stock", "fell"])
    for tree in trees:
        print(tree)

    print()
    print("By contrast, the following grammar contains a ")
    print("DependencyProduction that specifies a relationship")
    print("between a single head word, 'price', and two modifier")
    print("words, 'of' and 'the'.")
    grammar = DependencyGrammar.fromstring(
        """
    'fell' -> 'price' | 'stock'
    'price' -> 'of' 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    """
    )
    print(grammar)

    print()
    print(
        "This constrains the number of possible parses to just one:"
    )  # unimplemented, soon to replace
    pdp = ProjectiveDependencyParser(grammar)
    trees = pdp.parse(["the", "price", "of", "the", "stock", "fell"])
    for tree in trees:
        print(tree)
Ejemplo n.º 2
0
def arity_parse_demo():
    """
    A demonstration showing the creation of a ``DependencyGrammar``
    in which a specific number of modifiers is listed for a given
    head.  This can further constrain the number of possible parses
    created by a ``ProjectiveDependencyParser``.
    """
    print()
    print('A grammar with no arity constraints. Each DependencyProduction')
    print('specifies a relationship between one head word and only one')
    print('modifier word.')
    grammar = DependencyGrammar.fromstring(
        """
    'fell' -> 'price' | 'stock'
    'price' -> 'of' | 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    """
    )
    print(grammar)

    print()
    print('For the sentence \'The price of the stock fell\', this grammar')
    print('will produce the following three parses:')
    pdp = ProjectiveDependencyParser(grammar)
    trees = pdp.parse(['the', 'price', 'of', 'the', 'stock', 'fell'])
    for tree in trees:
        print(tree)

    print()
    print('By contrast, the following grammar contains a ')
    print('DependencyProduction that specifies a relationship')
    print('between a single head word, \'price\', and two modifier')
    print('words, \'of\' and \'the\'.')
    grammar = DependencyGrammar.fromstring(
        """
    'fell' -> 'price' | 'stock'
    'price' -> 'of' 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    """
    )
    print(grammar)

    print()
    print(
        'This constrains the number of possible parses to just one:'
    )  # unimplemented, soon to replace
    pdp = ProjectiveDependencyParser(grammar)
    trees = pdp.parse(['the', 'price', 'of', 'the', 'stock', 'fell'])
    for tree in trees:
        print(tree)
Ejemplo n.º 3
0
def rule_based_demo():
    from nltk.grammar import DependencyGrammar

    grammar = DependencyGrammar.fromstring("""
    'taught' -> 'play' | 'man'
    'man' -> 'the' | 'in'
    'in' -> 'corner'
    'corner' -> 'the'
    'play' -> 'golf' | 'dachshund' | 'to'
    'dachshund' -> 'his'
    """)
    print(grammar)
    ndp = NonprojectiveDependencyParser(grammar)
    graphs = ndp.parse([
        "the",
        "man",
        "in",
        "the",
        "corner",
        "taught",
        "his",
        "dachshund",
        "to",
        "play",
        "golf",
    ])
    print("Graphs:")
    for graph in graphs:
        print(graph)
Ejemplo n.º 4
0
def rule_based_demo():
    from nltk.grammar import DependencyGrammar

    grammar = DependencyGrammar.fromstring("""
    'taught' -> 'play' | 'man'
    'man' -> 'the' | 'in'
    'in' -> 'corner'
    'corner' -> 'the'
    'play' -> 'golf' | 'dachshund' | 'to'
    'dachshund' -> 'his'
    """)
    print(grammar)
    ndp = NonprojectiveDependencyParser(grammar)
    graphs = ndp.parse([
        'the',
        'man',
        'in',
        'the',
        'corner',
        'taught',
        'his',
        'dachshund',
        'to',
        'play',
        'golf',
    ])
    print('Graphs:')
    for graph in graphs:
        print(graph)
def rule_based_demo():
    from nltk.grammar import DependencyGrammar

    grammar = DependencyGrammar.fromstring(
        """
    'taught' -> 'play' | 'man'
    'man' -> 'the' | 'in'
    'in' -> 'corner'
    'corner' -> 'the'
    'play' -> 'golf' | 'dachshund' | 'to'
    'dachshund' -> 'his'
    """
    )
    print(grammar)
    ndp = NonprojectiveDependencyParser(grammar)
    graphs = ndp.parse(
        [
            'the',
            'man',
            'in',
            'the',
            'corner',
            'taught',
            'his',
            'dachshund',
            'to',
            'play',
            'golf',
        ]
    )
    print('Graphs:')
    for graph in graphs:
        print(graph)
Ejemplo n.º 6
0
def arity_parse_demo():
    """
    A demonstration showing the creation of a ``DependencyGrammar``
    in which a specific number of modifiers is listed for a given
    head.  This can further constrain the number of possible parses
    created by a ``ProjectiveDependencyParser``.
    """
    print()
    print('A grammar with no arity constraints. Each DependencyProduction')
    print('specifies a relationship between one head word and only one')
    print('modifier word.')
    grammar = DependencyGrammar.fromstring("""
    'fell' -> 'price' | 'stock'
    'price' -> 'of' | 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    """)
    print(grammar)

    print()
    print('For the sentence \'The price of the stock fell\', this grammar')
    print('will produce the following three parses:')
    pdp = ProjectiveDependencyParser(grammar)
    trees = pdp.parse(['the', 'price', 'of', 'the', 'stock', 'fell'])
    for tree in trees:
        print(tree)

    print()
    print('By contrast, the following grammar contains a ')
    print('DependencyProduction that specifies a relationship')
    print('between a single head word, \'price\', and two modifier')
    print('words, \'of\' and \'the\'.')
    grammar = DependencyGrammar.fromstring("""
    'fell' -> 'price' | 'stock'
    'price' -> 'of' 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    """)
    print(grammar)

    print()
    print('This constrains the number of possible parses to just one:'
          )  # unimplemented, soon to replace
    pdp = ProjectiveDependencyParser(grammar)
    trees = pdp.parse(['the', 'price', 'of', 'the', 'stock', 'fell'])
    for tree in trees:
        print(tree)
Ejemplo n.º 7
0
def projective_rule_parse_demo():
    """
    A demonstration showing the creation and use of a
    ``DependencyGrammar`` to perform a projective dependency
    parse.
    """
    grammar = DependencyGrammar.fromstring("""
    'scratch' -> 'cats' | 'walls'
    'walls' -> 'the'
    'cats' -> 'the'
    """)
    print(grammar)
    pdp = ProjectiveDependencyParser(grammar)
    trees = pdp.parse(['the', 'cats', 'scratch', 'the', 'walls'])
    for tree in trees:
        print(tree)
def projective_rule_parse_demo():
    """
    A demonstration showing the creation and use of a
    ``DependencyGrammar`` to perform a projective dependency
    parse.
    """
    grammar = DependencyGrammar.fromstring("""
    'scratch' -> 'cats' | 'walls'
    'walls' -> 'the'
    'cats' -> 'the'
    """)
    print(grammar)
    pdp = ProjectiveDependencyParser(grammar)
    trees = pdp.parse(['the', 'cats', 'scratch', 'the', 'walls'])
    for tree in trees:
        print(tree)
def rule_based_demo():
    from nltk.grammar import DependencyGrammar

    grammar = DependencyGrammar.fromstring(
        """
    'taught' -> 'play' | 'man'
    'man' -> 'the' | 'in'
    'in' -> 'corner'
    'corner' -> 'the'
    'play' -> 'golf' | 'dachshund' | 'to'
    'dachshund' -> 'his'
    """
    )
    print(grammar)
    ndp = NonprojectiveDependencyParser(grammar)
    graphs = ndp.parse(["the", "man", "in", "the", "corner", "taught", "his", "dachshund", "to", "play", "golf"])
    print("Graphs:")
    for graph in graphs:
        print(graph)