题目内容:实现逆向最大匹配分词算法,即从右向左扫描,找到最长的词并切分。如句子“研究生命的起源”,逆向最大匹配分词算法的输出结果为“研究生命的起源”。输入格式:第一行是以u...
题目内容:实现逆向最大匹配分词算法,即从右向左扫描,找到最长的词并切分。如句子“研究生命的起源”,逆向最大匹配分词算法的输出结果为“研究 生命 的 起源”。输入格式:第一行是以utf-8格式输入的词表,每个词之间以空格分隔。接下来是若干行以utf-8格式输入的中文句子。输出格式:以utf-8格式输出的逆向最大匹配的分词结果,每个词之间使用空格分隔。每个输入对应一行输出。oj平台上已经通过,但是在pythonIDLE(python2.7)上提示如下错误想知道是什么原因,如何更改Traceback (most recent call last): File "C:\Python27\Lib\text\test.py", line 57, in max_length, word_dict = load_dict() File "C:\Python27\Lib\text\test.py", line 3, in load_dict line = unicode(raw_input(), 'utf-8')UnicodeDecodeError: 'utf8' codec can't decode byte 0xce in position 0: invalid continuation byte相关python代码如下:def load_dict(): line = unicode(raw_input(), 'utf-8') word_dict = set() max_length = 1 words = line.split() for word in words: if len(word) > max_length: max_length = len(word) word_dict.add(word) return max_length, word_dict def bmm_word_seg(sentence, word_dict, max_length): words = [] sentence = unicode(sentence, 'utf-8') right = len(sentence) while right > 0: for left in range(max(right - max_length, 0), right,1): word = sentence[left:right] if word in word_dict or right == left + 1: words.append(word) break right = left return wordsmax_length, word_dict = load_dict()list=[]while 1: seginput=raw_input() if seginput=='': break words = bmm_word_seg(seginput, word_dict, max_length) words.reverse() list.append(words)for words in list: for word in words: print word.encode('utf-8'), print
展开