Search in sources :

Example 1 with EnumItem

use of com.hankcs.hanlp.corpus.dictionary.item.EnumItem in project HanLP by hankcs.

the class NTDictionary method loadDat.

private EnumItem<NT>[] loadDat(String path) {
    byte[] bytes = IOUtil.readBytes(path);
    if (bytes == null)
        return null;
    NT[] values = NT.values();
    int index = 0;
    int size = ByteUtil.bytesHighFirstToInt(bytes, index);
    index += 4;
    EnumItem<NT>[] valueArray = new EnumItem[size];
    for (int i = 0; i < size; ++i) {
        int currentSize = ByteUtil.bytesHighFirstToInt(bytes, index);
        index += 4;
        EnumItem<NT> item = new EnumItem<NT>();
        for (int j = 0; j < currentSize; ++j) {
            NT tag = values[ByteUtil.bytesHighFirstToInt(bytes, index)];
            index += 4;
            int frequency = ByteUtil.bytesHighFirstToInt(bytes, index);
            index += 4;
            item.labelMap.put(tag, frequency);
        }
        valueArray[i] = item;
    }
    return valueArray;
}
Also used : NT(com.hankcs.hanlp.corpus.tag.NT) EnumItem(com.hankcs.hanlp.corpus.dictionary.item.EnumItem)

Example 2 with EnumItem

use of com.hankcs.hanlp.corpus.dictionary.item.EnumItem in project HanLP by hankcs.

the class PlaceRecognition method Recognition.

public static boolean Recognition(List<Vertex> pWordSegResult, WordNet wordNetOptimum, WordNet wordNetAll) {
    List<EnumItem<NS>> roleTagList = roleTag(pWordSegResult, wordNetAll);
    if (HanLP.Config.DEBUG) {
        StringBuilder sbLog = new StringBuilder();
        Iterator<Vertex> iterator = pWordSegResult.iterator();
        for (EnumItem<NS> NSEnumItem : roleTagList) {
            sbLog.append('[');
            sbLog.append(iterator.next().realWord);
            sbLog.append(' ');
            sbLog.append(NSEnumItem);
            sbLog.append(']');
        }
        System.out.printf("地名角色观察:%s\n", sbLog.toString());
    }
    List<NS> NSList = viterbiExCompute(roleTagList);
    if (HanLP.Config.DEBUG) {
        StringBuilder sbLog = new StringBuilder();
        Iterator<Vertex> iterator = pWordSegResult.iterator();
        sbLog.append('[');
        for (NS NS : NSList) {
            sbLog.append(iterator.next().realWord);
            sbLog.append('/');
            sbLog.append(NS);
            sbLog.append(" ,");
        }
        if (sbLog.length() > 1)
            sbLog.delete(sbLog.length() - 2, sbLog.length());
        sbLog.append(']');
        System.out.printf("地名角色标注:%s\n", sbLog.toString());
    }
    PlaceDictionary.parsePattern(NSList, pWordSegResult, wordNetOptimum, wordNetAll);
    return true;
}
Also used : Vertex(com.hankcs.hanlp.seg.common.Vertex) NS(com.hankcs.hanlp.corpus.tag.NS) EnumItem(com.hankcs.hanlp.corpus.dictionary.item.EnumItem)

Example 3 with EnumItem

use of com.hankcs.hanlp.corpus.dictionary.item.EnumItem in project HanLP by hankcs.

the class PlaceRecognition method roleTag.

public static List<EnumItem<NS>> roleTag(List<Vertex> vertexList, WordNet wordNetAll) {
    List<EnumItem<NS>> tagList = new LinkedList<EnumItem<NS>>();
    ListIterator<Vertex> listIterator = vertexList.listIterator();
    //        int line = 0;
    while (listIterator.hasNext()) {
        Vertex vertex = listIterator.next();
        //            }
        if (Nature.ns == vertex.getNature() && vertex.getAttribute().totalFrequency <= 1000) {
            if (// 二字地名,认为其可以再接一个后缀或前缀
            vertex.realWord.length() < 3)
                tagList.add(new EnumItem<NS>(NS.H, NS.G));
            else
                // 否则只可以再加后缀
                tagList.add(new EnumItem<NS>(NS.G));
            continue;
        }
        // 此处用等效词,更加精准
        EnumItem<NS> NSEnumItem = PlaceDictionary.dictionary.get(vertex.word);
        if (NSEnumItem == null) {
            NSEnumItem = new EnumItem<NS>(NS.Z, PlaceDictionary.transformMatrixDictionary.getTotalFrequency(NS.Z));
        }
        tagList.add(NSEnumItem);
    //            line += vertex.realWord.length();
    }
    return tagList;
}
Also used : Vertex(com.hankcs.hanlp.seg.common.Vertex) NS(com.hankcs.hanlp.corpus.tag.NS) EnumItem(com.hankcs.hanlp.corpus.dictionary.item.EnumItem) LinkedList(java.util.LinkedList)

