使用Stanford NLP训练n-gram NER

Aru*_*A K 21 nlp named-entity-recognition stanford-nlp named-entity-extraction opennlp

最近,我一直在尝试使用Stanford Core NLP训练n-gram实体.我已经按照以下教程 - http://nlp.stanford.edu/software/crf-faq.shtml#b

有了这个,我只能指定unigram令牌及其所属的类.任何人都可以指导我,以便我可以将它扩展到n-gram.我试图从聊天数据集中提取电影名称等已知实体.

如果我错误地解释了斯坦福教程,请指导我,同样可以用于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
Run Code Online (Sandbox Code Playgroud)

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

CHAPTER O
I   O
Emma    PERS
Woodhouse   PERS
Run Code Online (Sandbox Code Playgroud)

现在我需要训练像Hulk,Titanic等已知实体(比如电影名称)作为电影,这种方法很容易.但是如果我需要训练我知道你去年夏天做什么或者宝宝出去什么,最好的办法是什么?

Aru*_*A K 25

这是一个漫长的等待回答.我无法找到使用Stanford Core完成任务的方法.但任务完成了.我使用了LingPipe NLP库.只是在这里引用答案,因为我认为其他人可以从中受益.

如果您是开发人员或研究人员或者其他任何人,请在潜入之前查看Lingpipe许可证.

Lingpipe提供各种NER方法.

1)基于字典的NER

2)统计NER(基于HMM)

3)基于规则的NER等

我使用了字典以及统计方法.

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

可以在此处找到基于字典的NER的示例

历史方法需要培训文件.我使用了以下格式的文件 -

<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>
Run Code Online (Sandbox Code Playgroud)

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

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

为了测试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());

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

代码礼貌:谷歌:)


Chr*_*ing 14

答案基本上在你引用的例子中给出,其中"Emma Woodhouse"是一个单一的名字.我们提供的默认模型使用IO编码,并假设同一类的相邻令牌是同一实体的一部分.在许多情况下,这几乎总是正确的,并使模型更简单.但是,如果您不想这样做,您可以使用其他标签编码训练NER模型,例如常用的IOB编码,您可以在其中标记事物:

Emma    B-PERSON
Woodhouse    I-PERSON
Run Code Online (Sandbox Code Playgroud)

然后,可以表示相同类别但不是相同实体的相邻令牌.