Search in sources :

Example 6 with CoNLLWord

use of com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord in project HanLP by hankcs.

the class NeuralNetworkDependencyParser method parse.

@Override
public CoNLLSentence parse(List<Term> termList) {
    List<String> posTagList = PosTagUtil.to863(termList);
    List<String> wordList = new ArrayList<String>(termList.size());
    for (Term term : termList) {
        wordList.add(term.word);
    }
    List<Integer> heads = new ArrayList<Integer>(termList.size());
    List<String> deprels = new ArrayList<String>(termList.size());
    parser_dll.parse(wordList, posTagList, heads, deprels);
    CoNLLWord[] wordArray = new CoNLLWord[termList.size()];
    for (int i = 0; i < wordArray.length; ++i) {
        wordArray[i] = new CoNLLWord(i + 1, wordList.get(i), posTagList.get(i), termList.get(i).nature.toString());
        wordArray[i].DEPREL = deprels.get(i);
    }
    for (int i = 0; i < wordArray.length; ++i) {
        int index = heads.get(i) - 1;
        if (index < 0) {
            wordArray[i].HEAD = CoNLLWord.ROOT;
            continue;
        }
        wordArray[i].HEAD = wordArray[index];
    }
    return new CoNLLSentence(wordArray);
}
Also used : CoNLLWord(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord) ArrayList(java.util.ArrayList) CoNLLSentence(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence) Term(com.hankcs.hanlp.seg.common.Term)

Example 7 with CoNLLWord

use of com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord in project HanLP by hankcs.

the class DemoDependencyParser method main.

public static void main(String[] args) {
    CoNLLSentence sentence = HanLP.parseDependency("徐先生还具体帮助他确定了把画雄鹰、松鼠和麻雀作为主攻目标。");
    System.out.println(sentence);
    // 可以方便地遍历它
    for (CoNLLWord word : sentence) {
        System.out.printf("%s --(%s)--> %s\n", word.LEMMA, word.DEPREL, word.HEAD.LEMMA);
    }
    // 也可以直接拿到数组,任意顺序或逆序遍历
    CoNLLWord[] wordArray = sentence.getWordArray();
    for (int i = wordArray.length - 1; i >= 0; i--) {
        CoNLLWord word = wordArray[i];
        System.out.printf("%s --(%s)--> %s\n", word.LEMMA, word.DEPREL, word.HEAD.LEMMA);
    }
    // 还可以直接遍历子树,从某棵子树的某个节点一路遍历到虚根
    CoNLLWord head = wordArray[12];
    while ((head = head.HEAD) != null) {
        if (head == CoNLLWord.ROOT)
            System.out.println(head.LEMMA);
        else
            System.out.printf("%s --(%s)--> ", head.LEMMA, head.DEPREL);
    }
}
Also used : CoNLLWord(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord) CoNLLSentence(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence)

Example 8 with CoNLLWord

use of com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord in project HanLP by hankcs.

the class TestDependencyCorpus method testMakeCRF.

/**
     * 导出CRF训练语料
     *
     * @throws Exception
     */
