Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

java - Find the flow of dependency from the ouput of a parser

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())+"
entry 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...