使用 apache lucene 进行词形还原

Lemmatization with apache lucene

我正在使用 apache lucene 开发一个文本分析项目。我需要对一些文本进行词形还原(将单词转换为它们的规范形式)。我已经编写了生成词干的代码。使用它,我可以转换下面的句子

The stem is the part of the word that never changes even when morphologically inflected; a lemma is the base form of the word. For example, from "produced", the lemma is "produce", but the stem is "produc-". This is because there are words such as production

进入

stem part word never chang even when morpholog inflect lemma base form word exampl from produc lemma produc stem produc becaus word product

但是,我需要获取单词的基本形式:example 而不是 examplproduce 而不是 produc,依此类推。

我正在使用 lucene,因为它有多种语言的分析器(我至少需要英语和俄语)。我知道 Stanford NLP 库,但它不支持俄语。

那么有什么方法可以像我使用 lucene 进行词干提取那样对多种语言进行词形还原吗?

我负责词干提取的代码的简化版本:

//Using apache tika to identify the language
LanguageIdentifier identifier = new LanguageIdentifier(text);
//getting analyzer according to the language (eg, EnglishAnalyzer for 'en')
Analyzer analyzer = getAnalyzer(identifier.getLanguage());
TokenStream stream = analyzer.tokenStream("field", text);
stream.reset();
while (stream.incrementToken()) {
    String stem = stream.getAttribute(CharTermAttribute.class).toString();
    // doing something with the stem
    System.out.print(stem+ " ");
}
stream.end();
stream.close();

更新: 我发现 library 几乎可以满足我的需要(针对英语和俄语)并使用 apache lucene(尽管以其自己的方式),绝对值得探索。

是的,StanfordNLP 很适合英语。但是如果你需要支持几种语言我可以推荐你Freeling,检查它的Freeling_online_demo,请select语言和输出(词法分析用于词形还原)。我不会说俄语,但我认为它适用于这段文字:

Продолжаю цикл постов об астрологии и науке. Астрология не имеет научного обоснования, но является частью истории науки, частью культуры и общественного сознания. Поэтому астрологический взгляд на науку весьма интересен.

为了机器可读性,您可以使用 xml 输出(在您的结果下方),为了自动化,您可以将 Freeling 与 python/java 集成,但通常我更喜欢通过命令行调用它。

如果有人仍然需要它,我决定 return 这个问题并说明如何使用我之前发现的 russianmorphology 库对英语和俄语进行词形还原。

首先,您需要这些 dependencies(除了 lucene-core):

<!-- if you need Russain -->
<dependency>
    <groupId>org.apache.lucene.morphology</groupId>
    <artifactId>russian</artifactId>
    <version>1.1</version>
</dependency>

<!-- if you need English-->
<dependency>
    <groupId>org.apache.lucene.morphology</groupId>
    <artifactId>english</artifactId>
    <version>1.1</version>
</dependency>

<dependency>
    <groupId>org.apache.lucene.morphology</groupId>
    <artifactId>morph</artifactId>
    <version>1.1</version>
</dependency>

然后,确保导入正确的分析器:

import org.apache.lucene.morphology.english.EnglishAnalyzer;
import org.apache.lucene.morphology.russian.RussianAnalyzer;

与标准的 Lucene 分析器不同,这些分析器使用 MorphologyFilter 将每个单词转换为一组其规范形式。

所以如果你使用下面的代码

String text = "The stem is the part of the word that never changes even when morphologically inflected; a lemma is the base form of the word. For example, from \"produced\", the lemma is \"produce\", but the stem is \"produc-\". This is because there are words such as production";
Analyzer analyzer = new EnglishAnalyzer();
TokenStream stream = analyzer.tokenStream("field", text);
stream.reset();
while (stream.incrementToken()) {
    String lemma = stream.getAttribute(CharTermAttribute.class).toString();
    System.out.print(lemma + " ");
}
stream.end();
stream.close();

它将打印

the stem be the part of the word that never change even when morphologically inflected inflect a lemma be the base form of the word for example from produced produce the lemma be produce but the stem be produc this be because there are be word such as production

以及俄语文本

String text = "Продолжаю цикл постов об астрологии и науке. Астрология не имеет научного обоснования, но является частью истории науки, частью культуры и общественного сознания. Поэтому астрологический взгляд на науку весьма интересен.";

RussianAnalyzer 将打印以下内容:

продолжать цикл пост об астрология и наука астрология не иметь научный обоснование но являться часть частью история наука часть частью культура и общественный сознание поэтому астрологический взгляд на наука весьма интересный

你可能会注意到有些词有不止一种基本形式,例如inflected 转换为 [inflected, inflect]。如果您不喜欢这种行为,则必须更改 org.apache.lucene.morphology.analyzer.MorhpologyFilter 的实现(如果您对具体操作感兴趣,请告诉我,我会详细说明)。

希望对你有帮助,祝你好运!