Question: The instructions are as follows: Read Syntax Trees from File: Read all the syntax trees from the provided file train.trees.pre.unk into a list. Use the
The instructions are as follows:
Read Syntax Trees from File: Read all the syntax trees from the provided file train.trees.pre.unk into a list. Use the same code used in the Step
Convert Trees to CNF: Convert each tree into Chomsky Normal Form with the Tree.chomskynormalform method from nltk library and the horzMarkov parameter.
Extract Productions:
Having converted the trees to Chomsky Normal Form, our next step is to extract the grammatical productions from these CNF trees. Grammatical productions are essentially the rules that define how sentences in a language can be constructed and are pivotal for understanding the structure and syntax of the language.
To accomplish this, create a list named productions. This list will store all the production rules extracted from each tree. Traverse through each of your CNF trees, and for every tree, use the productions method. This method will break down the tree into its constituent production rules. Accumulate these rules in the productions list, which will later be used for constructing our grammar.
I have found that the chomskynormalform and production methods don't support lists, so I have been appending to trees. But now I can't get the productions method to work at all.
The current state of my code is below:
from nltktree import chomskynormalform
with opentraintrees.prestline.unk', r as firstfile: #read the first Line of the file
firstline firstfile.readlinestrip
bigtree Tree.fromstringfirstline
with opentraintrees.pre.unk', r as file: #read the lines and append to original tree
for line in file:
line file.readlinestrip
tree Tree.fromstringline
bigtree.appendtree
#bigtree.appendtree # something like this, define bigtree as empty and then add the subtree
#trees Treefromstringlinestrip for line in file
treelist
for tree in bigtree:
Tree.chomskynormalformtree horzMarkov vertMarkov
treeforlist strtree
#treeforlist Tree.fromstring
treelist.appendtreeforlist
printtreelist
productions
#for eachtree in treelist:
#eachtreet Tree.fromstringstreachtree
#productions.extendeachtreetproductions
#eachtreeprod eachtree.productions
#productions.appendeachtreeprod
#for production in productions:
#printproductions
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
