gpt4 book ai didi

python - UnicodeDecodeError,python中Snowball stemming算法的ascii处理

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:58 27 4
gpt4 key购买 nike

我在将一般文件读入我制作的程序时遇到了一些问题。我目前遇到的问题是 pdf 基于某种变异的 utf-8,包括一个 BOM,这会给我的整个操作带来麻烦。在我的应用程序中,我使用需要 ascii 输入的 Snowball 词干提取算法。有许多主题涉及获取错误以解决 utf-8,但是没有一个主题涉及将它们发送到 Snowball 算法中,或者考虑 ascii 是我想要的最终结果这一事实。目前我使用的文件是一个使用标准 ANSI 编码的记事本文件。我得到的具体错误信息是这样的:

File "C:\Users\svictoroff\Desktop\Alleyoop\Python_Scripts\Keywords.py", line 38, in Map_Sentence_To_Keywords
Word = Word.encode('ascii', 'ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 0: ordinal not in range(128)

我的理解是,在 python 中,包括 ignore 参数会简单地传递遇到的任何非 ascii 字符,这样我会绕过任何 BOM 或特殊字符,但显然情况并非如此。实际调用的代码在这里:

def Map_Sentence_To_Keywords(Sentence, Keywords):
'''Takes in a sentence and a list of Keywords, returns a tuple where the
first element is the sentence, and the second element is a set of
all keywords appearing in the sentence. Uses Snowball algorithm'''
Equivalence = stem.SnowballStemmer('english')
Found = []
Sentence = re.sub(r'^(\W*?)(.*)(\n?)$', r'\2', Sentence)
Words = Sentence.split()
for Word in Words:
Word = Word.lower().strip()
Word = Word.encode('ascii', 'ignore')
Word = Equivalence.stem(Word)
Found.append(Word)
return (Sentence, Found)

通过将一般的非贪婪非字符正则表达式删除添加到字符串的前面,我也希望删除有问题的字符,但事实并非如此。除了 ascii 之外,我还尝试了许多其他编码,并且严格的 base64 编码有效,但对于我的应用程序来说非常不理想。关于如何以自动方式解决此问题的任何想法?

元素的初始解码失败,但在实际传递给编码器时返回 unicode 错误。

for Element in Curriculum_Elements:
try:
Element = Element.decode('utf-8-sig')
except:
print Element
Curriculum_Tuples.append(Map_Sentence_To_Keywords(Element, Keywords))

def scraping(File):
'''Takes in txt file of curriculum, removes all newlines and returns that occur \
after a lowercase character, then splits at all remaining newlines'''
Curriculum_Elements = []
Document = open(File, 'rb').read()
Document = re.sub(r'(?<=[a-zA-Z,])\r?\n', ' ', Document)
Curriculum_Elements = Document.split('\r\n')
return Curriculum_Elements

显示的代码生成看到的类(class)元素。

 for Element in Curriculum_Elements:
try:
Element = unicode(Element, 'utf-8-sig', 'ignore')
except:
print Element

这种类型转换 hackaround 实际上有效,但是转换回 ascii 有点不稳定。返回此错误:

Warning (from warnings module):
File "C:\Python27\lib\encodings\utf_8_sig.py", line 19
if input[:3] == codecs.BOM_UTF8:
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal

最佳答案

首先尝试将 UTF-8 输入解码为 unicode 字符串,然后将其编码为 ASCII(忽略非 ASCII)。对已经编码的字符串进行编码确实没有意义。

input = file.read()   # Replace with your file input code...
input = input.decode('utf-8-sig') # '-sig' handles BOM

# Now isinstance(input, unicode) is True

# ...
Sentence = Sentence.encode('ascii', 'ignore')

编辑后,我看到您已经在尝试对字符串进行解码,然后再将它们编码为 ASCII。但是,在文件内容已经被操纵之后,解码似乎发生得太晚了。这可能会导致问题,因为并非每个 UTF-8 字节都是一个字符(某些字符需要几个字节来编码)。想象一下将任何字符串转换为 ab 序列的编码。你不想在解码之前对其进行操作,因为你会到处看到 ab,即使在未编码的字符串中没有任何东西—— UTF-8 也会出现同样的问题,尽管要微妙得多,因为大多数 字节实际上是字符。

所以,在你做任何其他事情之前解码一次:

def scraping(File):
'''Takes in txt file of curriculum, removes all newlines and returns that occur \
after a lowercase character, then splits at all remaining newlines'''
Curriculum_Elements = []
Document = open(File, 'rb').read().decode('utf-8-sig')
Document = re.sub(r'(?<=[a-zA-Z,])\r?\n', ' ', Document)
Curriculum_Elements = Document.split('\r\n')
return Curriculum_Elements

# ...

for Element in Curriculum_Elements:
Curriculum_Tuples.append(Map_Sentence_To_Keywords(Element, Keywords))

您的原始 Map_Sentence_To_Keywords 函数无需修改即可运行,但我建议在拆分前编码为 ASCII,以提高效率/可读性。

关于python - UnicodeDecodeError,python中Snowball stemming算法的ascii处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10916613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com