def parse(args, stdinput): """Parse a given sentence after inducing a grammar from a given corpus. Parameters ---------- args : list(str) The list of arguments: corpus file, sentence. stdinput : list(str) The pruning policy which should be used for the parsing process. """ # assign the parameter values corpus = TigerXMLCorpusReader(args[0], encoding='utf8') sent = args[1] # create grammar and gold trees trees = [ ImmutableTree.convert(canonicalize(t)) for t in list(corpus.trees().values()) ] sentences = list(corpus.sents().values()) grammar = Grammar(trees, sentences) goldtrees = [t for s, t in zip(sentences, trees) if ' '.join(s) == sent] # create initial pruning policy pp = deserialize(stdinput, FEATURES, grammar) if\ stdinput else PruningPolicy() # create derivation tree parser = Parser(grammar) derivationgraph = parser.parse(sent, pp) derivationtree = derivationgraph.get_tree() stdout.flush() # print results if isinstance(derivationtree, Tree): # print graphical representation if the sentence could be parsed print(derivationtree.pprint()) drawtree = DrawTree(derivationtree, sent.split()) print("\n derivation tree: \n" + drawtree.text()) else: # otherwise print a error message print(derivationtree) if len(goldtrees) > 0: # print graphical representation if there is a gold tree drawgold = DrawTree(goldtrees[0], sent.split()) print("\n gold tree: \n" + drawgold.text()) # print recall if both trees are available if isinstance(derivationtree, Tree): print("\n recall: %f" % accuracy(derivationtree, goldtrees[0]))
def newlabel(): """Re-draw tree with newly picked label.""" sentno = int(request.args.get('sentno')) # 1-indexed sent = SENTENCES[QUEUE[sentno - 1][0]] senttok, _ = worker.postokenize(sent) treestr = request.args.get('tree', '') try: tree, _sent1 = validate(treestr, senttok) except ValueError as err: return str(err) # FIXME: re-factor; check label AFTER replacing it # now actually replace label at nodeid _treeid, nodeid = request.args.get('nodeid', '').lstrip('t').split('_') nodeid = int(nodeid) dt = DrawTree(tree, senttok) match = LABELRE.match(dt.nodes[nodeid].label) if 'label' in request.args: label = request.args.get('label', '') dt.nodes[nodeid].label = (label + (match.group(2) or '') + (match.group(3) or '')) elif 'function' in request.args: label = request.args.get('function', '') if label == '': dt.nodes[nodeid].label = '%s%s' % (match.group(1), match.group(3) or '') else: dt.nodes[nodeid].label = '%s-%s%s' % (match.group(1), label, match.group(3) or '') elif 'morph' in request.args: label = request.args.get('morph', '') if label == '': dt.nodes[nodeid].label = '%s%s' % (match.group(1), match.group(2) or '') else: dt.nodes[nodeid].label = '%s%s/%s' % (match.group(1), match.group(2) or '', label) else: raise ValueError('expected label or function argument') tree = dt.nodes[0] dt = DrawTree(tree, senttok) # kludge.. treestr = writediscbrackettree(tree, senttok, pretty=True).rstrip() link = ('<a href="/annotate/accept?%s">accept this tree</a>' % urlencode(dict(sentno=sentno, tree=treestr))) session['actions'][RELABEL] += 1 session.modified = True return Markup('%s\n\n%s\t%s' % (link, dt.text(unicodelines=True, html=True, funcsep='-', morphsep='/', nodeprops='t0'), treestr))
def replacesubtree(): n = int(request.args.get('n', 0)) sentno = int(request.args.get('sentno')) # 1-indexed sent = SENTENCES[QUEUE[sentno - 1][0]] senttok, _ = worker.postokenize(sent) username = session['username'] treestr = request.args.get('tree', '') try: tree, _sent1 = validate(treestr, senttok) except ValueError as err: return str(err) error = '' dt = DrawTree(tree, senttok) _treeid, nodeid = request.args.get('nodeid', '').lstrip('t').split('_') nodeid = int(nodeid) subseq = sorted(dt.nodes[nodeid].leaves()) subsent = ' '.join(senttok[n] for n in subseq) root = dt.nodes[nodeid].label resp = WORKERS[username].submit(worker.getparses, subsent, (), (), root=root).result() _senttok, parsetrees, _messages, _elapsed = resp newsubtree = parsetrees[n - 1][1] pos = sorted(list(newsubtree.subtrees(lambda n: isinstance(n[0], int))), key=lambda n: n[0]) for n, a in enumerate(pos): a[0] = subseq[n] dt.nodes[nodeid][:] = newsubtree[:] tree = canonicalize(dt.nodes[0]) dt = DrawTree(tree, senttok) # kludge.. treestr = writediscbrackettree(tree, senttok, pretty=True).rstrip() session['actions'][REPARSE] += 1 session.modified = True link = ('<a href="/annotate/accept?%s">accept this tree</a>' % urlencode(dict(sentno=sentno, tree=treestr))) return Markup('%s\n\n%s%s\t%s' % (link, error, dt.text(unicodelines=True, html=True, funcsep='-', morphsep='/', nodeprops='t0'), treestr))
def reattach(): """Re-draw tree after re-attaching node under new parent.""" sentno = int(request.args.get('sentno')) # 1-indexed sent = SENTENCES[QUEUE[sentno - 1][0]] senttok, _ = worker.postokenize(sent) treestr = request.args.get('tree', '') try: tree, _sent1 = validate(treestr, senttok) except ValueError as err: return str(err) dt = DrawTree(tree, senttok) error = '' if request.args.get('newparent') == 'deletenode': # remove nodeid by replacing it with its children _treeid, nodeid = request.args.get('nodeid', '').lstrip('t').split('_') nodeid = int(nodeid) x = dt.nodes[nodeid] if nodeid == 0 or isinstance(x[0], int): error = 'ERROR: cannot remove ROOT or POS node' else: children = list(x) x[:] = [] for y in dt.nodes[0].subtrees(): if any(child is x for child in y): i = y.index(x) y[i:i + 1] = children tree = canonicalize(dt.nodes[0]) dt = DrawTree(tree, senttok) # kludge.. break elif request.args.get('nodeid', '').startswith('newlabel_'): # splice in a new node under parentid _treeid, newparent = request.args.get('newparent', '').lstrip('t').split('_') newparent = int(newparent) label = request.args.get('nodeid').split('_', 1)[1] y = dt.nodes[newparent] if isinstance(y[0], int): error = 'ERROR: cannot add node under POS tag' else: children = list(y) y[:] = [] y[:] = [Tree(label, children)] tree = canonicalize(dt.nodes[0]) dt = DrawTree(tree, senttok) # kludge.. else: # re-attach existing node at existing new parent _treeid, nodeid = request.args.get('nodeid', '').lstrip('t').split('_') nodeid = int(nodeid) _treeid, newparent = request.args.get('newparent', '').lstrip('t').split('_') newparent = int(newparent) # remove node from old parent # dt.nodes[nodeid].parent.pop(dt.nodes[nodeid].parent_index) x = dt.nodes[nodeid] y = dt.nodes[newparent] for node in x.subtrees(): if node is y: error = ('ERROR: cannot re-attach subtree' ' under (descendant of) itself\n') break else: for node in dt.nodes[0].subtrees(): if any(child is x for child in node): if len(node) > 1: node.remove(x) dt.nodes[newparent].append(x) tree = canonicalize(dt.nodes[0]) dt = DrawTree(tree, senttok) # kludge.. else: error = ('ERROR: re-attaching only child creates' ' empty node %s; remove manually\n' % node) break treestr = writediscbrackettree(tree, senttok, pretty=True).rstrip() link = ('<a href="/annotate/accept?%s">accept this tree</a>' % urlencode(dict(sentno=sentno, tree=treestr))) if error == '': session['actions'][REATTACH] += 1 session.modified = True return Markup('%s\n\n%s%s\t%s' % (link, error, dt.text(unicodelines=True, html=True, funcsep='-', morphsep='/', nodeprops='t0'), treestr))
def test_treedraw(): """Draw some trees. Only tests whether no exception occurs.""" trees = '''(ROOT (S (ADV 0) (VVFIN 1) (NP (PDAT 2) (NN 3)) (PTKNEG 4) \ (PP (APPRART 5) (NN 6) (NP (ART 7) (ADJA 8) (NN 9)))) ($. 10)) (S (NP (NN 1) (EX 3)) (VP (VB 0) (JJ 2))) (S (VP (PDS 0) (ADV 3) (VVINF 4)) (PIS 2) (VMFIN 1)) (top (du (comp 0) (smain (noun 1) (verb 2) (inf (verb 8) (inf \ (adj 3) (pp (prep 4) (np (det 5) (noun 6))) (part 7) (verb 9) \ (pp (prep 10) (np (det 11) (noun 12) (pp (prep 13) (mwu \ (noun 14) (noun 15))))))))) (punct 16)) (top (smain (noun 0) (verb 1) (inf (verb 5) (inf (np (det 2) \ (adj 3) (noun 4)) (verb 6) (pp (prep 7) (noun 8))))) (punct 9)) (top (smain (noun 0) (verb 1) (noun 2) (inf (adv 3) (verb 4))) \ (punct 5)) (top (punct 5) (du (smain (noun 0) (verb 1) (ppart (np (det 2) \ (noun 3)) (verb 4))) (conj (sv1 (conj (noun 6) (vg 7) (np \ (det 8) (noun 9))) (verb 10) (noun 11) (part 12)) (vg 13) \ (sv1 (verb 14) (ti (comp 19) (inf (np (conj (det 15) (vg 16) \ (det 17)) (noun 18)) (verb 20)))))) (punct 21)) (top (punct 10) (punct 16) (punct 18) (smain (np (det 0) (noun 1) \ (pp (prep 2) (np (det 3) (noun 4)))) (verb 5) (adv 6) (np \ (noun 7) (noun 8)) (part 9) (np (det 11) (noun 12) (pp \ (prep 13) (np (det 14) (noun 15)))) (conj (vg 20) (ppres \ (adj 17) (pp (prep 22) (np (det 23) (adj 24) (noun 25)))) \ (ppres (adj 19)) (ppres (adj 21)))) (punct 26)) (top (punct 10) (punct 11) (punct 16) (smain (np (det 0) \ (noun 1)) (verb 2) (np (det 3) (noun 4)) (adv 5) (du (cp \ (comp 6) (ssub (noun 7) (verb 8) (inf (verb 9)))) (du \ (smain (noun 12) (verb 13) (adv 14) (part 15)) (noun 17)))) \ (punct 18) (punct 19)) (top (smain (noun 0) (verb 1) (inf (verb 8) (inf (verb 9) (inf \ (adv 2) (pp (prep 3) (noun 4)) (pp (prep 5) (np (det 6) \ (noun 7))) (verb 10))))) (punct 11)) (top (smain (noun 0) (verb 1) (pp (prep 2) (np (det 3) (adj 4) \ (noun 5) (rel (noun 6) (ssub (noun 7) (verb 10) (ppart \ (adj 8) (part 9) (verb 11))))))) (punct 12)) (top (smain (np (det 0) (noun 1)) (verb 2) (ap (adv 3) (num 4) \ (cp (comp 5) (np (det 6) (adj 7) (noun 8) (rel (noun 9) (ssub \ (noun 10) (verb 11) (pp (prep 12) (np (det 13) (adj 14) \ (adj 15) (noun 16))))))))) (punct 17)) (top (smain (np (det 0) (noun 1)) (verb 2) (adv 3) (pp (prep 4) \ (np (det 5) (noun 6)) (part 7))) (punct 8)) (top (punct 7) (conj (smain (noun 0) (verb 1) (np (det 2) \ (noun 3)) (pp (prep 4) (np (det 5) (noun 6)))) (smain \ (verb 8) (np (det 9) (num 10) (noun 11)) (part 12)) (vg 13) \ (smain (verb 14) (noun 15) (pp (prep 16) (np (det 17) \ (noun 18) (pp (prep 19) (np (det 20) (noun 21))))))) \ (punct 22)) (top (smain (np (det 0) (noun 1) (rel (noun 2) (ssub (np (num 3) \ (noun 4)) (adj 5) (verb 6)))) (verb 7) (ppart (verb 8) (pp \ (prep 9) (noun 10)))) (punct 11)) (top (conj (sv1 (np (det 0) (noun 1)) (verb 2) (ppart (verb 3))) \ (vg 4) (sv1 (verb 5) (pp (prep 6) (np (det 7) (adj 8) \ (noun 9))))) (punct 10)) (top (smain (noun 0) (verb 1) (np (det 2) (noun 3)) (inf (adj 4) \ (verb 5) (cp (comp 6) (ssub (noun 7) (adv 8) (verb 10) (ap \ (num 9) (cp (comp 11) (np (det 12) (adj 13) (noun 14) (pp \ (prep 15) (conj (np (det 16) (noun 17)) (vg 18) (np \ (noun 19))))))))))) (punct 20)) (top (punct 8) (smain (noun 0) (verb 1) (inf (verb 5) \ (inf (verb 6) (conj (inf (pp (prep 2) (np (det 3) (noun 4))) \ (verb 7)) (inf (verb 9)) (vg 10) (inf (verb 11)))))) \ (punct 12)) (top (smain (verb 2) (noun 3) (adv 4) (ppart (np (det 0) \ (noun 1)) (verb 5))) (punct 6)) (top (conj (smain (np (det 0) (noun 1)) (verb 2) (adj 3) (pp \ (prep 4) (np (det 5) (noun 6)))) (vg 7) (smain (np (det 8) \ (noun 9) (pp (prep 10) (np (det 11) (noun 12)))) (verb 13) \ (pp (prep 14) (np (det 15) (noun 16))))) (punct 17)) (top (conj (smain (noun 0) (verb 1) (inf (ppart (np (noun 2) \ (noun 3)) (verb 4)) (verb 5))) (vg 6) (smain (noun 7) \ (inf (ppart (np (det 8) (noun 9)))))) (punct 10)) (A (B1 (t 6) (t 13)) (B2 (t 3) (t 7) (t 10)) (B3 (t 1) \ (t 9) (t 11) (t 14) (t 16)) (B4 (t 0) (t 5) (t 8))) (A (B1 6 13) (B2 3 7 10) (B3 1 \ 9 11 14 16) (B4 0 5 8)) (VP (VB 0) (PRT 2)) (VP (VP 0 3) (NP (PRP 1) (NN 2))) (ROOT (S (VP_2 (PP (APPR 0) (ART 1) (NN 2) (PP (APPR 3) (ART 4) \ (ADJA 5) (NN 6))) (ADJD 10) (PP (APPR 11) (NN 12)) (VVPP 13)) \ (VAFIN 7) (NP (ART 8) (NN 9))) ($. 14))''' sents = '''Leider stehen diese Fragen nicht im Vordergrund der \ augenblicklichen Diskussion . is Mary happy there das muss man jetzt machen Of ze had gewoon met haar vriendinnen rond kunnen slenteren in de \ buurt van Trafalgar Square . Het had een prachtige dag kunnen zijn in Londen . Cathy zag hen wild zwaaien . Het was een spel geworden , zij en haar vriendinnen kozen iemand \ uit en probeerden zijn of haar nationaliteit te raden . Elk jaar in het hoogseizoen trokken daar massa's toeristen \ voorbij , hun fototoestel in de aanslag , pratend , gillend \ en lachend in de vreemdste talen . Haar vader stak zijn duim omhoog alsof hij wilde zeggen : " het \ komt wel goed , joch " . Ze hadden languit naast elkaar op de strandstoelen kunnen gaan \ liggen . Het hoorde bij de warme zomerdag die ze ginds achter had gelaten . De oprijlaan was niet meer dan een hobbelige zandstrook die zich \ voortslingerde tussen de hoge grijze boomstammen . Haar moeder kleefde bijna tegen het autoraampje aan . Ze veegde de tranen uit haar ooghoeken , tilde haar twee koffers \ op en begaf zich in de richting van het landhuis . Het meisje dat vijf keer juist raadde werd getrakteerd op ijs . Haar neus werd platgedrukt en leek op een jonge champignon . Cathy zag de BMW langzaam verdwijnen tot hij niet meer was dan \ een zilveren schijnsel tussen de bomen en struiken . Ze had met haar moeder kunnen gaan winkelen , zwemmen of \ terrassen . Dat werkwoord had ze zelf uitgevonden . De middagzon hing klein tussen de takken en de schaduwen van de \ wolken drentelden over het gras . Zij zou mams rug ingewreven hebben en mam de hare . 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Mit einer Messe in der Sixtinischen Kapelle ist das Konklave \ offiziell zu Ende gegangen .''' from discodop.tree import DrawTree trees = [Tree(a) for a in trees.splitlines()] sents = [a.split() for a in sents.splitlines()] sents.extend([['Wake', None, 'up'], [None, 'your', 'friend', None]]) for n, (tree, sent) in enumerate(zip(trees, sents)): drawtree = DrawTree(tree, sent) print('\ntree, sent', n, tree, ' '.join('...' if a is None else a for a in sent), repr(drawtree), sep='\n') try: print(drawtree.text(unicodelines=True, ansi=True), sep='\n') except (UnicodeDecodeError, UnicodeEncodeError): print(drawtree.text(unicodelines=False, ansi=False), sep='\n')