如何在数据帧内进行句子标记化

2024-09-24 06:27:27 发布

您现在位置:Python中文网/ 问答频道 /正文

“我的数据框架”中的一列包含以下文本:

'This is very good. No it is very bad. Actually it is alright'

我想对本专栏中的文本进行句子标记,主要是创建一个嵌套的句子列表

我试过了

def tokenizeAndList(text):

    raw_text = text
    nlp = English()
    nlp.add_pipe(nlp.create_pipe('sentencizer')) # updated
    doc = nlp(raw_text)
    sentences = [sent.string.strip() for sent in doc.sents]
    return(sentences)

out=myText['findings'].map(tokenizeAndList)

这给了我一个错误:

TypeError: object of type 'NAType' has no len()

如何创建嵌套列表


Tags: 数据text文本列表rawdocnlpis
1条回答
网友
1楼 · 发布于 2024-09-24 06:27:27

这是因为findings列中的某些值不是字符串类型

在使用该text创建空间文档之前,应检查其类型是否为str,否则按原样返回值:

nlp = English()
nlp.add_pipe(nlp.create_pipe('sentencizer'))

def tokenizeAndList(text):
    if isinstance(text, str):
        doc = nlp(text)
        return [sent.string.strip() for sent in doc.sents]
    else:
        return text

相关问题 更多 >