Nat*_*han 15 python nlp word-boundary text-segmentation southeast-asian-languages
我正在研究将高棉语(柬埔寨语)的长行分成单个单词(UTF-8)的解决方案.高棉语不会在单词之间使用空格.有一些解决方案,但它们远远不够(这里和这里),那些项目已经落伍了.
以下是需要拆分的高棉样本行(它们可能比这更长):
ចូរសរសើរដល់ទ្រង់ទ្រង់បានប្រទានទាំងអស់នោះមកដល់រូបដោយព្រោះអង្គព្រះយេស៊ូវហើយដែលមិនអាចរកការទាំងអស់នោះដោយសារការប្រព្រឹត្តរបស់អ្នកឡើយឡើយ.
创建分裂高棉语的可行解决方案的目标有两个:它将鼓励那些使用高棉遗留(非Unicode)字体转换为Unicode(具有许多好处)的人,并且它将使遗留的高棉语字体能够被导入进入Unicode以快速使用拼写检查器(而不是手动浏览和分割单词,使用大文档,可能需要很长时间).
我不需要100%的准确度,但速度很重要(特别是因为需要分成高棉语的行可能很长).我愿意接受建议,但目前我有一大堆高棉语单词正确分割(有一个不间断的空格),我创建了一个单词概率词典文件(frequency.csv)作为字典用于分词器.
我在这里发现这个使用Viterbi算法的 python代码,它应该运行得很快.
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()))
Run Code Online (Sandbox Code Playgroud)
我也尝试使用本页作者的源代码:文本分割:基于字典的单词分割,但它运行得太慢而无法使用(因为我的单词概率词典有超过100k的术语......).
以下是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'])
Run Code Online (Sandbox Code Playgroud)
对于python来说,我是一个新手,我对所有真正的编程(网站之外)都很陌生,所以请耐心等待.有没有人有任何他们认为会运作良好的选择?