Nlp 使用Mallet建立负荷模型并对输入进行分类

Nlp 使用Mallet建立负荷模型并对输入进行分类,nlp,tagging,mallet,Nlp,Tagging,Mallet,我已经有了一个使用SimpleTagger训练的CRF训练模型 SimpleTagger.main(new String[] { "--train", "true", "--model-file", "/Desktop/crfmodel", "--threads", "8", "--training-proportion", "0.8",

我已经有了一个使用SimpleTagger训练的CRF训练模型

        SimpleTagger.main(new String[] {
                "--train", "true",
                "--model-file", "/Desktop/crfmodel",
                "--threads",  "8",
                "--training-proportion", "0.8",
                "--weights", "dense",
                "--test", "lab",
//                "--orders", "2",
                "/Desktop/annotations.txt"
        });
我计划加载此模型并将其用于标记。我正在使用这个代码

    public static void main(String[] args) throws Exception {

        //DOCS http://mallet.cs.umass.edu/classifier-devel.php

        Instance instance = getMyInstance();

        Classifier classifier = loadClassifier(Paths.get("/Desktop/crfmodel").toFile());

        Labeling labeling = classifier.classify(instance).getLabeling();
        Label l = labeling.getBestLabel();
        System.out.print(instance);
        System.out.println(l);
    }


    private static Classifier loadClassifier(File serializedFile)
            throws FileNotFoundException, IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream (new FileInputStream(serializedFile));
        Classifier crf = (Classifier) ois.readObject();
        ois.close();

        return crf;
    }
当我尝试执行上述操作时,会出现以下错误

Exception in thread "main" java.lang.ClassCastException: cc.mallet.fst.CRF cannot be cast to cc.mallet.classify.Classifier
    at TagClassifier.loadClassifier(TagClassifier.java:77)
    at TagClassifier.main(TagClassifier.java:64)
错误正在发生

Classifier crf = (Classifier) ois.readObject();

我可以知道为什么会这样吗。此外,如果有一种正确的文档化方法,可以使用经过培训的模型来标记输入,您能否共享任何链接/文档?提前非常感谢

它们是不同的东西,所以你不能把一个换成另一个。CRF为序列中的每个元素推断类,因此其输出是一个标签数组。分类器接受一个输入并返回一个标签。

我想我是通过查看SimpleTagger代码找到的

        crfModel = loadClassifier(Paths.get("/Desktop/crfmodel").toFile());
        pipe = crfModel.getInputPipe();
        pipe.setTargetProcessing(false);
        String formatted = getFormattedQuery(q);

        Instance instance = pipe.pipe(new Instance(formatted, null, null, null));
        Sequence sequence = (Sequence) instance.getData();
        Sequence[] tags = tag(sequence, 3);

谢谢David Mimno,有没有一个示例/类可以让我参考,它可以推断实例中每个元素的顺序。还有可能得到这些预测的确定程度的百分比或概率吗?
    private static Sequence[] tag(Sequence input, int bestK) {
        Sequence[] answers;
        if (bestK == 1) {
            answers = new Sequence[1];
            answers[0] = crfModel.transduce(input);
        } else {
            MaxLatticeDefault lattice = new MaxLatticeDefault(crfModel, input, null);
            answers = lattice.bestOutputSequences(bestK).toArray(new Sequence[0]);
        }
        return answers;
    }