NLP切片函数 tokenlize() 中报 TypeError: a bytes-like object is required, not ‘str‘

def tokenlize(content):
    # content = content.decode()
    content = content.replace("'s"," is")
    content = content.replace("n't"," not")
    content = re.sub("<.*?>"," " ,content)
    fileters = ["\(","\)","\t", "\n", "\x97", "\x96", "#", "$", "%","\0x93", "&", "\.", ",","!",":","\"","\'","\?","\x95"]
    content = re.sub("|".join(fileters)," ", content)
    tokens = [i.strip().lower() for i in content.split()]
    # tokens = [item.lower() for item in tokens]
    return tokens

注意到,main.py 文件中的content来源:

if __name__ == '__main__':
    ws = Word2Sequence()
    path = r"D:\data\Desktop\aclImdb_v1.tar\aclImdb_v1\aclImdb\train"
    temp_data_path = [os.path.join(path,"pos"),os.path.join(path,"neg")]
    for data_path in temp_data_path:
        file_paths = [os.path.join(data_path,file_name) for file_name in os.listdir(data_path) if file_name.endswith("txt")]
        for file_path in tqdm(file_paths):
            sentence = tokenlize(open(file_path,'rb').read())  
            ws.fit(sentence)
    ws.build_vocab(min=10,max_feature=5000)
    pickle.dump(ws, open("../pythonProject/ws.pkl",'wb'))
    print(ws)
tokenlize(open(file_path,'rb').read()) 

是磁盘上读取的字节流,因此读到的数据就是 bytes类型

需decode() 转化为 str 类型 方可进行语句的拼接替换工作

content.decode()

更新后的tokenlize()函数:

def tokenlize(content):
    content = content.decode()
    content = content.replace("'s"," is")
    content = content.replace("n't"," not")
    content = re.sub("<.*?>"," " ,content)
    fileters = ["\(","\)","\t", "\n", "\x97", "\x96", "#", "$", "%","\0x93", "&", "\.", ",","!",":","\"","\'","\?","\x95"]
    content = re.sub("|".join(fileters)," ", content)
    tokens = [i.strip().lower() for i in content.split()]
    # tokens = [item.lower() for item in tokens]
    return tokens

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值