将标记化的 SpaCy 结果导出到 Excel 或 SQL 表中

Exporting Tokenized SpaCy result into Excel or SQL tables

我将 SpaCyPandas 结合使用,将句子标记化并导出为 excel 的词性 (POS)。代码如下:

import spacy
import xlsxwriter
import pandas as pd
nlp = spacy.load('en_core_web_sm')
text ="""He is a good boy."""
doc = nlp(text)
for token in doc:
    x=[token.text, token.lemma_, token.pos_, token.tag_,token.dep_,token.shape_, token.is_alpha, token.is_stop]
    print(x)

当我 print(x) 我得到以下信息:

['He', '-PRON-', 'PRON', 'PRP', 'nsubj', 'Xx', True, False]
['is', 'be', 'VERB', 'VBZ', 'ROOT', 'xx', True, True]
['a', 'a', 'DET', 'DT', 'det', 'x', True, True]
['good', 'good', 'ADJ', 'JJ', 'amod', 'xxxx', True, False]
['boy', 'boy', 'NOUN', 'NN', 'attr', 'xxx', True, False]
['.', '.', 'PUNCT', '.', 'punct', '.', False, False]

在令牌循环中,我添加了 DataFrame,如下所示: 对于文档中的标记:

for token in doc:
    x=[token.text, token.lemma_, token.pos_, token.tag_,token.dep_,token.shape_, token.is_alpha, token.is_stop]
    df=pd.Dataframe(x)
    print(df)

现在,我统计得到如下格式:

  0
0      He
1  -PRON-
2    PRON
3     PRP
4   nsubj
5      Xx
6    True
7   False   
........
........

但是,当我尝试使用 Pandas 将输出 (df) 导出到 excel 时,它只显示

列中 x 的最后一次迭代
df=pd.DataFrame(x)
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer,sheet_name='Sheet1')

输出(在ExcelSheet):

0
0      .
1      .
2  PUNCT
3      .
4  punct
5      .
6  False
7  False

在这种情况下,如何在新列中依次进行所有迭代?

 0     He      is   ….
1    -PRON-    be   ….
2     PRON    VERB  ….
3     PRP      VBZ  ….
4    nsubj     ROOT ….
5      Xx      xx   ….
6    True     True  ….
7    False   True   ….

如果您还没有您的版本:

import pandas as pd

rows =[
    ['He', '-PRON-', 'PRON', 'PRP', 'nsubj', 'Xx', True, False],
    ['is', 'be', 'VERB', 'VBZ', 'ROOT', 'xx', True, True],
    ['a', 'a', 'DET', 'DT', 'det', 'x', True, True],
    ['good', 'good', 'ADJ', 'JJ', 'amod', 'xxxx', True, False],
    ['boy', 'boy', 'NOUN', 'NN', 'attr', 'xxx', True, False],
    ['.', '.', 'PUNCT', '.', 'punct', '.', False, False],
    ]

headers = ['text', 'lemma', 'pos', 'tag', 'dep', 
           'shape', 'is_alpha', 'is_stop']

# example 1: list of lists of dicts
#following  
d = []
for row in rows:
    dict_ = {k:v for k, v in zip(headers, row)}
    d.append(dict_)
df = pd.DataFrame(d)[headers] 

# example 2: appending dicts 
df2 = pd.DataFrame(columns=headers)
for row in rows:
    dict_ = {k:v for k, v in zip(headers, row)}
    df2 = df2.append(dict_, ignore_index=True)

#example 3: lists of dicts created with map() function
def as_dict(row):
    return {k:v for k, v in zip(headers, row)}

df3 = pd.DataFrame(list(map(as_dict, rows)))[headers]     

def is_equal(df_a, df_b):
    """Substitute for pd.DataFrame.equals()"""
    return (df_a == df_b).all().all()

assert is_equal(df, df2)
assert is_equal(df2, df3)

一些更短的代码:

import spacy
import pandas as pd
nlp = spacy.load('en_core_web_sm')
text ="""He is a good boy."""
param = [[token.text, token.lemma_, token.pos_, 
          token.tag_,token.dep_,token.shape_, 
          token.is_alpha, token.is_stop] for token in nlp(text)]
df=pd.DataFrame(param)
headers = ['text', 'lemma', 'pos', 'tag', 'dep', 
           'shape', 'is_alpha', 'is_stop']
df.columns = headers