コード例 #1
0
ファイル: hw2.py プロジェクト: user1837/ANLPAssignment2
"Vt -> 'ate' | 'book' | 'Book' | 'gave' | 'told'", #transitive
"Vdt -> 'gave' | 'told' ", #ditransitive
"Subconj -> 'that'", #subordinating conjunction
"Mod -> 'Can' | 'will'", #modal verbs
"Aux -> 'did' ", #auxiliary verbs
"WhAdv -> 'Why'",
"PropN -> 'John' | 'Mary' | 'NYC' | 'London'",
"Adj -> 'nice' | 'drawing'",
"Pro -> 'you' | 'he'",
"Adv -> 'today'"
])

print(grammar) #the simpler grammar
chart=CKY(grammar)
 #this illustrates tracing of a very simple sentence; feel free to try others.
chart.recognise(tokenise("the frogs swim"),True)
chart.pprint()

#build a chart with the larger grammar
chart2=CKY(grammar2)

# Note, please do _not_ use the Tree.draw() method uncommented
# _anywhere in this file_ (you are encouraged to use it in preparing
# your report).

# The sentences to examine.
#
for s in ["John gave a book to Mary.",
           "John gave Mary a book.",
           "John gave Mary a nice drawing book.",
           "John ate salad with mushrooms with a fork.",
コード例 #2
0
ファイル: ass2.py プロジェクト: 1sForTheElder/anlp2
Det -> NP "'s"
Nom -> N SRel | N
VP -> Vi | Vt NP | VP PP
PP -> Prep NP
SRel -> Relpro VP
Det -> 'a' | 'the'
N -> 'fish' | 'frogs' | 'soup' | 'children' | 'books'
Prep -> 'in' | 'for'
Vt -> 'saw' | 'ate' | 'read'
Vi -> 'fish' | 'swim'
Relpro -> 'that'
""")

print(grammar)
chart = CKY(grammar)
chart.recognise("the frogs swim".split())  # Should use
# tokenise(s) once that's fixed
chart.pprint()

# Q1: Uncomment this once you've completed Q1
chart.recognise(tokenise("the frogs swim"), True)
# Q3 Uncomment the next three once when you're working on Q3
chart.recognise(tokenise("fish fish"))
chart.pprint()
chart.recognise(tokenise("fish fish"), True)

# Use this grammar for the rest of the assignment
grammar2 = parse_grammar([
    "S -> Sdecl '.' | Simp '.' | Sq '?' ", "Sdecl -> NP VP", "Simp -> VP",
    "Sq -> Sqyn | Swhadv", "Sqyn -> Mod Sdecl | Aux Sdecl",
    "Swhadv -> WhAdv Sqyn", "Sc -> Subconj Sdecl", "NP -> PropN | Pro | NP0 ",
コード例 #3
0
    "Vt -> 'ate' | 'book' | 'Book' | 'gave' | 'told'",  #transitive
    "Vdt -> 'gave' | 'told' ",  #ditransitive
    "Subconj -> 'that'",  #subordinating conjunction
    "Mod -> 'Can' | 'will'",  #modal verbs
    "Aux -> 'did' ",  #auxiliary verbs
    "WhAdv -> 'Why'",
    "PropN -> 'John' | 'Mary' | 'NYC' | 'London'",
    "Adj -> 'nice' | 'drawing'",
    "Pro -> 'you' | 'he'",
    "Adv -> 'today'"
])

print(grammar)  #the simpler grammar
chart = CKY(grammar)
#this illustrates tracing of a very simple sentence; feel free to try others.
chart.recognise(tokenise("the frogs swim"), True)
chart.pprint()

#build a chart with the larger grammar
chart2 = CKY(grammar2)

# Note, please do _not_ use the Tree.draw() method uncommented
# _anywhere in this file_ (you are encouraged to use it in preparing
# your report).

# The sentences to examine.
#
# for s in ["John gave a book to Mary.",
#           "John gave Mary a book.",
#           "John gave Mary a nice drawing book.",
#           "John ate salad with mushrooms with a fork.",
コード例 #4
0
    "Adj -> 'nice' | 'drawing'",
    "Pro -> 'you' | 'he'",
    "Adv -> 'today'"
])

print(grammar)  #the simpler grammar
chart = CKY(grammar)
#this illustrates tracing of a very simple sentence; feel free to try others.
# chart.recognise(tokenise("the frogs swim"),True)
# chart.pprint()

#build a chart with the larger grammar
chart2 = CKY(grammar2)

# Note, please do _not_ use the Tree.draw() method uncommented
# _anywhere in this file_ (you are encouraged to use it in preparing
# your report).

# The sentences to examine.
#
for s in ["Book a flight to NYC."]:
    print(s, chart2.recognise(tokenise(s), True))
    chart2.pprint()

    # print(s, chart2.recognise(tokenise(s), True))

# Task 5
# for s in [...]:
#     print(s, chart2.parse(tokenise(s)))
#     print(chart2.firstTree().pprint())
コード例 #5
0
#chart.pprint()

#build a chart with the larger grammar
chart2=CKY(grammar2)
#chart2.recognise(tokenise("John gave a book to Mary."),True)
#chart2.pprint()

# Note, please do _not_ use the Tree.draw() method uncommented
# _anywhere in this file_ (you are encouraged to use it in preparing
# your report).

# The sentences to examine.

for s in ["John gave a book to Mary.",
           "John gave Mary a book.",
           "John gave Mary a nice drawing book.",
           "John ate salad with mushrooms with a fork.",
           "Book a flight to NYC.",
           "Can you book a flight to London?",
           "Why did John book the flight?",
           "John told Mary that he will book a flight today."]:
    print(s, chart2.recognise(tokenise(s)))
    chart2.pprint()
# Task 5
# for s in [...]:
#     print(s, chart2.parse(tokenise(s)))
#     print(chart2.firstTree().pprint())