Linux系统如何使用SyntaxNet输出来操作执行命令,例如将文件保存在文件夹中 [英] How to use SyntaxNet output to operate an executive command ,for example save a file in a folder, on Linux system

查看:32
本文介绍了Linux系统如何使用SyntaxNet输出来操作执行命令,例如将文件保存在文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下载并训练了

  1. output.txt作为输入文件,使用下面的python算法分析SyntaxNet输出,从LibreOffice包中识别出你想要的目标应用程序的文件名和用户想要的命令使用.

#!/bin/sh

导入csv导入子流程导入系统导入操作系统#get SyntaxNet 输出作为 Python 算法输入文件文件名='/home/username/models/syntaxnet/work/output.txt'#所有可能的执行命令,用于使用 Libreoffice 文件打开任何格式的任何文件命令={('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'),}#Libreoffice 应用程序的所有可能同义词评论={'作家':['词','文字','作家'],'计算器': ['excel','calc','calculator'],'画':['画','画','画'],'impress': ['powerpoint','impress'],'数学':['数学','计算器','数学'],'全球':['全球'],'web': ['html','web'],'show':['演示','show']}root ='ROOT' #ROOT的句子noun='NOUN' #名词标注器动词='VERB' #动词标注器adjmod='amod' #形容词修饰符dirobj='dobj' #直接目标apposmod='appos' # 同位修饰符prepos_obj='pobj' # 介词目标app='libreoffice' # 包名介词='prep' # 介词noun_modi='nn' # 名词修饰符#从 Syntaxnet 输出制表符分隔的文本文件中读取定义读取数据(文件名):文件=打开(文件名,'r')行=file.readlines()线=线[:-1]数据= csv.reader(行,分隔符='\ t')大声笑=列表(数据)回来哈哈# 标识动作、文件名以及用户是否隐含地提到了应用程序的名称def exe(root,noun,verb,adjmod,dirobj,apposmod,commands,noun_modi):解释=空"列表=读取数据(文件名)对于列表中的子列表:if sublist[7]==root and sublist[3]==verb: # 当 ROOT 是动词时,dobj 可能是你想要的文件名动作=子列表[1]dep_num=子列表[0]对于列表中的子列表:如果 sublist[6]==dep_num 和 sublist[7]==dirobj:direct_object=子列表[1]dep_num=子列表[0]dep_num_obj=子列表[0]对于列表中的子列表:如果 direct_object=='file' 和 sublist[6]==dep_num_obj 和 sublist[7]==apposmod:direct_object=子列表[1]elif direct_object=='file' and sublist[6]==dep_num_obj and sublist[7]==adjmod:direct_object=子列表[1]对于列表中的子列表:如果 sublist[6]==dep_num_obj 和 sublist[7]==adjmod:对于key,v 在comments.iteritems() 中:如果 v 中的子列表 [1]:解释=键对于列表中的子列表:如果 sublist[6]==dep_num_obj 和 sublist[7]==noun_modi:dep_num_nn=子列表[0]对于key,v 在comments.iteritems() 中:如果 v 中的子列表 [1]:解释=键印刷解释如果解释=='null':对于列表中的子列表:如果 sublist[6]==dep_num_nn 和 sublist[7]==noun_modi:对于key,v 在comments.iteritems() 中:如果 v 中的子列表 [1]:解释=键elif sublist[7]==root and sublist[3]==noun: # 你必须找到形容词形式的单词,并且依赖于词根dep_num=子列表[0]dep_num_obj=子列表[0]direct_object=子列表[1]对于列表中的子列表:如果 sublist[6]==dep_num 和 sublist[7]==adjmod:actionis=any(t1==sublist[1] for (t1, t2, t3) 在命令中)如果动作是==真:动作=子列表[1]elif sublist[6]==dep_num 和 sublist[7]==noun_modi:dep_num=子列表[0]对于列表中的子列表:如果 sublist[6]==dep_num 和 sublist[7]==adjmod:如果有(t1==sublist[1] for (t1, t2, t3) in commands):动作=子列表[1]对于列表中的子列表:if direct_object=='file' and sublist[6]==dep_num_obj and sublist[7]==apposmod and sublist[1]!=action:direct_object=子列表[1]if direct_object=='file' and sublist[6]==dep_num_obj and sublist[7]==adjmod and sublist[1]!=action:direct_object=子列表[1]对于列表中的子列表:如果 sublist[6]==dep_num_obj 和 sublist[7]==noun_modi:dep_num_obj=子列表[0]对于key,v 在comments.iteritems() 中:如果 v 中的子列表 [1]:解释=键别的:对于列表中的子列表:如果 sublist[6]==dep_num_obj 和 sublist[7]==noun_modi:对于key,v 在comments.iteritems() 中:如果 v 中的子列表 [1]:解释=键返回动作,direct_object,解释动作,direct_object,interprete = exe(root,名词,动词,adjmod,dirobj,apposmod,commands,noun_modi)# 找到应用程序(我们假设我们知道用户想要使用 libreoffice 但我们不知道应该使用哪个子应用程序)定义应用程序(应用程序,prepos_obj,介词,名词莫迪):列表=读取数据(文件名)subapp='未提及'对于列表中的子列表:如果子列表[1]==应用程序:dep_num=子列表[6]对于列表中的子列表:如果 sublist[0]==dep_num 和 sublist[7]==prepos_obj:actioni=any(t3==sublist[1] for (t1, t2, t3) 在命令中)如果动作i==真:子应用=子列表[1]别的:对于列表中的子列表:如果 sublist[6]==dep_num 和 sublist[7]==noun_modi:actioni=any(t3==sublist[1] for (t1, t2, t3) 在命令中)如果动作i==真:子应用=子列表[1]elif sublist[0]==dep_num 和 sublist[7]==介词:子列表[6]=dep_num对于列表中的子行:如果 subline[0]==dep_num 和 subline[7]==prepos_obj:如果有(t3==sublist[1] for (t1, t2, t3) in commands):子应用=子列表[1]别的:对于列表中的子行:如果 subline[0]==dep_num 和 subline[7]==noun_modi:如果有(t3==sublist[1] for (t1, t2, t3) in commands):子应用=子列表[1]返回子应用程序sub_application=application(app,prepos_obj,preposition,noun_modi)如果 sub_application=='未提及'并解释!='null':sub_application=解释elif sub_application=='未提及'和解释=='null':sub_application=解释#文件格式def format_function(sub_application):子应用程序=子应用程序Dobj=exe(root,noun,verb,adjmod,dirobj,apposmod,commands,noun_modi)[1]如果 subapp!='null':如果子应用=='作者':a='.odt'Dobj=Dobj+aelif subapp=='计算器':a='.ods'Dobj=Dobj+aelif subapp=='印象':a='.odp'Dobj=Dobj+aelif subapp=='绘制':a='.odg'Dobj=Dobj+aelif subapp=='数学':a='.odf'Dobj=Dobj+aelif subapp=='数学':a='.odf'Dobj=Dobj+aelif 子应用=='网络':a='.html'Dobj=Dobj+a别的:Dobj='空'返回 Dobjdef get_filepaths(目录):myfile=format_function(sub_application)file_paths = [] # 将存储所有完整文件路径的列表.# 走树.对于 os.walk(directory) 中的根、目录、文件:对于文件中的文件名:# 连接两个字符串以形成完整的文件路径.如果文件名==我的文件:文件路径 = os.path.join(root, 文件名)file_paths.append(filepath) # 将其添加到列表中.return file_paths # 不言自明.# 运行上面的函数并将其结果存储在一个变量中.full_file_paths = get_filepaths("/home/ubuntu/")如果 full_file_paths==[]:打印未找到名为 %s 的文件"% format_function(sub_application)如果 full_file_paths!=[]:路径=full_file_paths提示='>'如果 len(full_file_paths) > 1:打印 full_file_paths打印您指的是哪个 %s?"% subapp输入名称=原始输入(提示)如果输入名称在 full_file_paths 中:路径=输入名称#主要代码结构如果 sub_application!='null':命令=命令[动作,应用程序,子应用程序]subprocess.call([command[0],command[1],path[0]])别的:打印子应用程序没有明确提到"

