Populate News Article Summary dictionary

new_articles = {
“article_1”:“On Independence Day eve, President Murmu calls on tribals to embrace modernity”,
“article_2”:“President approves Tatrakshak Medals for Indian Coast Guard personnel | List”,
“article_3”:“Pushed by food prices, retail inflation leaps to 7.4%, a 15-month high”,
“article_4”:“Shatrughan Sinha pitches Mamata Banerjee as PM face for 2024 elections”
}

From the above dictionary populate a summary dictionary for each article as key and its summary statistics as values
Sample output:

summary_dict = {“article_1”:{
“word_count”:20,
“num_of_words_which_start_with_capital”:4,
“num_of_unique_character”:65
},
“article_2”:{
“word_count”:20,
“num_of_words_which_start_with_capital”:4,
“num_of_unique_character”:34
}
}

Post Populating the summary statistics write the data in json format with file name as news_article_summary.json

Hint for writing to json file:

import json
with open(“student.json”,‘w’) as file_handler:
file_handler.write(json.dumps(student_marks))

import json

new_articles = {
“article_1”: “On Independence Day eve, President Murmu calls on tribals to embrace modernity”,
“article_2”: “President approves Tatrakshak Medals for Indian Coast Guard personnel | List”,
“article_3”: “Pushed by food prices, retail inflation leaps to 7.4%, a 15-month high”,
“article_4”: “Shatrughan Sinha pitches Mamata Banerjee as PM face for 2024 elections”
}

summary_dict = {}

for key, article in new_articles.items():
words = article.split()
word_count = len(words)
num_capital_words = sum(1 for word in words if word[0].isupper())
num_unique_characters = len(set(article))

summary_dict[key] = {
    "word_count": word_count,
    "num_of_words_which_start_with_capital": num_capital_words,
    "num_of_unique_character": num_unique_characters
}

Save the summary data to a JSON file

with open(“news_article_summary.json”, “w”) as json_file:
json.dump(summary_dict, json_file, indent=4)

print(“Summary statistics saved to news_article_summary.json”)

> summary_dict = {}
> 
> for key, text in new_articles.items():
>     words = text.split()
>     word_count = len(words)
>     capital_word_count = len([word for word in words if word[0].isupper()])
>     unique_characters = len(set(text))
> 
>     summary_dict[key] = {
>         "word_count": word_count,
>         "num_of_words_which_start_with_capital": capital_word_count,
>         "num_of_unique_character": unique_characters
>     }
> 
> 
> with open("news_article_summary.json", "w") as file_handler:
>     json.dump(summary_dict, file_handler)
> 
> print("Check summary statistics from news_article_summary.json")

import string
def create_news_article():
summary_dict = {
article_1{
“Word_count”:(article_1.count(article_1)),
“number_of_words_which_starts_with_capital”: len(article_1.isupper()),
“number_of_unique_characters”: len(article_1.string.punctuation)
}
}

new_dict={}
for key,value in new_articles.items():
        value_dict={}
        x=new_articles[key].split(" ")
        #print(x)
        size=len(x)
        count=0
        for i in x:
            if i[0].isupper():
                count+=1
        x=set(x)
        size2=len(x)
        value_dict['word_count']=size
        value_dict['num_of_words_which_start_with_capital']=count
        value_dict['num_of_unique_character']=size2
        new_dict[key]=key
        new_dict[key]=value_dict
print(new_dict)
import json
with open('student_data.json','w') as file_handler:
    file_handler.write(json.dumps(new_dict))

new_articles = {
“article_1”:“On Independence Day eve, President Murmu calls on tribals to embrace modernity”,
“article_2”:“President approves Tatrakshak Medals for Indian Coast Guard personnel | List”,
“article_3”:“Pushed by food prices, retail inflation leaps to 7.4%, a 15-month high”,
“article_4”:“Shatrughan Sinha pitches Mamata Banerjee as PM face for 2024 elections”
}
summary_dict={}

for key,value in new_articles.items():
word=value.split( )
word_count=len(word)
upper=[word for word in word if word[0].isupper()]
num_of_unique_characters=len(set(value))
summary_dict[key]={“word_count”:word_count,
“num_of_words_which_start_with_capital”:upper,
“num_of_unique_character”:num_of_unique_characters}
print(summary_dict)

import json
with open(“news_article_summary.json”,“w”)as file_handler:
file_handler.write(json.dumps(summary_dict))

Instructor Solution

> summary_dict = {}
> for article_id,article in news_articles.items():
>     word_list = article.split()
>     word_cnt = len(word_list)
>     word_in_upper_case = [word for word in word_list if word[0].isupper()]
>     upper_case_cnt = len(word_in_upper_case)
>     num_of_unique_character = len(set(article))
>     
>     summary_dict[article_id] = {
>     "word_count":word_cnt,
>     "num_of_words_which_start_with_capital":upper_case_cnt,
>     "num_of_unique_character":num_of_unique_character
>     }
> 
> with open("summary_1.json",'w') as file_handler:
>     file_handler.write(json.dumps(summary_dict))
> 
> with open("summary_2.json",'w') as file_handler:
>     json.dump(summary_dict,file_handler)
1 Like
import json
new_articles = {
    "article_1": "On Independence Day eve, President Murmu calls on tribals to embrace modernity",
    "article_2": "President approves Tatrakshak Medals for Indian Coast Guard personnel | List",
    "article_3": "Pushed by food prices, retail inflation leaps to 7.4%, a 15-month high",
    "article_4": "Shatrughan Sinha pitches Mamata Banerjee as PM face for 2024 elections"
}

summary_dict = {}

for a_name, a_text in new_articles.items():
    words = a_text.split()
    word_count = len(words)
    num_of_words_start_with_capital = sum(1 for word in words if word[0].isupper())
    num_of_unique_characters = len(set(a_text))

    summary_dict[a_name] = {
        "word_count": word_count,
        "start_with_capital": num_of_words_start_with_capital,
        "unique_characters": num_of_unique_characters
    }


with open("news_article_summary.json", "w") as file_h:
    file_h.write(json.dumps(summary_dict))

print("Summary dictionary has been written to news_article_summary.json")