【问题标题】:NLP: Tokenize : TypeError: expected string or bytes-like objectNLP:Tokenize:TypeError:预期的字符串或类似字节的对象
【发布时间】:2022-01-04 05:33:24
【问题描述】:

在标记化之前我也尝试过 .apply(str) 和 .astype(str),但我得到 TypeError: expected string or bytes-like object。


data.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 3 columns):
 #   Column           Non-Null Count  Dtype 
---  ------           --------------  ----- 
 0   tag              8 non-null      object
 1   clean_patterns   8 non-null      object
 2   clean_responses  8 non-null      object
dtypes: object(3)
memory usage: 320.0+ bytes

我正在尝试对 NLP 聊天机器人的数据进行 word_tokenize。


print(word_tokenize(data))

TypeError Traceback(最近调用 最后)在 ----> 1 个打印(word_tokenize(数据))

D:\anaconda\lib\site-packages\nltk\tokenize_init_.py in word_tokenize(文本,语言,preserve_line) 128:类型保留线:布尔 第129章 --> 130 个句子 = [text] if preserve_line else sent_tokenize(text, language) 131返回[ 132 token for sent in sentence for token in _treebank_word_tokenizer.tokenize(sent)

D:\anaconda\lib\site-packages\nltk\tokenize_init_.py in sent_tokenize(文本,语言) 106 """ 107 标记器 = 加载(“标记器/punkt/{0}.pickle”.format(语言)) --> 108 返回 tokenizer.tokenize(text) 109 110

D:\anaconda\lib\site-packages\nltk\tokenize\punkt.py in tokenize(self, text, realign_boundaries) 1272 给定一个文本,返回一个列表 该文本中的句子。第1273章 -> 1274 返回列表(self.sentences_from_text(text,realign_boundaries))1275 1276 def debug_decisions(self, 文本):

D:\anaconda\lib\site-packages\nltk\tokenize\punkt.py 在 sentence_from_text(self, text, realign_boundaries) 1326
跟随期间。第1327章 -> 1328 return [text[s:e] for s, e in self.span_tokenize(text, realign_boundaries)] 1329 1330 def _slices_from_text(self, 文本):

D:\anaconda\lib\site-packages\nltk\tokenize\punkt.py in (.0) 1326 跟随期间。第1327章 -> 1328 return [text[s:e] for s, e in self.span_tokenize(text, realign_boundaries)] 1329 1330 def _slices_from_text(self, 文本):

D:\anaconda\lib\site-packages\nltk\tokenize\punkt.py 在 span_tokenize(self, text, realign_boundaries) 1316 如果 重新对齐边界:1317 片 = self._realign_boundaries(文本,切片) -> 1318 for sl in slices: 1319 yield (sl.start, sl.stop) 1320

D:\anaconda\lib\site-packages\nltk\tokenize\punkt.py 在 _realign_boundaries(self, text, slices) 1357 """ 1358 realign = 0 -> 1359 for sl1, sl2 in _pair_iter(slices): 1360 sl1 = slice(sl1.start + realign, sl1.stop) 1361 如果不是 sl2:

D:\anaconda\lib\site-packages\nltk\tokenize\punkt.py in _pair_iter(it) 第314章 315尝试: --> 316 上一个 = 下一个(它) 317 除了 StopIteration: 318返回

D:\anaconda\lib\site-packages\nltk\tokenize\punkt.py 在 _slices_from_text(self, text) 1330 def _slices_from_text(self, text): 1331 last_break = 0 -> 1332 匹配 self._lang_vars.period_context_re().finditer(text):1333
上下文 = match.group() + match.group("after_tok") 1334
if self.text_contains_sentbreak(context):

TypeError:预期的字符串或类似字节的对象

【问题讨论】:

  • 您在数据帧上调用 word_tokenize,它不是 str 或 bytes 对象。您的意思是在每一行的列上运行它吗?

标签: python python-3.x nlp chatbot tokenize


【解决方案1】:

欢迎来到 SO ;) 给定以下数据框data 和函数word_tokenize,您必须这样做

import pandas as pd
def word_tokenize(sentence):
  return sentence.split()
data = pd.DataFrame(data={'col1': ['bar bar bar foo', 
                                 'foo foo foo bar', 124],
                        'col2': [12, 13, 14]})

以最简单的方式在col1 上应用函数

df['col1'].astype(str).apply(word_tokenize)
#ouput
0    [bar, bar, bar, foo]
1    [foo, foo, foo, bar]
2                   [124]
Name: col1, dtype: objec

首先将类型更改为str,然后将函数应用于每个元素。输出将是pandas.core.series.Series

【讨论】:

    猜你喜欢
    • 2019-10-31
    • 1970-01-01
    • 2018-10-17
    • 2020-07-08
    • 2018-11-21
    • 2018-05-04
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多