How to use SyntaxNet output to operate an executive command ,for example save a file in a folder, on Linux system

我怕爱的太早我们不能终老 提交于 2019-11-29 08:46:55

I am a beginner in Computer Science World and SyntaxNet. I wrote a simple SyntaxNet-Python algorithm which used SyntaxNet to analyze a text command a user inserts,"open the file book which I have written with laboratory writer with LibreOffice writer", and then analyzes SyntaxNet output with a python algorithm in order to turn it to an executive command, in this case open a file, with any supported format, with LibreOffice in Linux, Ubuntu 14.04) environment. you can see here the different command lines defined by LibreOffice in order to use different application in this package.

  1. After installing and running SyntaxNet (the installation process in explained here),the shell script is opened demo.sh in ~/models/syntaxnet/suntaxnet/ directory and the conl2tree function (line 54 to 56) is erased in order to get a tab delimited output from SyntaxNet instead of a tree format output.

  2. This command is typed in the terminal window:

    echo 'open the file book which I have writtern with the laboratory writer with libreOffice writer' | syntaxnet/demo.sh > output.txt

the output.txt document is saved in the directory where demo.sh exists and it will be somehow like the below figure:

  1. The output.txt as the input file and use the below python algorithm to analyze SyntaxNet output and identifies the name of the file you want the target application from LibreOffice package and the command the user wants to use.

#!/bin/sh

import csv

import subprocess

import sys

import os

#get SyntaxNet output as the Python algorithm input file
filename='/home/username/models/syntaxnet/work/output.txt'

#all possible executive commands for opening any file with any format with Libreoffice file
commands={
('open',  'libreoffice',  'writer'):  ('libreoffice', '--writer'),
('open',  'libreoffice',  'calculator'):  ('libreoffice' ,'--calc'),
('open',  'libreoffice',  'draw'):  ('libreoffice' ,'--draw'),
('open',  'libreoffice',  'impress'): ('libreoffice' ,'--impress'),
('open',  'libreoffice',  'math'):  ('libreoffice' ,'--math'),
('open',  'libreoffice',  'global'):  ('libreoffice' ,'--global'),
('open',  'libreoffice',  'web'): ('libreoffice' ,'--web'),
('open',  'libreoffice',  'show'):  ('libreoffice', '--show'),
}
#all of the possible synonyms of the application from Libreoffice 
comments={
 'writer': ['word','text','writer'],
 'calculator': ['excel','calc','calculator'],
 'draw': ['paint','draw','drawing'],
 'impress': ['powerpoint','impress'],
 'math': ['mathematic','calculator','math'],
 'global': ['global'],
 'web': ['html','web'],
 'show':['presentation','show']
 }

root ='ROOT'            #ROOT of the senctence
noun='NOUN'             #noun tagger
verb='VERB'             #verb tagger
adjmod='amod'           #adjective modifier
dirobj='dobj'           #direct objective
apposmod='appos'        # appositional modifier
prepos_obj='pobj'       # prepositional objective
app='libreoffice'       # name of the package
preposition='prep'      # preposition
noun_modi='nn'          # noun modifier 

#read from Syntaxnet output tab delimited textfile
def readata(filename):
    file=open(filename,'r')
    lines=file.readlines()
    lines=lines[:-1]
    data=csv.reader(lines,delimiter='\t')
    lol=list(data)
    return  lol

# identifies the action, the name of the file and whether the user mentioned the name of the application implicitely  
def exe(root,noun,verb,adjmod,dirobj,apposmod,commands,noun_modi):
    interprete='null'
    lists=readata(filename)
    for sublist in lists:
        if sublist[7]==root and sublist[3]==verb: # when the ROOT is verb the dobj is probably the name of the file you want to have
                action=sublist[1]
                dep_num=sublist[0]
                for sublist in lists:
                    if sublist[6]==dep_num and sublist[7]==dirobj:
                        direct_object=sublist[1]
                        dep_num=sublist[0]
                        dep_num_obj=sublist[0]
                        for sublist in lists:
                            if direct_object=='file' and sublist[6]==dep_num_obj and sublist[7]==apposmod:
                                direct_object=sublist[1]
                            elif  direct_object=='file' and sublist[6]==dep_num_obj and sublist[7]==adjmod:
                                direct_object=sublist[1]
                for sublist in lists:
                    if sublist[6]==dep_num_obj and sublist[7]==adjmod:
                            for key, v in  comments.iteritems():
                                if sublist[1] in v:
                                    interprete=key
                for sublist in lists:
                    if sublist[6]==dep_num_obj and sublist[7]==noun_modi:
                        dep_num_nn=sublist[0]
                        for key, v in  comments.iteritems():
                            if sublist[1] in v:
                                interprete=key
                                print interprete
                        if interprete=='null':
                            for sublist in lists:
                                if sublist[6]==dep_num_nn and sublist[7]==noun_modi:
                                    for key, v in  comments.iteritems():
                                        if sublist[1] in v:
                                            interprete=key
        elif  sublist[7]==root and sublist[3]==noun: # you have to find the word which is in a adjective form and depends on the root
            dep_num=sublist[0]
            dep_num_obj=sublist[0]
            direct_object=sublist[1]
            for sublist in lists:
                if sublist[6]==dep_num and sublist[7]==adjmod:
                    actionis=any(t1==sublist[1] for (t1, t2, t3) in commands)
                    if actionis==True:
                        action=sublist[1]
                elif sublist[6]==dep_num and sublist[7]==noun_modi:
                    dep_num=sublist[0]
                    for sublist in lists:
                        if sublist[6]==dep_num and sublist[7]==adjmod:
                            if any(t1==sublist[1] for (t1, t2, t3) in commands):
                                action=sublist[1]
            for sublist in lists:
                if direct_object=='file' and sublist[6]==dep_num_obj and sublist[7]==apposmod and sublist[1]!=action:
                    direct_object=sublist[1]
                if  direct_object=='file' and sublist[6]==dep_num_obj and sublist[7]==adjmod and sublist[1]!=action:
                    direct_object=sublist[1]
            for sublist in lists:
                if sublist[6]==dep_num_obj and sublist[7]==noun_modi:
                    dep_num_obj=sublist[0]
                    for key, v in  comments.iteritems():
                        if sublist[1] in v:
                            interprete=key
                        else:
                            for sublist in lists:
                                if sublist[6]==dep_num_obj and sublist[7]==noun_modi:
                                    for key, v in  comments.iteritems():
                                        if sublist[1] in v:
                                            interprete=key
    return action, direct_object, interprete

