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))
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
class SummarizationRequestTest(unittest.TestCase): URL = 'https://api.meaningcloud.com/summarization-1.0' KEY = 'MY_KEY' TIMEOUT_DEFAULT = 60 RESOURCES_DIR = './resources/' text = 'London is big. I love this city' sentences = 1 request = meaningcloud.SummarizationRequest(KEY, txt=text, sentences=sentences) def testConstruct(self): request = self.request self.assertEqual(self.URL, request.getUrl()) self.assertIsNotNone(request.getParams()) params = request.getParams() self.assertEqual('key' in params.keys(), True) self.assertEqual(params['key'], self.KEY) self.assertIsNotNone(request.getTimeout()) self.assertEqual(self.TIMEOUT_DEFAULT, request.getTimeout()) extraHeaders = ["Accept: application/json"] request2 = meaningcloud.SummarizationRequest(self.KEY, sentences=self.sentences, txt=self.text, extraheaders=extraHeaders) self.assertIsNotNone(request2.sendReq()) otherparams = {'key2': 'my_key2'} request3 = meaningcloud.SummarizationRequest(self.KEY, sentences=self.sentences, txt=self.text, extraheaders=extraHeaders, otherparams=otherparams) self.assertIsNotNone('key2' in request3.getParams().keys(), True) self.assertEqual(request3.getParams()['key2'], 'my_key2') url = 'https://en.wikipedia.org/wiki/Star_Trek' request4 = meaningcloud.SummarizationRequest(self.KEY, sentences=self.sentences, url=url, extraheaders=extraHeaders, otherparams=otherparams) self.assertIsNotNone('url' in request4.getParams().keys(), True) self.assertEqual(request4.getParams()['url'], url) file = self.RESOURCES_DIR + 'file.txt' request5 = meaningcloud.SummarizationRequest(self.KEY, sentences=self.sentences, doc=file, extraheaders=extraHeaders, otherparams=otherparams) self.assertIsNotNone('doc' in request5.getParams().keys(), False) doc = request5._file['doc'].read().decode('utf-8') request5._file['doc'].close() aux_doc = open(self.RESOURCES_DIR + 'file.txt', 'rb') aux_conten = aux_doc.read().decode('utf-8') aux_doc.close() self.assertEqual(aux_conten, doc) return request def testSendReq(self): request = self.request requestRq = request.sendReq() self.assertIsNotNone(requestRq)
def testConstruct(self): request = self.request self.assertEqual(self.URL, request.getUrl()) self.assertIsNotNone(request.getParams()) params = request.getParams() self.assertEqual('key' in params.keys(), True) self.assertEqual(params['key'], self.KEY) self.assertIsNotNone(request.getTimeout()) self.assertEqual(self.TIMEOUT_DEFAULT, request.getTimeout()) extraHeaders = ["Accept: application/json"] request2 = meaningcloud.SummarizationRequest(self.KEY, sentences=self.sentences, txt=self.text, extraheaders=extraHeaders) self.assertIsNotNone(request2.sendReq()) otherparams = {'key2': 'my_key2'} request3 = meaningcloud.SummarizationRequest(self.KEY, sentences=self.sentences, txt=self.text, extraheaders=extraHeaders, otherparams=otherparams) self.assertIsNotNone('key2' in request3.getParams().keys(), True) self.assertEqual(request3.getParams()['key2'], 'my_key2') url = 'https://en.wikipedia.org/wiki/Star_Trek' request4 = meaningcloud.SummarizationRequest(self.KEY, sentences=self.sentences, url=url, extraheaders=extraHeaders, otherparams=otherparams) self.assertIsNotNone('url' in request4.getParams().keys(), True) self.assertEqual(request4.getParams()['url'], url) file = self.RESOURCES_DIR + 'file.txt' request5 = meaningcloud.SummarizationRequest(self.KEY, sentences=self.sentences, doc=file, extraheaders=extraHeaders, otherparams=otherparams) self.assertIsNotNone('doc' in request5.getParams().keys(), False) doc = request5._file['doc'].read().decode('utf-8') request5._file['doc'].close() aux_doc = open(self.RESOURCES_DIR + 'file.txt', 'rb') aux_conten = aux_doc.read().decode('utf-8') aux_doc.close() self.assertEqual(aux_conten, doc) return request
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])