我有一些示例代码,可以使用 Google 的自然语言 API 来分析实体及其情绪。对于 Pandas 数据框中的每条记录,我想返回一个字典列表,其中每个元素都是一个实体。然而,当我尝试让它在生产数据上工作时遇到了问题。这是示例代码from google.cloud import language_v1 # version 2.0.0import os os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/json'import pandas as pd # establish client connectionclient = language_v1.LanguageServiceClient()# helper function def custom_analyze_entity(text_content): global client #print("Accepted Input::" + text_content) document = language_v1.Document(content=text_content, type_=language_v1.Document.Type.PLAIN_TEXT, language = 'en') response = client.analyze_entity_sentiment(request = {'document': document}) # a document can have many entities # create a list of dictionaries, every element in the list is a dictionary that represents an entity # the dictionary is nested l = [] #print("Entity response:" + str(response.entities)) for entity in response.entities: #print('=' * 20) temp_dict = {} temp_meta_dict = {} temp_mentions = {} temp_dict['name'] = entity.name temp_dict['type'] = language_v1.Entity.Type(entity.type_).name temp_dict['salience'] = str(entity.salience) sentiment = entity.sentiment temp_dict['sentiment_score'] = str(sentiment.score) temp_dict['sentiment_magnitude'] = str(sentiment.magnitude) for metadata_name, metadata_value in entity.metadata.items(): temp_meta_dict['metadata_name'] = metadata_name temp_meta_dict['metadata_value'] = metadata_value temp_dict['metadata'] = temp_meta_dict for mention in entity.mentions: temp_mentions['mention_text'] = str(mention.text.content) temp_mentions['mention_type'] = str(language_v1.EntityMention.Type(mention.type_).name) temp_dict['mentions'] = temp_mentions #print(u"Appended Entity::: {}".format(temp_dict)) l.append(temp_dict) return l
1 回答
慕后森
TA贡献1802条经验 获得超5个赞
尝试这样做:
input_df.loc[0, 'entity_object'] = ""
for i in range(len(input_df)):
op = custom_analyze_entity(input_df.loc[i,'freeform_text'])
input_df.loc[i, 'entity_object'] = op
或者对于您的具体情况,您不需要使用loc函数。
input_df["entity_object"] = ""
for i in range(len(input_df)):
op = custom_analyze_entity(input_df.loc[i,'freeform_text'])
input_df["entity_object"][i] = op
添加回答
举报
0/150
提交
取消