public void testMakeCRF() throws Exception {
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Tools\\CRF++-0.58\\example\\dependency\\dev.txt")));
    LinkedList<CoNLLSentence> coNLLSentences = CoNLLLoader.loadSentenceList("D:\\Doc\\语料库\\依存分析训练数据\\THU\\dev.conll.fixed.txt");
    for (CoNLLSentence coNLLSentence : coNLLSentences) {
        for (CoNLLWord coNLLWord : coNLLSentence.word) {
            bw.write(coNLLWord.NAME);
            bw.write('\t');
            bw.write(coNLLWord.CPOSTAG);
            bw.write('\t');
            bw.write(coNLLWord.POSTAG);
            bw.write('\t');
            int d = coNLLWord.HEAD.ID - coNLLWord.ID;
            int posDistance = 1;
            if (// 在后面
            d > 0) {
                for (int i = 1; i < d; ++i) {
                    if (coNLLSentence.word[coNLLWord.ID - 1 + i].CPOSTAG.equals(coNLLWord.HEAD.CPOSTAG)) {
                        ++posDistance;
                    }
                }
            } else {
                for (// 在前面
                int i = 1; // 在前面
                i < -d; // 在前面
                ++i) {
                    if (coNLLSentence.word[coNLLWord.ID - 1 - i].CPOSTAG.equals(coNLLWord.HEAD.CPOSTAG)) {
                        ++posDistance;
                    }
                }
            }
            bw.write((d > 0 ? "+" : "-") + posDistance + "_" + coNLLWord.HEAD.CPOSTAG);
            bw.newLine();
        }
        bw.newLine();
    }
    bw.close();
}
Also used : CoNLLWord(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord) FileOutputStream(java.io.FileOutputStream) CoNLLSentence(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 9 with CoNLLWord

use of com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord in project HanLP by hankcs.

the class TestDependencyCorpus method testPosTag.

/**
     * 细粒度转粗粒度
     *
     * @throws Exception
     */
public void testPosTag() throws Exception {
    DictionaryMaker dictionaryMaker = new DictionaryMaker();
    LinkedList<CoNLLSentence> coNLLSentences = CoNLLLoader.loadSentenceList("D:\\Doc\\语料库\\依存分析训练数据\\THU\\dev.conll.fixed.txt");
    for (CoNLLSentence coNLLSentence : coNLLSentences) {
        for (CoNLLWord coNLLWord : coNLLSentence.word) {
            dictionaryMaker.add(new Item(coNLLWord.POSTAG, coNLLWord.CPOSTAG));
        }
    }
    System.out.println(dictionaryMaker.entrySet());
}
Also used : Item(com.hankcs.hanlp.corpus.dictionary.item.Item) CoNLLWord(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord) CoNLLSentence(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence) DictionaryMaker(com.hankcs.hanlp.corpus.dictionary.DictionaryMaker)

Example 10 with CoNLLWord

use of com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord in project HanLP by hankcs.

the class MinimumSpanningTreeParser method parse.

@Override
public CoNLLSentence parse(List<Term> termList) {
    if (termList == null || termList.size() == 0)
        return null;
    termList.add(0, new Term("##核心##", Nature.begin));
    Node[] nodeArray = new Node[termList.size()];
    Iterator<Term> iterator = termList.iterator();
    for (int i = 0; i < nodeArray.length; ++i) {
        nodeArray[i] = new Node(iterator.next(), i);
    }
    Edge[][] edges = new Edge[nodeArray.length][nodeArray.length];
    for (int i = 0; i < edges.length; ++i) {
        for (int j = 0; j < edges[i].length; ++j) {
            if (i != j) {
                edges[j][i] = makeEdge(nodeArray, i, j);
            }
        }
    }
    // 最小生成树Prim算法
    int max_v = nodeArray.length * (nodeArray.length - 1);
    float[] mincost = new float[max_v];
    Arrays.fill(mincost, Float.MAX_VALUE / 3);
    boolean[] used = new boolean[max_v];
    Arrays.fill(used, false);
    used[0] = true;
    PriorityQueue<State> que = new PriorityQueue<State>();
    // 找虚根的唯一孩子
    float minCostToRoot = Float.MAX_VALUE;
    Edge firstEdge = null;
    Edge[] edgeResult = new Edge[termList.size() - 1];
    for (Edge edge : edges[0]) {
        if (edge == null)
            continue;
        if (minCostToRoot > edge.cost) {
            firstEdge = edge;
            minCostToRoot = edge.cost;
        }
    }
    if (firstEdge == null)
        return null;
    que.add(new State(minCostToRoot, firstEdge.from, firstEdge));
    while (!que.isEmpty()) {
        State p = que.poll();
        int v = p.id;
        if (used[v] || p.cost > mincost[v])
            continue;
        used[v] = true;
        if (p.edge != null) {
            //                System.out.println(p.edge.from + " " + p.edge.to + p.edge.label);
            edgeResult[p.edge.from - 1] = p.edge;
        }
        for (Edge e : edges[v]) {
            if (e == null)
                continue;
            if (mincost[e.from] > e.cost) {
                mincost[e.from] = e.cost;
                que.add(new State(mincost[e.from], e.from, e));
            }
        }
    }
    CoNLLWord[] wordArray = new CoNLLWord[termList.size() - 1];
    for (int i = 0; i < wordArray.length; ++i) {
        wordArray[i] = new CoNLLWord(i + 1, nodeArray[i + 1].word, nodeArray[i + 1].label);
        wordArray[i].DEPREL = edgeResult[i].label;
    }
    for (int i = 0; i < edgeResult.length; ++i) {
        int index = edgeResult[i].to - 1;
        if (index < 0) {
            wordArray[i].HEAD = CoNLLWord.ROOT;
            continue;
        }
        wordArray[i].HEAD = wordArray[index];
    }
    return new CoNLLSentence(wordArray);
}
Also used : Node(com.hankcs.hanlp.dependency.common.Node) Term(com.hankcs.hanlp.seg.common.Term) PriorityQueue(java.util.PriorityQueue) State(com.hankcs.hanlp.dependency.common.State) CoNLLWord(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord) CoNLLSentence(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence) Edge(com.hankcs.hanlp.dependency.common.Edge)

Aggregations

CoNLLWord (com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord)12 CoNLLSentence (com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence)10 Term (com.hankcs.hanlp.seg.common.Term)4 DictionaryMaker (com.hankcs.hanlp.corpus.dictionary.DictionaryMaker)2 Evaluator (com.hankcs.hanlp.corpus.dependency.CoNll.Evaluator)1 Item (com.hankcs.hanlp.corpus.dictionary.item.Item)1 Edge (com.hankcs.hanlp.dependency.common.Edge)1 Node (com.hankcs.hanlp.dependency.common.Node)1 State (com.hankcs.hanlp.dependency.common.State)1 Table (com.hankcs.hanlp.model.crf.Table)1 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 PriorityQueue (java.util.PriorityQueue)1 TreeSet (java.util.TreeSet)1