python - 分词高棉语的可行解决方案?

标签 python nlp word-boundary text-segmentation southeast-asian-languages

我正在研究一种解决方案,将长行的高棉语(柬埔寨语)拆分为单个单词(UTF-8)。高棉语单词之间不使用空格。有一些解决方案,但它们还远远不够(herehere),而且这些项目已经半途而废。

这是需要拆分的高棉示例行(它们可以比这更长):

ចូរសរសើរដល់ទ្រង់ដែលទ្រង់បានប្រទានការទាំងអស់នោះមកដល់រូបអ្នកដោយព្រោះអង្គព្រះយេស៊ូវ ហើយដែលអ្នកមិនអាចរកការទាំងអស់នោះដោយសារការប្រព្រឹត្តរបស់អ្នកឡើយ។

创建拆分高棉语单词的可行解决方案的目标是双重的:它将鼓励那些使用高棉遗留(非 Unicode)字体的人转换为 Unicode(它有很多好处),并且它将启用遗留高棉字体导入到 Unicode 以快速与拼写检查器一起使用(而不是手动检查和拆分单词,对于大文档,可能需要很长时间)。

我不需要 100% 的准确性,但速度很重要(特别是因为需要拆分成高棉语单词的行可能会很长)。 我乐于接受建议,但目前我有大量高棉语单词语料库,这些单词被正确分割(使用不间断空格),并且我创建了一个单词概率字典文件 (frequency.csv) 用作字典分词器。

我找到了这个 python 代码 here使用 Viterbi algorithm而且据说它运行得很快。

import re
from itertools import groupby

def viterbi_segment(text):
    probs, lasts = [1.0], [0]
    for i in range(1, len(text) + 1):
        prob_k, k = max((probs[j] * word_prob(text[j:i]), j)
                        for j in range(max(0, i - max_word_length), i))
        probs.append(prob_k)
        lasts.append(k)
    words = []
    i = len(text)
    while 0 < i:
        words.append(text[lasts[i]:i])
        i = lasts[i]
    words.reverse()
    return words, probs[-1]

def word_prob(word): return dictionary.get(word, 0) / total
def words(text): return re.findall('[a-z]+', text.lower()) 
dictionary = dict((w, len(list(ws)))
                  for w, ws in groupby(sorted(words(open('big.txt').read()))))
max_word_length = max(map(len, dictionary))
total = float(sum(dictionary.values()))

我还尝试使用此页面作者的源 Java 代码:Text segmentation: dictionary-based word splitting但它运行得太慢而没有任何用处(因为我的单词概率词典有超过 10 万个术语......)。

这是来自 Detect most likely words from text without spaces / combined words 的另一个 python 选项:

WORD_FREQUENCIES = {
    'file': 0.00123,
    'files': 0.00124,
    'save': 0.002,
    'ave': 0.00001,
    'as': 0.00555
}

def split_text(text, word_frequencies, cache):
    if text in cache:
        return cache[text]
    if not text:
        return 1, []
    best_freq, best_split = 0, []
    for i in xrange(1, len(text) + 1):
        word, remainder = text[:i], text[i:]
        freq = word_frequencies.get(word, None)
        if freq:
            remainder_freq, remainder = split_text(
                    remainder, word_frequencies, cache)
            freq *= remainder_freq
            if freq > best_freq:
                best_freq = freq
                best_split = [word] + remainder
    cache[text] = (best_freq, best_split)
    return cache[text]

print split_text('filesaveas', WORD_FREQUENCIES, {})

--> (1.3653e-08, ['file', 'save', 'as'])

我是 python 的新手,我对所有真正的编程(网站之外)都是新手,所以请多多包涵。有没有人有任何他们认为行之有效的选择?

最佳答案

ICU 库(有 Python 和 Java 绑定(bind))有一个 DictionaryBasedBreakIterator可用于此的类。

关于python - 分词高棉语的可行解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4861619/

相关文章:

Python类解释

Python 名称错误 : Name is not defined Linux

python - 如何让 pyodbc 在 Azure Web App 中工作

java - 从java中的词干文本中提取专有名词

algorithm - 基于相似词序列的聚类字符串

python - regex.WORD 如何影响\b 的行为?

python - 检测图像中对象的宽度

python - 如何将损失函数中的变量存储到实例变量中

regex - 正则表达式中的单词边界是什么?

r - 单词边界正则表达式问题