%%html
<script>
function code_toggle() {
if (code_shown){
$('div.input').hide('500');
$('#toggleButton').val('Show Code')
} else {
$('div.input').show('500');
$('#toggleButton').val('Hide Code')
}
code_shown = !code_shown
}
$( document ).ready(function(){
code_shown=false;
$('div.input').hide()
});
</script>
<form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show Code"></form>
%%capture
%load_ext autoreload
%autoreload 2
import sys
sys.path.append("..")
import statnlpbook.util as util
import statnlpbook.parsing as parsing
from statnlpbook.transition import *
util.execute_notebook('transition-based_dependency_parsing.ipynb')
Constituent Parsers more complex than needed:
Dependency Parsing addresses this...
tokens = ["ROOT", "Economic", "news", "had", "little", "effect", "on", "financial", "markets", "."]
arcs = set([(0,3, "root"), (0,9,"p"), (2,1,"amod"),(3,2,"nsubj"), (3, 5, "dobj"), (5,4,"amod"), (5,6, "prep"), (6,8,"pmod"), (8,7,"amod")])
render_displacy(*transition.to_displacy_graph(arcs, tokens),"900px")
How would you define features $\mathbf{f}(h,c,\x)$?
tokens = ["ROOT", "Economic", "news", "had", "little", "effect", "on", "financial", "markets", "."]
arcs = set([(3, 5, "dobj")])
render_displacy(*transition.to_displacy_graph(arcs, tokens),"900px")
Find the tree with maximal total score
$$ \argmax_{\y} \sum_{(h,c) \in \y} s(h,c,\x) $$Corresponds to finding maximum spanning trees
higher order dependencies
Currently the state-of-the-art...
Akin to bottom up parsing for CFGs...
render_transitions_displacy(transitions[0:1], tokenized_sentence)
buffer | stack | parse | action |
ROOT Economic news had little effect on financial markets . | INIT |
render_transitions_displacy(transitions[2:3],tokenized_sentence)
buffer | stack | parse | action |
news had little effect on financial markets . | ROOT Economic | shift |
render_transitions_displacy(transitions[9:10], tokenized_sentence)
buffer | stack | parse | action |
on financial markets . | ROOT had effect | rightArc-dobj |
We use the following
push the word at the top of the buffer to the stack
$$ (S, i|B, A)\rightarrow(S|i, B, A) $$render_transitions_displacy(transitions[0:2], tokenized_sentence)
buffer | stack | parse | action |
ROOT Economic news had little effect on financial markets . | INIT | ||
Economic news had little effect on financial markets . | ROOT | shift |
pop the word at the top of the stack if it has a head
$$ (S|i, B, A)\rightarrow(S, B, A) $$render_transitions_displacy(transitions[13:15], tokenized_sentence)
buffer | stack | parse | action |
. | ROOT had effect on markets | rightArc-pmod | |
. | ROOT had effect on | reduce |
add labeled arc from top of stack $i$ to top of the buffer $j$
$$ (S|i, j|B, A) \rightarrow (S|i|j, B, A\cup\{(i,j,l)\}) $$render_transitions_displacy(transitions[5:7], tokenized_sentence)
buffer | stack | parse | action |
had little effect on financial markets . | ROOT | leftArc-nsubj | |
little effect on financial markets . | ROOT had | rightArc-root |
add labeled arc from top of buffer, $j$, to top of stack, $i$, if $i$ has no head
$$ (S|i, j|B, A) \rightarrow (S, j|B, A\cup\{(j,i,l)\}) $$render_transitions_displacy(transitions[2:4], tokenized_sentence)
buffer | stack | parse | action |
news had little effect on financial markets . | ROOT Economic | shift | |
news had little effect on financial markets . | ROOT | leftArc-amod |
render_transitions_displacy(transitions[:], tokenized_sentence)
buffer | stack | parse | action |
ROOT Economic news had little effect on financial markets . | INIT | ||
Economic news had little effect on financial markets . | ROOT | shift | |
news had little effect on financial markets . | ROOT Economic | shift | |
news had little effect on financial markets . | ROOT | leftArc-amod | |
had little effect on financial markets . | ROOT news | shift | |
had little effect on financial markets . | ROOT | leftArc-nsubj | |
little effect on financial markets . | ROOT had | rightArc-root | |
effect on financial markets . | ROOT had little | shift | |
effect on financial markets . | ROOT had | leftArc-amod | |
on financial markets . | ROOT had effect | rightArc-dobj | |
financial markets . | ROOT had effect on | rightArc-prep | |
markets . | ROOT had effect on financial | shift | |
markets . | ROOT had effect on | leftArc-amod | |
. | ROOT had effect on markets | rightArc-pmod | |
. | ROOT had effect on | reduce | |
. | ROOT had effect | reduce | |
. | ROOT had | reduce | |
. | ROOT | reduce | |
ROOT . | rightArc-p | ||
ROOT . | TERMINAL |
How to decide what action to take?