0

I am trying to save the output summarized text to a text file without manually copying the output, I have a function called generate_summary(), this is the one I'm trying to save the output from. this is my code, I think I want to save the list in the function:

def generate_summary(file_name, top_n=7):
   stop_words = stopwords.words('english')
   summarizaed_text = []

   print("original Text:\n")
   sentences =  read_article("AI.txt")

   sentence_similarity_martix = build_similarity_matrix(sentences, stop_words)

   sentence_similarity_graph = nx.from_numpy_array(sentence_similarity_martix)
   scores = nx.pagerank(sentence_similarity_graph)

   ranked_sentence = sorted(((scores[i],s) for i,s in enumerate(sentences)), reverse=True)    
   print("\nIndexes of top ranked_sentence order:\n \n ", ranked_sentence)    

   for i in range(top_n):
     summarizaed_text.append(" ".join(ranked_sentence[i][1]))

   print("\nSummarized Text:\n \n", ". ".join(summarizaed_text))

I tried running the below code but it didn't work, can someone please help me? thank you in advance:

with open('Summaty.txt', 'w') as f:
generate_summary(f)
Ola Ayman
  • 9
  • 1
  • What specifically did not work? There is a lot going on in your function. Was there an error? If so, provide the full traceback in your code. If the output is different from what you expected, include both the output you received and the output you expected. And If the problem lies in the other functions and libraries you are using, you will need to include the relevant code. – AlexK May 22 '22 at 03:59
  • Code to save the summarized output from the generate_summary() function into a text file called summary, the code itself works perfectly fine, I apologize for lack of info. here's a link to the full code: https://drive.google.com/file/d/1XyeZ4FqIYWWSrdiNG8ZIo5or74LhVinG/view?usp=sharing – Ola Ayman May 22 '22 at 04:10
  • Your print statements do not send anything to the file right now. You are not even using your file object in the function. See [Correct way to write line to file](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) or [Reading and Writing Files](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files). – AlexK May 22 '22 at 04:26
  • thank you very much, I converted it to string and it worked! – Ola Ayman May 22 '22 at 04:52

0 Answers0