【问题标题】:Stemming of the multilingual text corpus多语言文本语料库的词干提取
【发布时间】:2019-02-01 23:34:58
【问题描述】:

我有一个包含英语、俄语和波兰语项目描述的文本语料库。

这个文本语料库有 68K 观察。其中一些观察是用英语写的,一些是用俄语写的,还有一些是用波兰语写的。

您能否告诉我在这种情况下如何正确经济高效地实施词干提取?我不能对俄语单词使用英语词干分析器,反之亦然。

很遗憾,我找不到合适的语言标识符。例如。 langdetect 工作太慢而且经常出错。例如,我尝试识别英语单词“today”的语言:

detect("today") 
"so" 
# i.e Somali 

到目前为止,我的代码实现看起来很糟糕。我只是在另一个上使用一个词干分析器:

import nltk
# polish stemmer
from pymorfologik import Morfologik

clean_items = []

# create stemmers

snowball_en = nltk.SnowballStemmer("english")
snowball_ru = nltk.SnowballStemmer("russian")
stemmer_pl = Morfologik()

# loop over each item; create an index i that goes from 0 to the length
# of the item list 

for i in range(0, num_items):
    # Call our function for each one, and add the result to the list of
    # clean items

    cleaned = items.iloc[i]

    # to word stem
    clean_items.append(snowball_ru.stem(stemmer_pl(snowball_en.stem(cleaned))))

【问题讨论】:

  • 如何先从文本的句子/标记中检测语言,然后使用适当的词干分析器?
  • 您可以通过利用字符的存在和/或频率以及语音来制作粗略的单词语言分类器。您甚至可以添加第四类,其中包含无法分类的单词,并且可能由于长度甚至不需要分类(例如英语文章“a”,捷克语连词“a”)。
  • 我没用过langid。感谢您的建议,我会尝试使用它!
  • 这就是我建议您制作自己的分类器的部分原因。但即使langdetect 可以调整它(因为老实说API 有点混乱:P)。如果你只有英语、俄语和捷克语,为什么还要看看是不是索马里语?见here

标签: python nlp nltk text-processing stemming


【解决方案1】:

即使 API 不是那么好,您也可以让 langdetect 将自己限制为仅使用您实际使用的语言。例如:

from langdetect.detector_factory import DetectorFactory, PROFILES_DIRECTORY
import os

def get_factory_for(langs):
    df = DetectorFactory()
    profiles = []
    for lang in ['en', 'ru', 'pl']:
        with open(os.path.join(PROFILES_DIRECTORY, lang), 'r', encoding='utf-8') as f:
            profiles.append(f.read())
    df.load_json_profile(profiles)

    def _detect_langs(text):
        d = df.create()
        d.append(text)
        return d.get_probabilities()

    def _detect(text):
        d = df.create()
        d.append(text)
        return d.detect()

    df.detect_langs = _detect_langs
    df.detect = _detect
    return df

虽然不受限制的langdetect 似乎认为"today" 是索马里语,但如果您只有英语、俄语和波兰语,您现在可以这样做:

df = get_factory_for(['en', 'ru', 'pl'])
df.detect('today')         # 'en'
df.detect_langs('today')   # [en:0.9999988994459187]

它仍然会遗漏很多东西("snow" 显然是波兰语),但它仍然会大大降低您的错误率。

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    相关资源
    最近更新 更多