Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / In this exam, you will implement the Inside-Outside algorithm forbei probabilistic context-free grammars

In this exam, you will implement the Inside-Outside algorithm forbei probabilistic context-free grammars

Statistics

In this exam, you will implement the Inside-Outside algorithm forbei probabilistic context-free grammars. You will find a code template in the file EMParser.py which you received along with this exam sheet. The file contains a class Node which is used to implement the parse forest, on which your code will operate.

A parse forest is a graph of Node elements. Each node element has an attribute label which stores the syntactic category (such as NP) in case of a nonterminal node, and a word (such as Klausur) in case of a terminal node. There is also an attribute analyses where all the analyses of the (possible ambiguous) node are stored. Each element of the list analyses belongs to the class Analysis and has an attribute children where the child nodes are stored as a list of Node elements.

You implement the inside-outside algorithm by completing the missing parts (...) in the code template. At the end of the template, you can see how the methods which you implement are used.

 

Task 1) Implement the method inside prob which recursively computes the inside probabi-lities for the parse forest. The method first checks whether self.alpha is None. If not, alpha has already been computed and can be returned directly. Otherwise, it is checked if self.analyses is None. If that is the case the current node is a terminal node and self.alpha is set to 1.

Otherwise it is a non-terminal node, and the method iterates over all analyses ana and calls ana.inside prob(self.label, rule prob) to compute the inside probabilities for this analysis. The inside probabilities of all analyses are summed up and stored in self.alpha. Finally, self.alpha is returned as the result.

You also implement the method inside prob of the class Analysis. This method calls the

function get rule probto look up the probability of the grammar rule in rule prob. The rule

probability is multiplied by the inside probabilities of all the child nodes which are computes

by calling the method inside prob of each child node. The result is stored in self.alpha and

returned. (9 points)

Task 2) Implement the method outside prob which computes the outside probabilities.

It iterates over all analyses ana and over all child nodes child of ana and computes the

contribution of analysis ana to the outside probability of child by multiplying the outside

probability of the parent node and the inside probability of ana and dividing by the inside

probability of child. The result is added to child.beta. Finally, the method child.outside prob

is called for recursive processing of the child.

 

You have to make sure that outside prob is executed for each node after all its parent no-

des have been processed. You proceed as follows: You increment the value of the attribute

 

self.counter in each call of the method self.inside prob by 1. By doing this, you count the

 

parent nodes. In the method self.outside prob, you reduce self.counter by 1 and check whe-

ther the new value is 0. If so, outside prob has visited all parent nodes and can now proceed

 

to process the current node. Otherwise the method returns without computing anything.

(8 points)

Task 3) Implement the method increment estimated freqs which computes the expected

rule frequencies. It iterates over all analyses ana of the current node, multiplies the outside

probability of the current node with the inside probability of ana, and divides by the overall

probability of the parse forest which has been passed to the method as an argument. The

result is added to rule freq[lhs][rhs], where lhs is the label of the current node and rhs is a

tuple with the labels of all the child nodes. Finally, the method recursively calls itself for each

child node.

Again, you must prevent the method from processing the same node twice. To this end, you

set self.alpha to None when the method is first called for some node. When the node is visited

again and alpha is None, you return immediately. The same is done in case of terminal nodes.

(8 points)

Task 4) Implement the method reestimate probs which estimates rule probabilities from

the rule frequencies without smoothing.

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE