Wiki Process Flow

Getting Results from Wikipedia

Transformed Text is passed to wikiSearch flow to get the Summary from Wikipedia. By default Aryan gets 2 sentences from Wikipedia for the given Human query.
If Human explicitly mentions to get details about a particular topic, then Aryan gets 5 sentences from Wikipedia, This is achieved by passing a detailFlag as "Y" by the Speech Action flow which invokes Wikipedia flow.
Wikipedia is a Python library that makes it easy to access and parse data from Wikipedia.
Search Wikipedia, get article summaries, get data like links and images from a page, and more. Wikipedia wraps the MediaWiki API so you can focus on using Wikipedia data, not getting it.
1
'''When the detailFlag is set as Y then get the 5 sentences from Wikipedia, else get only 2 sentences per user request'''
2
if detailFlag == "Y":
3
output = wikipedia.summary(query, sentences = 5)
4
else:
5
output = wikipedia.summary(query, sentences = 2)
Copied!

Cleaning the Wikipedia Output

Once Aryan gets output from Wikipedia, Next step is to Clean the Wiki Result, Regular Expressions are used to parse the wiki data and clean it to make it usable for speaking and readability by Aryan. cleanWikiResult flow takes care of it
Below is an example of usage of the Regular Expressions to clean the output response of "Can you tell me about Queen Victoria?"
Wiki Raw Output:
Victoria (Alexandrina Victoria; 24 May 1819 – 22 January 1901) was Queen of the United Kingdom of Great Britain and Ireland from 20 June 1837 until her death in 1901. Known as the Victorian era, her reign of 63 years and seven months was longer than any previous British monarch.
Wiki Cleaned Output:
Victoria was Queen of the United Kingdom of Great Britain and Ireland from 20 June 1837 until her death in 1901. Known as the Victorian era, her reign of 63 years and seven months was longer than any previous British monarch.
1
updated_output = re.sub("[\(\[].*?[\)\]]", "", output)
Copied!

Do you want to Know more?

By default, Aryan fetches 2 sentences from Wikipedia for the given query, Thus in this flow Aryan asks Human if they want to know more? wikiMoreDetailsCheck flow is executed to fulfill this task,
1
wikiMoreDetailsCheck(aifriend,query,updatedoutput)
Copied!
It starts by asking the Human "Do you want to know more?",
  • In case Human says "yes", getMoreDetailsWiki flow is executed to fetch more information, i.e. 3 more sentences.
  • If Human says "no", Aryan triggers the NextStep flow to proceed with further execution.
  • In case of no response the Aryan worker thread exits and Aryan goes back to sleeping mode.
1
PrintText(aifriend,"Do you want to know more?")
2
SpeakText(aifriend,"Do you want to know more?")
3
4
DetailsCheck = TextTransformation(RawSpeechRecognization(aifriend)).lower().lstrip().rstrip()
5
6
if subStrCheck(DetailsCheck,"yes"):
7
8
getMoreDetailsWiki(aifriend,query,updatedoutput)
9
NextStep(aifriend)
10
11
elif subStrCheck(DetailsCheck,"no") or subStrCheck(DetailsCheck, "thankyou") or subStrCheck(DetailsCheck, "thanks") or subStrCheck(DetailsCheck, "thank"):
12
13
PrintText(aifriend, "Okay")
14
SpeakText(aifriend,"Okay")
15
NextStep(aifriend)
16
17
else:
18
PrintText(aifriend, "Didn't got a response")
19
SpeakText(aifriend,"Didn't got a response")
20
exit()
Copied!

Get More Information from Wikipedia

In case the user wants to know more about a particular query then getMoreDetailsWiki flow is executed to get additional 3 sentences it is achieved by getting total 5 sentences and thereafter removing the sentences which is already communicated to our Human friend
1
output = wikipedia.summary(query, sentences = 5)
2
updatedoutputlong = cleanWikiResult(output)
3
moredetails = updatedoutputlong.replace(updatedoutputshort,"")
Copied!

Wikipedia Disambiguation Error

In case for a user query, Aryan get's more then one topics from Wikipedia then disambiguation Error occurs, mentioning user to be more specific in their search as several options are present and flow fall backs to starting point of worker thread.
1
except wikipedia.exceptions.DisambiguationError as e:
2
PrintText(aifriend,"Please be more specific, I have several options with same meaning")
3
SpeakText(aifriend,"Please be more specific, I have several options with same meaning")
4
''' Revert the flow back to starting point'''
5
TriggerAryanOnce(aifriend)
Copied!

Wikipedia Exception Handling

Besides this, if for any topic Aryan is unable to get the response from Wiki, then Human is politely mentioned "Sorry could not get it" and Flow fall backs to starting point of worker thread.
1
except Exception as e:
2
PrintText(aifriend, " Sorry could not get it")
3
SpeakText(aifriend,"Sorry could not get it")
4
PrintText("Exception",str(e))
5
''' Revert the flow back to starting point'''
6
TriggerAryanOnce(aifriend)
Copied!