我有一个用于文本生成的Spacy模型,我想用我的Spacy模型在每次迭代中生成的所有文本创建一个熊猫数据框架。如何将spacy.tokens.doc.Doc输出保存到熊猫数据中?
nlp = spacy.load('en_core_web_sm')
newDataSet=pd.dataframe()
docs = nlp.pipe(df['Text'])
syn_augmenter =augmenty.load('random_synonym_insertion.v1',level=0.1)
for doc in augmenty.docs(docs, augmenter=syn_augmenter, nlp=nlp):
newDataSet=newDataSet.add(doc) # this produces an error发布于 2022-07-23 11:38:01
因此,您可能希望使用DframCy库来实现这一点。这也是SpaCy:https://spacy.io/universe/project/dframcy推荐的。我使用的一个片段是:
import spacy
from dframcy import DframCy
from tqdm import tqdm
nlp = spacy.load('en_core_web_trf')
dframcy = DframCy(nlp)
columns=["id", "text", "start", "end", "pos_", "tag_", "dep_", \
"head", "ent_type_", "lemma_", "lower_", "is_punct", "is_quote", "is_digit"]
def get_features(item):
doc = dframcy.nlp(item[1]["discourse_text"])
annotation_dataframe = dframcy.to_dataframe(doc, columns=columns)
annotation_dataframe['index'] = item[0]
return annotation_dataframe
results = []
for item in tqdm(df.iterrows(), total=df.shape[0]):
results.append(get_features(item))
features = pd.concat(results)
features因此,columns对象表示您希望返回的对象。这被解析为dframcy,它提取了每个文档的特性并返回了一个很好的数据。如果您有一个要标记并从中获取特性的字符串表,则需要对其进行迭代。TQDM跟踪您的for-循环的总体进度。对数据文件列表进行对比(每个文档)将为您提供一个完整的概述。
https://stackoverflow.com/questions/73090214
复制相似问题