nlp - 使用斯坦福 NLP 训练 n-gram NER

标签 nlp stanford-nlp opennlp named-entity-recognition named-entity-extraction

最近我一直在尝试使用斯坦福核心 NLP 来训练 n-gram 实体。我已遵循以下教程 - http://nlp.stanford.edu/software/crf-faq.shtml#b

有了这个,我只能指定一元标记及其所属的类。任何人都可以指导我,以便我可以将其扩展到 n 元语法。我正在尝试从聊天数据集中提取已知实体,例如电影名称。

如果我误解了斯坦福教程,请指导我,并且该教程也可用于 n-gram 训练。

我所坚持的是以下属性

#structure of your training file; this tells the classifier
#that the word is in column 0 and the correct answer is in
#column 1
map = word=0,answer=1

这里第一列是单词(unigram),第二列是实体,例如

CHAPTER O
I   O
Emma    PERS
Woodhouse   PERS

现在我需要将已知实体(例如电影名称)(如绿巨人浩克、泰坦尼克号等)训练为电影,使用这种方法会很容易。但如果我需要训练我知道你去年夏天做了什么婴儿节外出,最好的方法是什么?

最佳答案

在这里等待答案已经很长时间了。我一直无法找出使用斯坦福核心完成它的方法。然而任务完成了。我已经使用 LingPipe NLP 库来实现同样的目的。只是在这里引用答案,因为我认为其他人可以从中受益。

请查看Lingpipe licencing如果您是开发人员或研究人员或其他人员,那么在投入实现之前。

Lingpipe提供了多种NER方法。

1) 基于字典的 NER

2)统计NER(基于HMM)

3) 基于规则的 NER 等

我使用了字典和统计方法。

第一个是直接查找方法,第二个是基于培训的方法。

基于字典的 NER 示例可以在 here 中找到。

统计方法需要训练文件。我使用了以下格式的文件 -

<root>
<s> data line with the <ENAMEX TYPE="myentity">entity1</ENAMEX>  to be trained</s>
...
<s> with the <ENAMEX TYPE="myentity">entity2</ENAMEX>  annotated </s>
</root>

然后我使用以下代码来训练实体。

import java.io.File;
import java.io.IOException;

import com.aliasi.chunk.CharLmHmmChunker;
import com.aliasi.corpus.parsers.Muc6ChunkParser;
import com.aliasi.hmm.HmmCharLmEstimator;
import com.aliasi.tokenizer.IndoEuropeanTokenizerFactory;
import com.aliasi.tokenizer.TokenizerFactory;
import com.aliasi.util.AbstractExternalizable;

@SuppressWarnings("deprecation")
public class TrainEntities {

    static final int MAX_N_GRAM = 50;
    static final int NUM_CHARS = 300;
    static final double LM_INTERPOLATION = MAX_N_GRAM; // default behavior

    public static void main(String[] args) throws IOException {
        File corpusFile = new File("inputfile.txt");// my annotated file
        File modelFile = new File("outputmodelfile.model"); 

        System.out.println("Setting up Chunker Estimator");
        TokenizerFactory factory
            = IndoEuropeanTokenizerFactory.INSTANCE;
        HmmCharLmEstimator hmmEstimator
            = new HmmCharLmEstimator(MAX_N_GRAM,NUM_CHARS,LM_INTERPOLATION);
        CharLmHmmChunker chunkerEstimator
            = new CharLmHmmChunker(factory,hmmEstimator);

        System.out.println("Setting up Data Parser");
        Muc6ChunkParser parser = new Muc6ChunkParser();  
        parser.setHandler( chunkerEstimator);

        System.out.println("Training with Data from File=" + corpusFile);
        parser.parse(corpusFile);

        System.out.println("Compiling and Writing Model to File=" + modelFile);
        AbstractExternalizable.compileTo(chunkerEstimator,modelFile);
    }

}

为了测试 NER,我使用了以下类

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Set;

import com.aliasi.chunk.Chunk;
import com.aliasi.chunk.Chunker;
import com.aliasi.chunk.Chunking;
import com.aliasi.util.AbstractExternalizable;

public class Recognition {
    public static void main(String[] args) throws Exception {
        File modelFile = new File("outputmodelfile.model");
        Chunker chunker = (Chunker) AbstractExternalizable
                .readObject(modelFile);
        String testString="my test string";
            Chunking chunking = chunker.chunk(testString);
            Set<Chunk> test = chunking.chunkSet();
            for (Chunk c : test) {
                System.out.println(testString + " : "
                        + testString.substring(c.start(), c.end()) + " >> "
                        + c.type());

        }
    }
}

代码提供:Google :)

关于nlp - 使用斯坦福 NLP 训练 n-gram NER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15609324/

相关文章:

nlp - 从依赖树中提取(主语、谓语、宾语)

java - 解析斯坦福依赖关系

python - 我如何在 Python 中使用 Stanford Parser 的类型化依赖项?

java - 如何使用 OpenNLP 创建自定义模型?

search - 有没有可以直接给出答案的搜索引擎?

nlp - 使用动态技术的单词之间的语义相似性(使用维基百科)

java - 导入 edu.stanford.nlp.ling.CoreAnnotations.OriginalTextAnnotation 无法解析

algorithm - 比较和匹配来自不同商店/供应商的产品名称

java - OpenNLP find() 方法

text-mining - 使用 OpenNLP 进行情感分析