Example 4 with EnumItem

use of com.hankcs.hanlp.corpus.dictionary.item.EnumItem in project HanLP by hankcs.

the class OrganizationRecognition method roleTag.

public static List<EnumItem<NT>> roleTag(List<Vertex> vertexList, WordNet wordNetAll) {
    List<EnumItem<NT>> tagList = new LinkedList<EnumItem<NT>>();
    //        int line = 0;
    for (Vertex vertex : vertexList) {
        // 构成更长的
        Nature nature = vertex.guessNature();
        switch(nature) {
            case nrf:
                {
                    if (vertex.getAttribute().totalFrequency <= 1000) {
                        tagList.add(new EnumItem<NT>(NT.F, 1000));
                    } else
                        break;
                }
                continue;
            case ni:
            case nic:
            case nis:
            case nit:
                {
                    EnumItem<NT> ntEnumItem = new EnumItem<NT>(NT.K, 1000);
                    ntEnumItem.addLabel(NT.D, 1000);
                    tagList.add(ntEnumItem);
                }
                continue;
            case m:
                {
                    EnumItem<NT> ntEnumItem = new EnumItem<NT>(NT.M, 1000);
                    tagList.add(ntEnumItem);
                }
                continue;
        }
        // 此处用等效词,更加精准
        EnumItem<NT> NTEnumItem = OrganizationDictionary.dictionary.get(vertex.word);
        if (NTEnumItem == null) {
            NTEnumItem = new EnumItem<NT>(NT.Z, OrganizationDictionary.transformMatrixDictionary.getTotalFrequency(NT.Z));
        }
        tagList.add(NTEnumItem);
    //            line += vertex.realWord.length();
    }
    return tagList;
}
Also used : Nature(com.hankcs.hanlp.corpus.tag.Nature) Vertex(com.hankcs.hanlp.seg.common.Vertex) NT(com.hankcs.hanlp.corpus.tag.NT) EnumItem(com.hankcs.hanlp.corpus.dictionary.item.EnumItem) LinkedList(java.util.LinkedList)

Example 5 with EnumItem

use of com.hankcs.hanlp.corpus.dictionary.item.EnumItem in project HanLP by hankcs.

the class PersonRecognition method roleObserve.

/**
     * 角色观察(从模型中加载所有词语对应的所有角色,允许进行一些规则补充)
     * @param wordSegResult 粗分结果
     * @return
     */
public static List<EnumItem<NR>> roleObserve(List<Vertex> wordSegResult) {
    List<EnumItem<NR>> tagList = new LinkedList<EnumItem<NR>>();
    for (Vertex vertex : wordSegResult) {
        EnumItem<NR> nrEnumItem = PersonDictionary.dictionary.get(vertex.realWord);
        if (nrEnumItem == null) {
            switch(vertex.guessNature()) {
                case nr:
                    {
                        // 有些双名实际上可以构成更长的三名
                        if (vertex.getAttribute().totalFrequency <= 1000 && vertex.realWord.length() == 2) {
                            nrEnumItem = new EnumItem<NR>(NR.X, NR.G);
                        } else
                            nrEnumItem = new EnumItem<NR>(NR.A, PersonDictionary.transformMatrixDictionary.getTotalFrequency(NR.A));
                    }
                    break;
                case nnt:
                    {
                        // 姓+职位
                        nrEnumItem = new EnumItem<NR>(NR.G, NR.K);
                    }
                    break;
                default:
                    {
                        nrEnumItem = new EnumItem<NR>(NR.A, PersonDictionary.transformMatrixDictionary.getTotalFrequency(NR.A));
                    }
                    break;
            }
        }
        tagList.add(nrEnumItem);
    }
    return tagList;
}
Also used : Vertex(com.hankcs.hanlp.seg.common.Vertex) NR(com.hankcs.hanlp.corpus.tag.NR) EnumItem(com.hankcs.hanlp.corpus.dictionary.item.EnumItem) LinkedList(java.util.LinkedList)

Aggregations

EnumItem (com.hankcs.hanlp.corpus.dictionary.item.EnumItem)9 Vertex (com.hankcs.hanlp.seg.common.Vertex)6 NR (com.hankcs.hanlp.corpus.tag.NR)3 NS (com.hankcs.hanlp.corpus.tag.NS)3 NT (com.hankcs.hanlp.corpus.tag.NT)3 LinkedList (java.util.LinkedList)3 Nature (com.hankcs.hanlp.corpus.tag.Nature)1