【问题标题】:Training n-gram NER with Stanford NLP使用斯坦福 NLP 训练 n-gram NER
【发布时间】:2023-03-22 02:25:01
【问题描述】:

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

有了这个,我只能指定一元标记和它所属的类。任何人都可以指导我,以便我可以将其扩展到 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

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

CHAPTER O
I   O
Emma    PERS
Woodhouse   PERS

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

【问题讨论】:

  • 亲爱的@Arun,你成功地训练了 n-gram 的 NER 吗?我想培训教育,例如,理学硕士:教育,电子学博士:教育。你能指导我吗?谢谢
  • @KhalidUsman,感谢您与我们联系。我在下面的答案中使用了 LingPipe 来实现这一点。在相当数量的训练数据集上工作得很好。任何模型都可以正常工作,这取决于您提供的数据集的好坏程度。

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


【解决方案1】:

我在为自动领域标记 ngram 短语时遇到了同样的挑战。我一直在寻找一种有效的关键字映射,可用于在稍后阶段创建训练文件。我最终在 NLP 管道中使用了 regexNER,方法是提供一个包含正则表达式(ngram 组件术语)及其相应标签的映射文件。请注意,在这种情况下没有实现 NER 机器学习。希望这些信息对某人有所帮助!

【讨论】:

    【解决方案2】:

    在这里等待答案已经很长时间了。我一直无法弄清楚使用斯坦福核心完成它的方法。然而任务完成了。我也使用过 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());
    
            }
        }
    }
    

    代码礼貌:谷歌:)

    【讨论】:

    • tech.groups.yahoo.com/group/LingPipe/message/68 提供有关语料库准备的更多信息。
    • 我也试过同样的代码。您能否提一下您是如何准备训练集的。我将其添加为文本文件并尝试添加我自己的实体,但它不起作用...请帮助我。我不知道我是否误解了训练集
    • USAir 乘务员在飞机后部进行短途飞往夏洛特,NC,一直在第 21 排座位的拐角处偷看,逗 9 个月大的 Danasia Brown 笑。
    • 使用的训练集与我上面讨论的格式相同。您需要大量数据才能让模型“学习”。可能是一些格式良好的句子中的新闻文章或维基页面等。
    【解决方案3】:

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

    Emma    B-PERSON
    Woodhouse    I-PERSON
    

    然后,可以表示相同类别但不同实体的相邻标记。

    【讨论】:

    • 谢谢@Chris,让我尝试用这种编码格式创建一个新模型。
    • @ChristopherManning 如何在 NER 中启用 IOB 编码?谢谢
    • 我在回答这个问题时讨论了 IOB 编码选项:stackoverflow.com/questions/21469082/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多