我再次说我是初学者,代码可能看起来不那么整洁或专业,但我只是尝试使用我对这个迷人的所有知识SyntaxNet 到实用的算法.这个简单的算法可以打开文件:

  1. 使用 LibreOffice 支持的任何格式,例如.odt,.odf,.ods,.html,.odp.

  2. 可以理解LibreOffice中不同应用的隐含引用,例如:用libreoffice打开文本文件簿"而不是用libreoffice writer打开文件簿"

  3. 可以克服 SyntaxNet 解释被称为形容词的文件名的问题.

having downloaded and trained SyntaxNet, I am trying to write a program that can open new/existed files, for example AutoCAD files, and save the files in an specific directory by analyzing the text: open LibreOffice file X . considering the output of SyntaxNet as:

echo "save AUTOCAD file  X in directory Y" | ./test.sh > output.txt


Input: save AUTOCAD file X in directory Y
Parse:
save VB ROOT
 +-- X NNP dobj
 |   +-- file NN compound
 |       +-- AUTOCAD CD nummod
 +-- directory NN nmod
     +-- in IN case
     +-- Y CD nummod

First I thought about changing the parsed text to XML format then parse the XML file with semantic analyses (like SPARQL) to find ROOT=save, dobj=X, and nummode=Y and write a python program that can do the same thing which is said in the text

  1. I don’t know that if I change the parsed text to XML and then use semantic analyses that uses querying in order to match ROOT with its counterpart function or script that saves dobj, in a directory with mention in nummode

  2. I have some ideas to connect python to the terminal with subprocesspackage but I didn't find anything that can help me saving for example an AUTOCAD file, or any other files, from the terminal or do I need to write a script, .sh, with the help of python?

I did a huge research on syntactic and semantic analyses on texts, such as Christian Chiarcos, 2011, Hunter and Cohen, 2006 and Verspoor et al., 2015, and also studied Microsoft Cortana, Sirius, google now but none of them went through the detail of how they change a parsed text to a executive command which brought me to the conclusion that this work is too easy to be talked about but as I am none computer science major I cannot figure out what I can do about it.

解决方案

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.

这篇关于Linux系统如何使用SyntaxNet输出来操作执行命令,例如将文件保存在文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