class WikiMedia: """Wikipedia class.""" def __init__(self): self.wikipedia = MediaWiki() self.wikipedia.language = "fr" def get_infos(self, query): """Method allowing to retrieve informations from wikipedia.fr.""" try: titles = self.wikipedia.search(query) if len(titles) > 0: infos = self.wikipedia.page(titles[0]) summary = self.wikipedia.summary(titles[0], sentences=3) # Add regex to remove == string == in summary: summary = re.sub(r"={2}\s.+={2}", r"", summary) status = True url = infos.url # Return empty results if no titles are return from the API else: summary = "" url = "" status = False # Use one except block in case of disambiguations errors. # Allow to search for the next title if the first one lead # to a disambiguation error. except mediawiki.exceptions.DisambiguationError: if len(titles) > 1: try: infos = self.wikipedia.page(titles[1]) summary = self.wikipedia.summary(titles[1], sentences=3) summary = re.sub(r"={2}\s.+={2}", r"", summary) url = infos.url status = True except mediawiki.exceptions.DisambiguationError: summary = "" url = "" status = False logging.exception("Exception occurred") else: summary = "" url = "" status = False logging.exception("Exception occurred") return {"summary": summary, "url": url, "status": status}
def wikipedia_summary(topic, lang='en'): wikipedia = MediaWiki(lang=lang) search = wikipedia.search(topic) summary = wikipedia.summary(search[0]) text = '**{}**\n\n{}\n**Read more at:** [{}]({})'.format( page.title, summary, page.title, page.url) return text
def get_prediction(): wikipedia = MediaWiki() word = request.args.get('word') # Set stop words language stop_words = get_stop_words('en') stop_words = get_stop_words('english') # split query filtered_sentence = "" filtered_sentence = word.split() reponse = [] for each in filtered_sentence: if each not in stop_words: reponse.append(each) string_query = ' '.join(reponse) serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?' address = string_query if len(address) < 1: return try: url = serviceurl + "key=" + app.config['KEY_API'] +\ "&" + urllib.parse.urlencode({'address': address}) uh = urllib.request.urlopen(url) data = uh.read().decode() js = json.loads(data) except: print('==== Failure URL ====') js = None if not js: if 'status' not in js: if js['status'] != 'OK': print('==== Failure To Retrieve ====') print(js) else: lat = js["results"][0]["geometry"]["location"]["lat"] lng = js["results"][0]["geometry"]["location"]["lng"] # sent coordinates to Media wiki query = wikipedia.geosearch(str(lat), str(lng)) # Save first answer history = query[0] # sent answer to Media wiki summary = wikipedia.summary(history) # return summary to view html return jsonify({'html': summary})
def message(coordinates): data = json.loads(coordinates) # get data from json latitude = data[0] longitude = data[1] address = data[2] # instanciation wikipedia object wikipedia = MediaWiki() # sent coordinates to Media wiki query = wikipedia.geosearch(str(latitude), str(longitude)) # Save first answer history = query[0] # sent answer to Media wiki summary = wikipedia.summary(history) # format answer to display answer = "Of course! There she is : " + address +\ ". But have I already told you his history: " + summary return (answer)