|
|
from conllu import parse_incr, parse |
|
|
from nltk.parse import DependencyGraph, DependencyEvaluator |
|
|
from nltk.parse.transitionparser import TransitionParser |
|
|
import pickle |
|
|
import pygraphviz as pgv |
|
|
from test_hn_pos import test_fn |
|
|
import os |
|
|
|
|
|
def Process(sentence): |
|
|
words = sentence.replace('|','।').split() |
|
|
tags = test_fn(words) |
|
|
text = [] |
|
|
i = 0 |
|
|
for word, tag in zip(words,tags): |
|
|
i += 1 |
|
|
fill = '_' |
|
|
text.append('\t'.join([str(i),word,fill,fill,fill,fill,fill,fill,fill,fill])) |
|
|
dg = DependencyGraph('\n'.join(text)) |
|
|
text = '\n'.join(text) |
|
|
text = text + '\n\n' + text |
|
|
with open('biaffine-parser-master/data/naive3.conllx','w') as f: |
|
|
f.write(text) |
|
|
os.chdir('biaffine-parser-master') |
|
|
os.system('python3.7 run.py predict --feat=bert --fdata=data/naive3.conllx --fpred=data/naive3.conllx') |
|
|
txt = '' |
|
|
os.chdir('..') |
|
|
with open('biaffine-parser-master/data/naive3.conllx','r') as f: |
|
|
txt = f.read().split('\n\n')[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = False |
|
|
try: |
|
|
out = DependencyGraph(txt) |
|
|
out_dot = out.to_dot() |
|
|
G = pgv.AGraph(out_dot) |
|
|
G.layout(prog='dot') |
|
|
G.draw('static/process.png') |
|
|
except: |
|
|
err = True |
|
|
txt += '''Error generating graph.\n''' |
|
|
return txt, err |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def DepGraphList(sentenceList): |
|
|
dgList = [] |
|
|
i = 0 |
|
|
j = 0 |
|
|
for sentence in sentenceList: |
|
|
text = [] |
|
|
for token in sentence: |
|
|
text.append(' '.join([token['form'],token['upostag'],str(token['head']),token['deprel'].upper()])) |
|
|
try: |
|
|
dg = DependencyGraph('\n'.join(text)) |
|
|
except: |
|
|
j += 1 |
|
|
continue |
|
|
i += 1 |
|
|
dgList.append(dg) |
|
|
print(i,j) |
|
|
return dgList |
|
|
|
|
|
def main(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Process('राम अच्छा पुरुष है |') |
|
|
return |
|
|
|
|
|
if __name__=='__main__': |
|
|
main() |
|
|
|