action, direct_object, interprete = exe(root,noun,verb,adjmod,dirobj,apposmod,commands,noun_modi)

# find the application (we assume we know user want to use libreoffice but we donot know what subapplication should be used)
def application(app,prepos_obj,preposition,noun_modi):
    lists=readata(filename)
    subapp='not mentioned'
    for sublist in lists:
        if sublist[1]==app:
            dep_num=sublist[6]
            for sublist in lists:
                if sublist[0]==dep_num and sublist[7]==prepos_obj:
                    actioni=any(t3==sublist[1] for (t1, t2, t3) in commands)
                        if actioni==True:
                            subapp=sublist[1]
                        else:
                            for sublist in lists:
                                if sublist[6]==dep_num and sublist[7]==noun_modi:
                                    actioni=any(t3==sublist[1] for (t1, t2, t3) in commands)
                                    if actioni==True:
                                        subapp=sublist[1]
                        elif sublist[0]==dep_num and sublist[7]==preposition:
                            sublist[6]=dep_num
                            for subline in lists:
                                if subline[0]==dep_num and subline[7]==prepos_obj:
                                    if any(t3==sublist[1] for (t1, t2, t3) in commands):
                                        subapp=sublist[1]
                                    else:
                                        for subline in lists:
                                            if subline[0]==dep_num and subline[7]==noun_modi:
                                                if any(t3==sublist[1] for (t1, t2, t3) in commands):
                                                    subapp=sublist[1]
    return subapp

sub_application=application(app,prepos_obj,preposition,noun_modi)

if sub_application=='not mentioned' and interprete!='null':
    sub_application=interprete
elif sub_application=='not mentioned' and interprete=='null':
    sub_application=interprete

# the format of file
def format_function(sub_application):
    subapp=sub_application
    Dobj=exe(root,noun,verb,adjmod,dirobj,apposmod,commands,noun_modi)[1]
    if subapp!='null':
        if subapp=='writer':
            a='.odt'
            Dobj=Dobj+a
        elif subapp=='calculator':
            a='.ods'
            Dobj=Dobj+a
        elif subapp=='impress':
            a='.odp'
            Dobj=Dobj+a
        elif subapp=='draw':
            a='.odg'
            Dobj=Dobj+a
        elif subapp=='math':
            a='.odf'
            Dobj=Dobj+a
        elif subapp=='math':
            a='.odf'
            Dobj=Dobj+a
        elif subapp=='web':
            a='.html'
            Dobj=Dobj+a
    else:
        Dobj='null'
    return Dobj

def get_filepaths(directory):
    myfile=format_function(sub_application)
    file_paths = []  # List which will store all of the full filepaths.
    # Walk the tree.
    for root, directories, files in os.walk(directory):
        for filename in files:
        # Join the two strings in order to form the full filepath.
            if filename==myfile:
                filepath = os.path.join(root, filename)
                file_paths.append(filepath)  # Add it to the list.
    return file_paths  # Self-explanatory.

# Run the above function and store its results in a variable.
full_file_paths = get_filepaths("/home/ubuntu/")

if full_file_paths==[]:
    print 'No file with name %s is found' % format_function(sub_application)
if full_file_paths!=[]:
    path=full_file_paths
    prompt='> '
    if len(full_file_paths) >1:
        print full_file_paths
        print 'which %s do you mean?'% subapp
        inputname=raw_input(prompt)
        if inputname in full_file_paths:
            path=inputname
        #the main code structure
    if sub_application!='null':
        command= commands[action,app,sub_application]
        subprocess.call([command[0],command[1],path[0]])
    else:
        print "The sub application is not mentioned clearly"

I again say I am a beginner and the code might not seems so tidied up or professional but I just tried to use all my knowledge about this fascinating SyntaxNet to a practical algorithm. This simple algorithm can open the file:

  1. with any format which is supported by LibreOffice e.g. .odt,.odf,.ods,.html,.odp.

  2. it can understand implicit reference of different application in LibreOffice, for example: " open the text file book with libreoffice" instead of "open the file book with libreoffice writer"

  3. can overcome the problem of SyntaxNet interpreting the name of the files which are referred as an adjective.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!