Use a list comprehension to create a list called values, using the .get() method on ner_categories to compute the values

动态 未结 0 88
小小草
小小草 LV1 2023年9月23日 23:40 发表
Here's the completed code: ``` # Import necessary modules import nltk from nltk.tokenize import sent_tokenize, word_tokenize from collections import defaultdict import matplotlib.pyplot as plt # Create the defaultdict: ner_categories ner_categories = defaultdict(int) # Create the nested for loop for sent in chunked_sentences: for chunk in sent: if hasattr(chunk, 'label'): ner_categories[chunk.label()] += 1 # Create a list from the dictionary keys for the chart labels: labels labels = list(ner_categories.keys()) # Create a list of the values: values values = [ner_categories.get(v) for v in labels] # Create the pie chart plt.pie(values, labels=labels, autopct='%1.1f%%', startangle=140) # Display the chart plt.show() ``` The code uses a list comprehension to create a list called `values` by iterating over `labels` and using the `.get()`
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复