Advertisement
sodohertyie

Intent Confusion detection example

Sep 30th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. # Note: The code below is a sample provided to illustrate one way
  2. # to approach this issue and is used as is and at your own risk. In order
  3. # for this example to perform as intended, the script must be laid out exactly
  4. # as indicated below. This script will not be customized for specific
  5. # environments or applications.
  6. #
  7. # @author: Simon O'Doherty
  8.  
  9.  
  10. from watson_developer_cloud import ConversationV1
  11. import pandas as pd
  12. from scipy.cluster.vq import kmeans, vq
  13. import numpy as np
  14.  
  15. # Get these details from your service credentials.
  16. ctx = {
  17.   "url": "https://gateway.watsonplatform.net/assistant/api",
  18.   "password": "...",
  19.   "username": "..."
  20. }
  21. workspace_id = '...';
  22.  
  23. # This is used to append to a question, to prevent 100% match.
  24. safeword = 'SIO'
  25.  
  26. wa = ConversationV1(
  27.     username=ctx.get('username'),
  28.     password=ctx.get('password'),
  29.     version='2018-09-20',
  30.     url=ctx.get('url')
  31. )
  32.  
  33. # Send message to WA, and return output, intents and context.
  34. def message(text='',context=None):
  35.     msg = {'text': text}
  36.     response = wa.message(workspace_id=workspace_id, input=msg, context=context, alternate_intents=True)
  37.    
  38.     result = response.result
  39.     output = result['output']['text']
  40.     context = result['context']
  41.     intents = result['intents']
  42.    
  43.     return (output, intents, context)
  44.  
  45.  
  46. # Get the list of intents and their example questions.
  47. def getIntents():
  48.     response = wa.get_workspace(workspace_id=workspace_id, export=True)
  49.     return response.result['intents']
  50.  
  51.  
  52. # Uses K-Means to deteremine if the question was confused with another intent.
  53. def confusedQuestion(intents, question):
  54.    
  55.     ic = []
  56.     for i in intents:
  57.         ic.append(i['confidence'])
  58.    
  59.     v = np.array(ic)
  60.     codebook, _ = kmeans(v,2)
  61.     ci, _ = vq(v,codebook)
  62.    
  63.     # We want to make everything in the top bucket to have a value of 1.
  64.     if ci[0] == 0: ci = 1-ci
  65.    
  66.    
  67.     if sum(ci) > 1:
  68.         r = {
  69.             'question': question,
  70.             'intent': intents[0]['intent'],
  71.             'confused_with': intents[1]['intent']
  72.             }
  73.          
  74.         return True, r
  75.    
  76.    
  77.     return False, ''
  78.  
  79. ## ---
  80.  
  81. intents = getIntents()
  82.  
  83. print('Intent total: {}'.format(len(intents)))
  84.  
  85. recs = []
  86. for intent in intents:
  87.     print('{}'.format(intent['intent']))
  88.     for example in intent['examples']:
  89.         o,i,c = message(text='{} {}'.format(safeword,example['text']),context={})
  90.        
  91.         confused, response = confusedQuestion(i,example['text'])
  92.        
  93.         if confused:
  94.             recs.append(response)
  95.             print('    {} = {} . Confused with: {}'.format(
  96.                 response['question'], response['intent'], response['confused_with'])
  97.             )
  98.  
  99. df = pd.DataFrame(recs, columns=['question','intent','confused_with'])
  100. df.to_csv('confusion_report.csv')
  101.  
  102. print('\nDone.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement