Intent Disambigution Helper

The code below creates a class called DisambiguationHelper, which helps you handle the situation that multiple classes have been annotated with very closed confidence score. You can copy the code and paste it in Globals > Scripts > Solution Loaded, or create a groovy file (for example named DisambiguationHelper.groovy) with this code and upload into your solution under Resource. For more details about how to use this class to do intent disambiguation, please read this article: Intent Disambiguation Setup.

public class DisambiguationHelper{
	
	public ArrayList<Object> intents
	public Double confidenceLowerBound
	public String prevFlowName

	public DisambiguationHelper(binding){
		intents = new ArrayList<Object>()
		this.setIntents(binding)
		this.setConfidenceLowerBound(binding)
	}

	public Boolean hasAlternativeIntents(){		
		if(prevFlowName.replace(" ","").equalsIgnoreCase("safetynet")&& intents.size()>0)
			return true
		else
			return false
	}

	private Double getConfidence(Object intent){
		return intent.getVariables().confidence;		
	}

	public Boolean hasSmallDifference(){
		if(confidenceLowerBound){
			Iterator it = intents.iterator()
			while (it.hasNext()){
				if (getConfidence(it.next())<confidenceLowerBound)
					it.remove()
			}
			if(intents.size()>1)
				return true
			else
				return false
		}
		else
			return false
	}

	private void setIntents(binding){
		Set annotations = binding._.getInputAnnotations().getAll();
		for(Object annotation : annotations){
			if(annotation.getName().contains("INTENT")){
				if(getConfidence(annotation)>=binding.minThreshold&&binding.intentsForDisambiguation.contains(annotation.getName().replace(".INTENT","")))
					intents.add(annotation);
			}
		}
	}

	public void setConfidenceLowerBound(binding){	
		if(intents.size()>1)
			confidenceLowerBound = getConfidence(intents.get(0)) - binding.diffThreshold
	}
	
}
1 Like