【发布时间】:2017-03-09 03:15:38
【问题描述】:
这是一个将随机生成的句子发送到不和谐聊天中的脚本。但它偶尔会遇到错误:UnicodeDecodeError: 'ascii' codec cant decode byte 0xef in position 2141: ordinal not in range(128)
我该如何解决这个错误?
代码:
import asyncio
import random
import discord.ext.commands
import markovify
import nltk
import re
with open("/root/sample.txt") as f:
text = f.read()
class POSifiedText(markovify.Text):
def word_split(self, sentence):
words = re.split(self.word_split_pattern, sentence)
words = [w for w in words if len(w) > 0]
words = [" :: ".join(tag) for tag in nltk.pos_tag(words)]
return words
def word_join(self, words):
sentence = "".join(word.split("::")[0] for word in words)
return sentence
text_model = POSifiedText(text, state_size=1)
client = discord.Client()
async def background_loop():
await client.wait_until_ready()
while not client.is_closed:
channel = client.get_channel('286342556600762369')
messages = [(text_model.make_sentence(tries=33, max_overlap_total=10, default_max_overlap_ratio=0.5))]
await client.send_message(channel, random.choice(messages))
await asyncio.sleep(15)
client.loop.create_task(background_loop())
client.run("MjY2NjkwNDY4MjI4NzU5NTU4.C5jcdw.WFfBTUmAY7UcrwKTwYFJ9_bFHjI")
错误发生在第 9 行。
【问题讨论】:
-
您的样本包含非 ascii 字符。在
open()调用中,指定正确的编码。 -
但是您的问题与 nltk 或您正在构建的聊天机器人无关。停止标记所有内容
nltk;缩小你的问题范围,很容易用谷歌搜索解决方案。
标签: python python-3.x