本文介绍了从解析器的输出找到依赖关系的流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在门口使用了解析器来获得依赖。从这个依赖关系,我找到了依赖关系对和依赖名称的单独列表。



我的输入是



String idlist = [26,28]



String argslist = [[26,25],[26,24],[26,29],[26,28] ,[28,27],[35,26],[29,30]]



String kindlist = [aux,dobj,pobj,prep,advmod,aux, adj]



我的预期输出是两个列表



Depflowlist是第一个,另一个是kindflowlist。



对于26本身它有不同的流程。
为26预期的depflowlist是[26,25]和其相应的kindflowlist [aux]



然后[26,24]是另一个depflowlist为26和其对应kindflowlist [dobj]



然后[[26,29],[29,30]]是另一个用于26的depflowlist及其对应的kindflowlist [pobj,adj]。 >
这也应该为28。



我写了代码如下:

 `public static void dependencyparse(String a,ArrayList< String> argslist,ArrayList< String> kindlist,Document doc){

ArrayList< String> selectarg_list = new ArrayList< String>(); (int j = 0; j< argslist.size(); j ++)
{
int index = argslist.get(j).indexOf(a);
if(index == 1)
{
String next = argslist.get(j).substring(5,7);
selectarg_list.add(next);
}
}


System.out.println(a +---------->+ selectarg_list);

if(!selectarg_list.isEmpty())
{
// System.out.println(notempty);
hm.put(a,selectarg_list);
}
//System.out.println(\"Hashmap\"+hm);

if(selectarg_list.size()> 0){

dependencyparse(selectarg_list.get(0),argslist,kindlist,doc);

}

if(selectarg_list.size()== 0){
String prevKey = delete(a,selectarg_list,argslist,kindlist,doc);
System.out.println(prevKey);


if(!hm.isEmpty()){
if(hm.keySet()。contains(prevKey)){
ArrayList< String> list = hm.get(prevKey);
dependencyparse(list.get(0),argslist,kindlist,doc);
}
else {
if(!hm.isEmpty()){
try {
dependencyparse(hm.values()。iterator() .get(0),argslist,kindlist,doc);
} catch(异常e){}
}
}
迭代器< String> itr3 = hm.keySet()。iterator();
while(itr3.hasNext()){
String string =(String)itr3.next();
System.out.println(string +------------>+ hm.get(string));
}
}


}


}


私人静态字符串delete(String a,ArrayList< String> selectarg_list,ArrayList< String> argslist,ArrayList< String> kindlist,Document doc){
AnnotationSet outputAnnotations = doc.getNamedAnnotationSets()get(Output markups

String retKey =;
集合< ArrayList< String>> values = hm.values();
String key =;
//System.out.println(\"Valuess\"+values);
//System.out.println(\"find\"+a);
boolean flag = false;
迭代器< ArrayList< String>> Valueitr = values.iterator();
while(Valueitr.hasNext())
{
//System.out.println(\"Valuess ---->+ Valueitr.next());

ArrayList< String> contentnext = Valueitr.next();
if(contentnext.contains(a))
{
flag = true;

//System.out.println(\"Valuess find ---->+ contentnext);
for(Entry< String,ArrayList< String>>条目:hm.entrySet())
{
if(contentnext.equals(entry.getValue()))
{
key = entry.getKey();
// System.out.println(Key find ---->+ key);
//使用键
进行某些操作String arg =[+ key +,+ a +];
// System.out.println(arg find ---->+ arg);
depFlowList.addFirst(arg);
// System.out.println(entry find ---->+ entry.getValue());
try {
if(!hm.keySet()。contains(a)){
entry.getValue()。remove(a);
// hm.put(entry.getKey(),value)
// System.out.println(entry.getKey()+------------- ---+ hm.get(entry.getKey())+\\\
entry delete ---->+ entry.getValue());
}} catch(Exception e){}



}
}

}

$

if(!flag){
if(!depFlowList.isEmpty()){
System.out.println(Dep Flow List ----- >中+ depFlowList); (int x = 0; x< depFlowList.size(); x ++){
String item = depFlowList.get(x);


int item_index = argslist.indexOf(item);
String item_kind = kindlist.get(item_index);
System.out.println(add kind of -----+ depFlowList.get(x));
kindFlowList.add(item_kind);
}

System.out.println(Kind Flow List ----->+ kindFlowList);
file_write(/ home / cognicor / vagateplugin / testing / dependency.txt,kindFlowList.toString());
}

if(!kindFlowList.isEmpty()){
FeatureMap depfeature = new SimpleFeatureMapImpl();
depfeature.put(depflow,kindFlowList);
try {
outputAnnotations.add(new Long(0),new Long(1),DependencyFlow,depfeature);
} catch(InvalidOffsetException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}
if(depFlowList.size()== 0){
return;
}
retKey = depFlowList.getLast()。substring(1,3);
depFlowList.clear();
kindFlowList.clear();
System.out.println(retKey);

return retKey;
}

//System.out.println(\"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++);

ArrayList< String> findval = hm.get(key);
if(findval.isEmpty()){
hm.remove(key);
//System.out.println(\" key removed);
}

//if(!hm.isEmpty()){
retKey = delete(key,selectarg_list,argslist,kindlist,doc);
return retKey;

//}


}






public static void main(String [] args){
// TODO自动生成的方法stub

ArrayList< String> testlist = file_read(/ home / cognicor / vagateplugin / testing / test_set1.txt);
//System.out.println(testlist);
ArrayList< String> query_class = new ArrayList< String>();
ArrayList< String> property_class = new ArrayList< String>();
迭代器< String> testinput = testlist.iterator();
字符串条目;
System.out.println(testlist ---->+ testlist); (int i = 0; i< testlist.size(); i ++)
{
try {
ArrayList< Integer> idlist = new ArrayList< Integer>();
ArrayList< String> argslist = new ArrayList< String>();
ArrayList< String> kindlist = new ArrayList< String>();
String query =< currDial>+ testlist.get(i)+< / currDial>;
System.out.println(query);
文档doc = pipline.get_Annoted_Doument(query);
/ *代码,我得到的输入像idlist,arglist和kindlist * /
AnnotationSet annotset = doc.getNamedAnnotationSets()。get(Output markups)。get(TokenID);
迭代器<注释> itr = annotset.iterator();
while(itr.hasNext())
{
注释注释= itr.next();
int id =(int)annot.getFeatures()。get(id);
idlist.add(id);
}
AnnotationSet注释集1 = doc.getNamedAnnotationSets()。get(输出标记)。get(Dependency);
迭代器<注释> itr1 = annotset1.iterator();
while(itr1.hasNext())
{
注释注释= itr1.next();
String arg = annot.getFeatures()。get(args)。toString();
String kind = annot.getFeatures()。get(kind)。toString();
argslist.add(arg);
kindlist.add(kind);
}
System.out.println(args+ argslist); (int j = 0; j< idlist.size(); j ++)

System.out.println(--------- ----------------------项目: + idlist.get(J)的ToString()+ ------------ ------------------------------);
dependencyparse(idlist.get(j).toString(),argslist,kindlist,doc);
if(j == idlist.size())
{
dependencyparse(null,argslist,kindlist,doc);
}
}

} catch(Exception e){e.printStackTrace(); }
}

}
}`

