【问题标题】:How to create a list of tokenized words from dataframe column using spaCy?如何使用 spaCy 从数据框列创建标记词列表?
【发布时间】:2022-07-22 23:03:33
【问题描述】:

我正在尝试在数据框列上应用 spaCys 标记器以获取包含标记列表的新列。 假设我们有以下数据框:

import pandas as pd
details = {
    'Text_id' : [23, 21, 22, 21],
    'Text' : ['All roads lead to Rome', 
              'All work and no play makes Jack a dull buy', 
              'Any port in a storm', 
              'Avoid a questioner, for he is also a tattler'],
}
  
# creating a Dataframe object 
example_df = pd.DataFrame(details)

以下代码旨在标记Text 列:

import spacy

nlp = spacy.load("en_core_web_sm")

example_df["tokens"] = example_df["Text"].apply(lambda x: nlp.tokenizer(x))

example_df

结果如下:

现在,我们有一个新列 tokens,它为每个句子返回 doc 对象。

我们如何更改代码以获得标记词的python列表

我尝试了以下行:

example_df["tokens"] = example_df["Text"].apply(token.text for token in (lambda x: nlp.tokenizer(x)))

但我有以下错误:

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_33/3712416053.py in <module>
     14 nlp = spacy.load("en_core_web_sm")
     15 
---> 16 example_df["tokens"] = example_df["Text"].apply(token.text for token in (lambda x: nlp.tokenizer(x)))
     17 
     18 example_df

TypeError: 'function' object is not iterable

提前谢谢你!

【问题讨论】:

    标签: python pandas nlp spacy tokenize


    【解决方案1】:

    你可以使用

    example_df["tokens"] = example_df["Text"].apply(lambda x: [t.text for t in nlp.tokenizer(x)])
    

    查看 Pandas 测试:

    import pandas as pd
    details = {
        'Text_id' : [23, 21, 22, 21],
        'Text' : ['All roads lead to Rome', 
                  'All work and no play makes Jack a dull buy', 
                  'Any port in a storm', 
                  'Avoid a questioner, for he is also a tattler'],
    }
      
    # creating a Dataframe object 
    example_df = pd.DataFrame(details)
    import spacy
    
    nlp = spacy.load("en_core_web_sm")
    
    example_df["tokens"] = example_df["Text"].apply(lambda x: [t.text for t in nlp.tokenizer(x)])
    
    print(example_df.to_string())
    

    输出:

       Text_id                                          Text                                                    tokens
    0       23                        All roads lead to Rome                              [All, roads, lead, to, Rome]
    1       21    All work and no play makes Jack a dull buy     [All, work, and, no, play, makes, Jack, a, dull, buy]
    2       22                           Any port in a storm                                 [Any, port, in, a, storm]
    3       21  Avoid a questioner, for he is also a tattler  [Avoid, a, questioner, ,, for, he, is, also, a, tattler]
    

    【讨论】:

      【解决方案2】:

      试试这个

      example_df["tokens"] = example_df["Text"].apply(lambda x : [token.text for token in nlp.tokenizer(x)])
      

      这给了我们

      【讨论】:

      • NameError: name 'x' is not defined
      • 更新了我的答案
      猜你喜欢
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2022-11-28
      • 2020-09-19
      • 2022-11-09
      • 1970-01-01
      相关资源
      最近更新 更多