示例#1
0
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(
        'python 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]

    # parser = TransitionParser('arc-eager')
    # with open('models/parser.pkl','rb') as in_file:
    #     parser = pickle.load(in_file)
    # predictions = parser.parse([dg],'models/arc_eager.model')
    # txt = predictions[0].to_conll(4)
    err = False
    try:
        out = DependencyGraph(txt)
        out_dot = out.to_dot()
        G = pgv.AGraph(out_dot)
        G.layout(prog='dot')  # use dot
        G.draw('static/process.png')
    except:
        err = True
        txt += '''Error generating graph.\n'''
    return txt, err