我有一个文本语料库,其中有英文、俄文和波兰文的项目说明。
本文语料库有68K的观察结果。有些意见是用英语写的,有些用俄文写的,有些用波兰文写的。
您能告诉我,在这种情况下,cost-efficiently和是如何正确地实现一个单词的吗?我不能在俄语单词上使用英语词干器,反之亦然。
不幸的是,我找不到一个好的语言标识符。例如,langdetect工作太慢,而且常常不正确。例如,我试图识别英语单词“今天”的语言:
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))))发布于 2018-08-28 10:26:19
即使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"显然是波兰的),但它仍然会大大降低您的错误率。
https://stackoverflow.com/questions/52039155
复制相似问题