【问题标题】:Getting sentence structure in a column dataframe在列数据框中获取句子结构
【发布时间】:2020-11-06 00:37:15
【问题描述】:

我发现将以下代码应用于我的数据集中的一列有一些困难:

import spacy
import pandas as pd

nlp = spacy.load('en_core_web_sm',disable=['ner','textcat'])

def f(text):
    pos = ""
    for token in text:
        pos += token.pos_ + " "
    return pos

df['Structure']= df.Low_Sentences.str.apply(f)

Low_Sentences 是这样的:

Low_Sentences

A sentence is a set of words that is complete in itself
Seeing a specific word used in a sentence can provide more context and help you better understand proper usage.
Short example: She walks. 

但我收到此错误:

AttributeError: 'StringMethods' 对象没有属性 'apply'

您能告诉我如何应用该函数来获取数据框中每一行的结构吗?谢谢

【问题讨论】:

  • 什么是pos_?如果text是一个字符串,token也是一个字符串并且没有为字符串定义pos_方法
  • 我正在尝试使用这个算法stackoverflow.com/questions/63763542/… 但对于每一行,不仅仅是一个字符串

标签: python pandas spacy


【解决方案1】:

只需删除 str 转换。在 pandas 中,您适用于列。

import spacy
import pandas as pd

nlp = spacy.load('en_core_web_sm',disable=['ner','textcat'])

def f(text):
    doc = nlp(text)
    pos = ""
    for token in doc:
        pos += token.pos_ + " "
    return pos

df['Structure']= df.Low_Sentences.apply(f)

仅供参考:您忘记了doc = nlp(text) 行!!

【讨论】:

  • 我收到了这个错误:AttributeError: 'str' object has no attribute 'pos_'
  • 你的f 函数不正确你到底想用你的函数做什么?
  • 什么是后置标签?请用适当的例子详细描述您的问题,人们只有在了解完整的上下文时才能帮助您
  • 我正在尝试应用此 stackoverflow.com/questions/63763542/… 但对于每一行
  • 更新了答案,马上试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 2018-04-14
相关资源
最近更新 更多