现在我有一个没有结束的循环。我正在使用门的输出。

解决方案

这是我的代码。它在Java 7 + 。请注意,我还打电话给Stanford Parser,并直接从注释中读取输入(不需要String解析)。



解析的句子来自。

  import gate。*; 
import gate.creole.SerialAnalyserController;
import java.io.File;
import java.util。*;
import com.google.common.collect.ArrayListMultimap;

public class DepflowFinder {
public static void main(String [] args)throws异常{
Gate.setGateHome(new File(C:\\Program Files\\ \\\GATE_Developer_8.1\" ));
Gate.init();
regiterGatePlugin(ANNIE); regiterGatePlugin(Stanford_CoreNLP/ *Parser_Stanford* /);

SerialAnalyserController pipeline =(SerialAnalyserController)Factory.createResource(gate.creole.SerialAnalyserController);
pipeline.add((ProcessingResource)Factory.createResource(gate.creole.tokeniser.DefaultTokeniser));
pipeline.add((ProcessingResource)Factory.createResource(gate.creole.splitter.SentenceSplitter));
pipeline.add((ProcessingResource)Factory.createResource(gate.stanford.Parser));

语料库语料库= Factory.newCorpus(DepflowCorpus);
文件文件= Factory.newDocument(港口和移民的帐单由堪萨斯共和国参议员布朗克提交。
corpus.add(document); pipeline.setCorpus(语料库); pipeline.execute();

AnnotationSet defaultAS = document.getAnnotations();
DepflowFinder finder = new DepflowFinder(defaultAS.get(Dependency));
finder.findDepflowsFor(defaultAS.get(Token,Utils.featureMap(string,submitted))。iterator()。next()。getId()); (int i = 0; i< finder.getNumDepflows(); i ++){
System.err.println(finder.getDepflow(i));


System.err.println(finder.getKindflow(i));
}
}

private ArrayListMultimap< Integer,Dependency> parentDependencies = ArrayListMultimap.create();
private List< DependencyLink> depflows = new ArrayList<();

public DepflowFinder(AnnotationSet dependencyAS){
for(Annotation dep:dependencyAS){
FeatureMap fm = dep.getFeatures();
@SuppressWarnings(unchecked)
依赖关系依赖=新的依赖关系((List< Integer>)fm.get(args),(String)fm.get(kind));
parentDependencies.put(dependency.getParent(),dependency);
}
}

public void findDepflowsFor(Integer nodeId){
depflows = new ArrayList& (Dependency dep:parentDependencies.get(nodeId))
{
findDepflowsInternal(new DependencyLink(dep,null));
}
}

private void findDepflowsInternal(DependencyLink lastLink){
整数节点= lastLink.dependency.getChild();
列表<依赖关系> children = parentDependencies.get(node);
if(children.isEmpty()){
depflows.add(lastLink);返回;
}
(Dependency child:children){
findDepflowsInternal(new DependencyLink(child,lastLink));
}
}

public List< String> getKindflow(int i){
DependencyLink last = depflows.get(i);
LinkedList< String> ret = new LinkedList<>();
while(last!= null){
ret.addFirst(last.dependency.kind);
last = last.parentLink;
}
return ret;
}

public List< List< Integer>> getDepflow(int i){
DependencyLink last = depflows.get(i);
LinkedList< List< Integer>> ret = new LinkedList<>();
while(last!= null){
ret.addFirst(last.dependency.args);
last = last.parentLink;
}
return ret;
}

private static class Dependency {
List< Integer> ARGS;字符串种类
public Dependency(List< Integer> args,String kind){
this.args = args; this.kind = kind;
}
public Integer getParent(){return args.get(0); }
public Integer getChild(){return args.get(1); }
}

私有静态类DependencyLink {
依赖关系依赖; DependencyLink parentLink;
public DependencyLink(依赖关系依赖关系,DependencyLink parentLink){
this.dependency = dependency; this.parentLink = parentLink;
}
}

public int getNumDepflows(){return depflows.size(); }

public static void regiterGatePlugin(String name)throws Exception {
Gate.getCreoleRegister()。registerDirectories(new File(Gate.getPluginsHome(),name).toURI().toURL ));
}
}

这里是输出:

  [[12,10]] 
[auxpass]
[[12,14],[14,18] [18,21],[21,23],[23,25]]
[prep,pobj,appos,prep,pobj]
[[12,14],[14,18] [18,16]]
[prep,pobj,nn]
[[12,0],[0,2],[2,4],[4,8]]
$ s
[[12,0],[0,2],[2,4],[4,6]]
[nsubjpass,prep,pobj,cc ]


I have used the parser in the gate to get the dependency. And from that dependency, I found separate list for the dependency pair and dependency name.

My input is

String idlist =[26, 28]

String argslist = [[26, 25], [26, 24], [26, 29], [26, 28], [28, 27], [35, 26], [29, 30]]

String kindlist =[ aux, dobj, pobj, prep, advmod, aux, adj]

My expected output is two list

Depflowlist is first one and another one kindflowlist.

for 26 itself it has different flow.for 26 expected depflowlist is [26, 25] and its corresponding kindflowlist [aux]

then [26, 24] is another depflowlist for 26 and its corresponding kindflowlist [dobj]

then [[26, 29] ,[29, 30]] is another depflowlist for 26 and its corresponding kindflowlist [pobj, adj].
This should be done for 28 also.

I have write the code like :

    `public static void dependencyparse(String a,ArrayList<String> argslist,ArrayList<String> kindlist,Document doc){

     ArrayList<String> selectarg_list = new ArrayList<String>();
     for(int j=0;j<argslist.size();j++)
     {  
        int index=argslist.get(j).indexOf(a);
        if(index==1)
        {   
            String next=argslist.get(j).substring(5, 7);
            selectarg_list.add(next);
        }   
     }


    System.out.println(a+"---------->"+selectarg_list);

     if(!selectarg_list.isEmpty())
     {
        // System.out.println("notempty");
         hm.put(a, selectarg_list);
     }
    //System.out.println("Hashmap"+hm);

     if(selectarg_list.size()>0){

         dependencyparse(selectarg_list.get(0),argslist,kindlist,doc);

     }

     if(selectarg_list.size()==0){
         String prevKey = delete(a,selectarg_list,argslist,kindlist,doc);
         System.out.println(prevKey);


         if(!hm.isEmpty()){
             if(hm.keySet().contains(prevKey)){
            ArrayList<String> list = hm.get(prevKey);
             dependencyparse(list.get(0),argslist,kindlist,doc);
             }
             else{
                 if(!hm.isEmpty()){
                     try{
                 dependencyparse(hm.values().iterator().next().get(0), argslist, kindlist,doc);
                     }catch (Exception e) {}
                 }
             }
             Iterator<String> itr3 = hm.keySet().iterator();
             while (itr3.hasNext()) {
                String string = (String) itr3.next();
                System.out.println(string+"------------>"+hm.get(string));
            }
         }


     } 


 }


 private static String  delete(String a,ArrayList<String> selectarg_list,ArrayList<String> argslist,ArrayList<String> kindlist,Document doc) {
     AnnotationSet outputAnnotations=doc.getNamedAnnotationSets().get("Output markups");

     String retKey="";
    Collection<ArrayList<String>> values = hm.values();
    String key="";
    //System.out.println("Valuess"+values);
    //System.out.println("find"+a);
    boolean flag=false;
    Iterator<ArrayList<String>> Valueitr = values.iterator();
    while(Valueitr.hasNext())
    {
        //System.out.println("Valuess---->"+Valueitr.next());

        ArrayList<String> contentnext = Valueitr.next();
        if(contentnext.contains(a))
        { 
            flag=true;

            //System.out.println("Valuess find---->"+contentnext);
            for(Entry<String, ArrayList<String>> entry : hm.entrySet())
            {
                  if(contentnext.equals(entry.getValue()))
                  {
                    key = entry.getKey();
                   // System.out.println("Key find---->"+key);
                    // do something with the key
                    String arg = "["+key+", "+a+"]";
                   // System.out.println("arg find---->"+arg);
                    depFlowList.addFirst(arg);
                   // System.out.println("entry find---->"+entry.getValue());
                    try{
                    if(!hm.keySet().contains(a)){
                     entry.getValue().remove(a);
                   // hm.put(entry.getKey(), value)
                   //  System.out.println(entry.getKey()+"----------------"+hm.get(entry.getKey())+"\nentry delete---->"+entry.getValue());
                    }}catch (Exception e) { }



                  }
            }

         }

    }

    if(!flag){
        if(!depFlowList.isEmpty()){
            System.out.println("Dep Flow List----->"+depFlowList);

            for(int x=0;x<depFlowList.size();x++){
                String item=depFlowList.get(x);
                int item_index=argslist.indexOf(item);
                String item_kind=kindlist.get(item_index);
                System.out.println("adding kind of -----"+depFlowList.get(x));
                kindFlowList.add(item_kind);
            }

            System.out.println("Kind Flow List----->"+kindFlowList);
             file_write("/home/cognicor/vagateplugin/testing/dependency.txt",kindFlowList.toString());
        }

        if(!kindFlowList.isEmpty()){
            FeatureMap depfeature = new SimpleFeatureMapImpl();
            depfeature.put("depflow", kindFlowList);
            try {
                outputAnnotations.add(new Long(0),new Long(1), "DependencyFlow",depfeature);
            } catch (InvalidOffsetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        }
        if(depFlowList.size()==0){
            return "";
        }
        retKey=depFlowList.getLast().substring(1,3);
        depFlowList.clear();
        kindFlowList.clear();
        System.out.println(retKey);

        return retKey;
    }

    //System.out.println("++++++++++++++++++++++++++++++++++++++++++++++");

    ArrayList<String> findval = hm.get(key);
    if(findval.isEmpty()){
        hm.remove(key);
        //System.out.println("key removed");
    }

    //if(!hm.isEmpty()){
        retKey=delete(key, selectarg_list, argslist, kindlist,doc);
        return retKey;

    //}


}






public static void main(String[] args) {
    // TODO Auto-generated method stub

    ArrayList<String> testlist = file_read("/home/cognicor/vagateplugin/testing/testing_set1.txt");
    //System.out.println(testlist);
    ArrayList<String> query_class = new ArrayList<String>();
    ArrayList<String> property_class = new ArrayList<String>();
    Iterator<String> testinput = testlist.iterator();
    String entry;
    System.out.println("testlist---->"+testlist);
    for(int i=0;i<testlist.size();i++) 
    {
        try {
                ArrayList<Integer> idlist = new ArrayList<Integer>();
                ArrayList<String>  argslist = new ArrayList<String>();
                ArrayList<String>  kindlist = new ArrayList<String>();
                String query = "<currDial>"+testlist.get(i)+"</currDial>";
                System.out.println(query);
                Document doc = pipline.get_Annoted_Doument(query);
 /* code where i get the input like idlist, arglist and kindlist*/
                AnnotationSet annotset = doc.getNamedAnnotationSets().get("Output markups").get("TokenID");
                Iterator<Annotation> itr = annotset.iterator();
                while(itr.hasNext())
                {
                    Annotation annot = itr.next();
                    int id = (int) annot.getFeatures().get("id");
                    idlist.add(id);
                }
                AnnotationSet annotset1 = doc.getNamedAnnotationSets().get("Output markups").get("Dependency");
                Iterator<Annotation> itr1 = annotset1.iterator();
                while(itr1.hasNext())
                {
                    Annotation annot = itr1.next();
                    String arg = annot.getFeatures().get("args").toString();
                    String kind = annot.getFeatures().get("kind").toString();
                    argslist.add(arg);
                    kindlist.add(kind);
                 }
                System.out.println("args"+argslist);

                for(int j=0;j<idlist.size();j++)
                {
                    System.out.println("-------------------------------Item:"+idlist.get(j).toString()+"------------------------------------------");
                    dependencyparse(idlist.get(j).toString(),argslist,kindlist,doc);
                    if(j==idlist.size())
                    {
                        dependencyparse("null",argslist,kindlist,doc);
                    }
                }

            }catch(Exception e) { e.printStackTrace(); }
    } 

}
 }`

Now I got a loop which is not ending . I am using the output from the gate.

解决方案

Here is my code. Its in Java 7 + Guava. Note that I'm also calling the Stanford Parser and reading the input directly from annotations (no String parsing is needed).

The parsed sentence comes from nlp.stanford.edu.

import gate.*;
import gate.creole.SerialAnalyserController;
import java.io.File;
import java.util.*;
import com.google.common.collect.ArrayListMultimap;

public class DepflowFinder {
    public static void main(String[] args) throws Exception {
        Gate.setGateHome(new File("C:\\Program Files\\GATE_Developer_8.1"));
        Gate.init();
        regiterGatePlugin("ANNIE"); regiterGatePlugin("Stanford_CoreNLP" /*"Parser_Stanford"*/);

        SerialAnalyserController pipeline = (SerialAnalyserController) Factory.createResource("gate.creole.SerialAnalyserController");
        pipeline.add((ProcessingResource) Factory.createResource("gate.creole.tokeniser.DefaultTokeniser"));
        pipeline.add((ProcessingResource) Factory.createResource("gate.creole.splitter.SentenceSplitter"));
        pipeline.add((ProcessingResource) Factory.createResource("gate.stanford.Parser"));

        Corpus corpus = Factory.newCorpus("DepflowCorpus");
        Document document = Factory.newDocument("Bills on ports and immigration were submitted by Senator Brownback, Republican of Kansas.");
        corpus.add(document); pipeline.setCorpus(corpus); pipeline.execute();

        AnnotationSet defaultAS = document.getAnnotations();
        DepflowFinder finder = new DepflowFinder(defaultAS.get("Dependency"));
        finder.findDepflowsFor(defaultAS.get("Token", Utils.featureMap("string", "submitted")).iterator().next().getId());

        for (int i=0; i<finder.getNumDepflows(); i++) {
            System.err.println(finder.getDepflow(i));
            System.err.println(finder.getKindflow(i));
        }
    }

    private ArrayListMultimap<Integer, Dependency> parentDependencies = ArrayListMultimap.create();
    private List<DependencyLink> depflows = new ArrayList<>();

    public DepflowFinder(AnnotationSet dependencyAS) {
        for (Annotation dep : dependencyAS) {
            FeatureMap fm = dep.getFeatures();
            @SuppressWarnings("unchecked")
            Dependency dependency = new Dependency((List<Integer>) fm.get("args"), (String) fm.get("kind"));
            parentDependencies.put(dependency.getParent(), dependency);
        }
    }

    public void findDepflowsFor(Integer nodeId) {
        depflows = new ArrayList<>();
        for (Dependency dep : parentDependencies.get(nodeId)) {
            findDepflowsInternal(new DependencyLink(dep, null));
        }
    }

    private void findDepflowsInternal(DependencyLink lastLink) {
        Integer node = lastLink.dependency.getChild();
        List<Dependency> children = parentDependencies.get(node);
        if (children.isEmpty()) {
            depflows.add(lastLink); return;
        }
        for (Dependency child : children) {
            findDepflowsInternal(new DependencyLink(child, lastLink));
        }
    }

    public List<String> getKindflow(int i) {
        DependencyLink last = depflows.get(i);
        LinkedList<String> ret = new LinkedList<>();
        while (last != null) {
            ret.addFirst(last.dependency.kind);
            last = last.parentLink;
        }
        return ret;
    }

    public List<List<Integer>> getDepflow(int i) {
        DependencyLink last = depflows.get(i);
        LinkedList<List<Integer>> ret = new LinkedList<>();
        while (last != null) {
            ret.addFirst(last.dependency.args);
            last = last.parentLink;
        }
        return ret;
    }

    private static class Dependency {
        List<Integer> args; String kind;
        public Dependency(List<Integer> args, String kind) {
            this.args = args; this.kind = kind;
        }
        public Integer getParent() {return args.get(0); }
        public Integer getChild() {return args.get(1);  }
    }

    private static class DependencyLink {
        Dependency dependency; DependencyLink parentLink;
        public DependencyLink(Dependency dependency, DependencyLink parentLink) {
            this.dependency = dependency; this.parentLink = parentLink;
        }
    }

    public int getNumDepflows() {return depflows.size(); }

    public static void regiterGatePlugin(String name) throws Exception {
        Gate.getCreoleRegister().registerDirectories(new File(Gate.getPluginsHome(), name).toURI().toURL());
    }
}

And here is the output:

[[12, 10]]
[auxpass]
[[12, 14], [14, 18], [18, 21], [21, 23], [23, 25]]
[prep, pobj, appos, prep, pobj]
[[12, 14], [14, 18], [18, 16]]
[prep, pobj, nn]
[[12, 0], [0, 2], [2, 4], [4, 8]]
[nsubjpass, prep, pobj, conj]
[[12, 0], [0, 2], [2, 4], [4, 6]]
[nsubjpass, prep, pobj, cc]

这篇关于从解析器的输出找到依赖关系的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:54