def textSummarization(text): try: # We are going to make a request to the Topics Extraction API summarization_response = meaningcloud.SummarizationResponse( meaningcloud.SummarizationRequest(license_key, txt=text).sendReq()) # summarization_response = meaningcloud.TopicsResponse(meaningcloud.TopicsRequest(license_key, txt=text, lang='en', # topicType='e').sendReq()) # If there are no errors in the request, we print the output if summarization_response.isSuccessful(): #print("\nThe request to 'Text Summarization' finished successfully!\n") summary = summarization_response.getSummary() if summary: # print(text) # print(summary) summary = re.sub("[\(\[].*?[\)\]]", "", summary) return summary else: print("\tNo entities detected!\n") else: print("The summarization request didn't work") except ValueError: e = sys.exc_info()[0] print("\nException: " + str(e))
class SummarizationResponseTest(unittest.TestCase): outputOK = { "status": { "code": "0", "msg": "OK", "credits": "1", "remaining_credits": "32014" }, "summary": "London is big." } outputOK = json.dumps(outputOK) response = meaningcloud.SummarizationResponse(outputOK) outputEmpty = { "status": { "code": "0", "msg": "OK", "credits": "1", "remaining_credits": "32014" }, "summary": "" } empty_response = meaningcloud.SummarizationResponse( json.dumps(outputEmpty)) def testConstruct(self): self.assertIsNotNone(self.response.getResponse()) def testConstructWithWrongJson(self): outputWrong = 'malformed json' with self.assertRaises(json.JSONDecodeError): meaningcloud.SummarizationResponse(outputWrong) def testConstructWithEmptyParam(self): with self.assertRaises(Exception): meaningcloud.SummarizationResponse('') def testGetSummary(self): self.assertIsNotNone(self.response.getSummary()) def testGetSummaryWithEmptyInput(self): self.assertEqual(self.empty_response.getSummary(), '')
def getSummarization(text, sentences): # We are going to make a request to the Summarization API summary = '' print("\tGetting automatic summarization...") summarization_response = meaningcloud.SummarizationResponse(meaningcloud.SummarizationRequest(license_key, sentences=sentences, txt=text).sendReq()) if summarization_response.isSuccessful(): summary = summarization_response.getSummary() else: print("\tOops! Request to Summarization was not succesful: (" + summarization_response.getStatusCode() + ') ' + summarization_response.getStatusMsg()) return summary
def summarizeArticle(articleURL): model = 'IAB_en' #basically specifies that the language is english key = '9cb4dbf4b67056958232d52630d21988' #to access any API you need to pass your personal key into the request. This is our key summary = '' summarization_response = meaningcloud.SummarizationResponse( meaningcloud.SummarizationRequest(key, sentences=3, url=articleURL).sendReq()) if summarization_response.isSuccessful(): summary = summarization_response.getSummary() return summary else: print("\tOops! Request to Summarization was not succesful: (" + summarization_response.getStatusCode() + ') ' + summarization_response.getStatusMsg())
def summary(): try: # summary api call summary = '' print("\tGetting automatic summarization...") summarization_response = meaningcloud.SummarizationResponse( meaningcloud.SummarizationRequest(license_key, sentences=4, doc=document).sendReq()) if summarization_response.isSuccessful(): summary = summarization_response.getSummary() print(summary) else: print("\tOops! Request to Summarization was not succesful: (" + summarization_response.getStatusCode() + ') ' + summarization_response.getStatusMsg()) except ValueError: e = sys.exc_info()[0] print("\nException: " + str(e))
def analyzeText(text): global index_count print("Extracting summary for text #%s" % str(index_count)) # this is where we are going to store our results summary = "" try: # We are going to make a request to the Summarization API request = meaningcloud.SummarizationRequest(license_key, sentences=sentences, txt=text, server=server) setRequestSource(request) response = meaningcloud.SummarizationResponse(request.sendReq()) if response.isSuccessful(): summary = response.getSummary() else: if isBlockingErrorType(response.getStatusCode()): raise ValueError( "Something went wrong in the MeaningCloud request!: (" + response.getStatusCode() + ") " + response.getStatusMsg()) else: print("Oops! The request to Summarization for text #" + str(index_count) + " was not succesful: (" + response.getStatusCode() + ") " + response.getStatusMsg()) summary = ("ERROR (" + response.getStatusCode() + "): " + response.getStatusMsg()) except ValueError as e: raise ValueError(str(e)) index_count += 1 return pd.Series([summary])
def testConstructWithEmptyParam(self): with self.assertRaises(Exception): meaningcloud.SummarizationResponse('')
def testConstructWithWrongJson(self): outputWrong = 'malformed json' with self.assertRaises(json.JSONDecodeError): meaningcloud.SummarizationResponse(outputWrong)