首页 > 解决方案 > 修复最大匹配算法,同时使用 ntlk 标记低资源语言

问题描述

我正在努力标记一种低资源语言,但是在运行程序时我得到了一个外部错误的返回。这是代码:

D = ['چھی', 'معلومات', 'کا', 'بندی']


def max_match(sentence, dictionary):
    if not sentence:
        return ""
for i in range(len(sentence), -1, -1):
    first_word = sentence[:i]
    remainder = sentence[i:]
    if first_word in dictionary:
        return first_word + " " + max_match(remainder, dictionary)
    first_word = sentence[0]
    remainder = sentence[1:]
        return first_word + max_match(remainder, dictionary)

print (max_match('چھیمعلوماتکا بندی'), D)

错误是SyntaxError: "return" outside function

标签: pythonnltk

解决方案


您的代码的问题很简单;您未能将for循环放入函数中。你的代码应该是这样的:

D = ['چھی', 'معلومات', 'کا', 'بندی']


def max_match(sentence, dictionary):
    if not sentence:
        return ""
    for i in range(len(sentence), -1, -1):
        first_word = sentence[:i]
        remainder = sentence[i:]
        if first_word in dictionary:
            return first_word + " " + max_match(remainder, dictionary)
        first_word = sentence[0]
        remainder = sentence[1:]
        return first_word + max_match(remainder, dictionary)

print(max_match('چھیمعلوماتکا بندی', D))

推荐阅读