我在门中使用了解析器来获取依赖关系。从该依赖项中,我找到了依赖关系对和依赖关系名称的单独列表。
我的输入是
String idlist = [26,28]
String argslist = [[26,25],[26,24],[26,29],[26,28],[28,27],[35,26],[29,30]] < / p>
String kindlist = [aux,dobj,pobj,prep,advmod,aux,adj]
我的预期输出是两个列表
Depflowlist是第一个和另一个kindflowlist。
对于26本身它有不同的流量。 对于26个预期的depflowlist是[26,25]及其相应的kindflowlist [aux]然后[26,24]是26的另一个depflowlist及其对应的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>();
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(); }
}
}
}`
现在我得到了一个没有结束的循环。我正在使用门的输出。
答案 0 :(得分:0)
这是我的代码。它在Java 7 + Guava中。请注意,我也在调用Stanford Parser并直接从注释中读取输入(不需要进行String解析)。
解析的句子来自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());
}
}
这是输出:
[[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]