如何从下一个顺序中的依赖图中提取我的句子中的单词:current_Word,governor,dependent和relation type

时间:2017-06-21 19:33:46

标签: java xml stanford-nlp

我有这句话:

  

她将庆祝复活节

     

我有这个依赖图:

  <dependencies style="typed">
      <dep type="nsubj">
        <governor idx="3">celebrate</governor>
        <dependent idx="1">She</dependent>
      </dep>
      <dep type="aux">
        <governor idx="3">celebrate</governor>
        <dependent idx="2">will</dependent>
      </dep>
      <dep type="dobj">
        <governor idx="3">celebrate</governor>`enter code here`
        <dependent idx="4">Easter</dependent>
      </dep>
      <dep type="punct">
        <governor idx="3">celebrate</governor>
        <dependent idx="5">.</dependent>
      </dep>
    </dependencies>

如何提取单词以产生下一个结果:  result = current_word + governor + dependent + relation_type

州长的意思是“谁依赖我”和依赖的手段:谁是我的州长。

我有以下代码:

public static String depRel(String graph) throws SAXException, IOException,
        ParserConfigurationException {
    String xml_String;
    String[] split_Sentence = graph.split("\\s+");
    xml_String = Features.dependency_Graph(graph);
    String result = "";
    String dependent = "";
    String governor = "";
    String type = "";


    Document document = convertStringToDocument(xmlString);
    document.getDocumentElement().normalize();
    Element root = document.getDocumentElement();
    NodeList nList = document.getElementsByTagName("dependencies");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node node = nList.item(temp);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement1 = (Element) node;

        }
        NodeList nodesDocPart = node.getChildNodes();
        for (int temp2 = 0; temp2 < nodesDocPart.getLength(); temp2++) {

            Node n = nodesDocPart.item(temp2);

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element el1 = (Element) n;
                type = el1.getAttribute("type");
            }

            // /////////////////////////////////////////////////sentence/////////////////////////////////////////////
            NodeList nodesSentencePart = n.getChildNodes();
            for (int temp3 = 0; temp3 < nodesSentencePart.getLength(); temp3++) {
                Node sentence = nodesSentencePart.item(temp3);
                if (sentence.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement4 = (Element) sentence;
                    if (eElement4.getTagName().equals("governor")) {
                        governor = eElement4.getTextContent();
                        result = governor + 
                                + dependent + ":" + type;

                    } else if (eElement4.getTagName().equals("dependent")) {
                        dependent = eElement4.getTextContent();
                        result = dependent  + governor
                                + ":" + type;

                    }

                }
                for (int i = 0; i < splitSentence.length; i++) {
                    if (split_Sentence[i].equals(dependent)) {

                        result = split_Sentence[i] + governor + dependent
                                + type;
                    }
                }
                System.out.println("Rez=> " + result);
            }

        }

    }

    return result;

}

这是在句子上制作图表的方法:

I want for every word the next output:
result=current_Word+governor+dependent+relation_type.

方法dependency_Graph为给定的句子创建依赖图。     输出出现很多次并且没有出现单词。我试图将一个初始句子保留在一个变量中,但它没有用。     我不知道如何将句子中的每个单词的结果作为字符串返回一次,因为我将在另一个方法中重用它。     我不知道如果我对一个单词有更多的依赖,我怎么能做,因为我必须保留三个。     我必须将结果保存在逗号分隔值文件中,然后在机器学习工具中处理它。任何想法如何使用我的代码获得此结果?提前感谢您!

0 个答案:

没有答案