【问题标题】:Sentence detection using opennlp on hadoop在 hadoop 上使用 opennlp 进行句子检测
【发布时间】:2014-01-24 06:41:39
【问题描述】:

我想使用 OPenNLP 和 Hadoop 进行句子检测。我已经成功地在 Java 上实现了相同的功能。想在 Mapreduce 平台上实现相同的功能。谁能帮帮我?

【问题讨论】:

    标签: hadoop mapreduce detection opennlp sentence


    【解决方案1】:

    我用两种不同的方式做到了这一点。 一种方法是将每个节点的句子检测模型推送到标准目录(即/opt/opennlpmodels/),并在您的映射器类中的类级别读取序列化模型,然后在您的映射中适当地使用它或减少函数。

    另一种方法是将模型放入数据库或分布式缓存中(作为 blob 或其他东西......我之前使用 Accumulo 来存储文档分类模型)。然后在类级别建立与数据库的连接并将模型作为 bytearrayinputstream 获取。

    我使用 Puppet 推出模型,但使用您通常使用的任何方式来保持集群上的文件是最新的。

    根据您的 hadoop 版本,您可以将模型作为属性偷偷放入 jobsetup 中,然后只有 master(或您启动作业的任何位置)需要在其上具有实际的模型文件。这个我没试过。

    如果您需要知道如何实际使用 OpenNLP 句子检测器,请告诉我,我会发布一个示例。 高温

    import java.io.File;
    import java.io.FileInputStream;
    import opennlp.tools.sentdetect.SentenceDetector;
    import opennlp.tools.sentdetect.SentenceDetectorME;
    import opennlp.tools.sentdetect.SentenceModel;
    import opennlp.tools.util.Span;
    
    public class SentenceDetection {
    
      SentenceDetector sd;
    
      public Span[] getSentences(String docTextFromMapFunction) throws Exception {
    
        if (sd == null) {
          sd = new SentenceDetectorME(new SentenceModel(new FileInputStream(new File("/standardized-on-each-node/path/to/en-sent.zip"))));
        }
        /**
         * this gives you the actual sentences as a string array
         */
        // String[] sentences = sd.sentDetect(docTextFromMapFunction);
        /**
         * this gives you the spans (the charindexes to the start and end of each
         * sentence in the doc)
         *
         */
        Span[] sentenceSpans = sd.sentPosDetect(docTextFromMapFunction);
        /**
         * you can do this as well to get the actual sentence strings based on the spans
         */
        // String[] spansToStrings = Span.spansToStrings(sentPosDetect, docTextFromMapFunction);
        return sentenceSpans;
      }
    }
    

    HTH...只要确保文件就位。有更优雅的方法可以做到这一点,但这很有效,而且很简单。

    【讨论】:

    猜你喜欢
    • 2015-10-30
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多