<commit_before>package routing;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.inject.Injector;
import org.reflections.Reflections;

import com.amazon.speech.slu.Intent;
import com.amazon.speech.speechlet.SpeechletException;
import com.amazon.speech.speechlet.SpeechletResponse;
import com.amazon.speech.ui.PlainTextOutputSpeech;
import com.amazon.speech.ui.SimpleCard;
import com.google.inject.Singleton;
import routing.attributes.FilterFor;
import routing.attributes.Slot;
import routing.attributes.Utterances;
import routing.providers.RequestContextProvider;

@Singleton
public class SpeechRouter {
	private List<String> sampleUtterances;
	private IntentSchema intentSchema;
	private Map<String, IntentActivationContext> intents;
    private Injector injector;
	
	public List<String> getSampleUtterances() { return sampleUtterances; }
	public IntentSchema getIntentSchema() { return intentSchema; }

	private SpeechRouter(List<String> sampleUtterances, IntentSchema intentSchema, Map<String, IntentActivationContext> intents, Injector injector) {
		this.sampleUtterances = sampleUtterances;
		this.intentSchema = intentSchema;
		this.intents = intents;
        this.injector = injector;
	}
	
	public SpeechletResponse processIntent(String intentName, Intent intent) throws SpeechletException {
		if (intents.containsKey(intentName) == false) {
            throw new SpeechletException("Invalid Intent " + intentName);
        }
		
		IntentActivationContext activation = intents.get(intentName);
		ActionInvocationContext action = activation.getActionInvocationContext();
		
		// Create controller
		AlexaController controller = null;
		Constructor<?> ctor;
		try {
			Class controllerClass = action.getActionMethod().getDeclaringClass();
            controller = (AlexaController)injector.getInstance(controllerClass);
		} catch (Exception e) {
			throw new SpeechletException("Could not create controller instance", e);
		}
		
		// Parse slot values
		Map<String, Object> slotValues = new HashMap<String, Object>();
		for (Entry<String, Class<?>> slot : action.getSlots().entrySet()) {
			if (intent.getSlot(slot.getKey()) == null || intent.getSlot(slot.getKey()).getValue() == null) {
				// Supply default values for Integer parameters
				if (slot.getValue() == int.class)<commit_msg>Handle weird new Amazon requests with empty slot values<commit_after>package routing;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.inject.Injector;
import org.reflections.Reflections;

import com.amazon.speech.slu.Intent;
import com.amazon.speech.speechlet.SpeechletException;
import com.amazon.speech.speechlet.SpeechletResponse;
import com.amazon.speech.ui.PlainTextOutputSpeech;
import com.amazon.speech.ui.SimpleCard;
import com.google.inject.Singleton;
import routing.attributes.FilterFor;
import routing.attributes.Slot;
import routing.attributes.Utterances;
import routing.providers.RequestContextProvider;

@Singleton
public class SpeechRouter {
	private List<String> sampleUtterances;
	private IntentSchema intentSchema;
	private Map<String, IntentActivationContext> intents;
    private Injector injector;
	
	public List<String> getSampleUtterances() { return sampleUtterances; }
	public IntentSchema getIntentSchema() { return intentSchema; }

	private SpeechRouter(List<String> sampleUtterances, IntentSchema intentSchema, Map<String, IntentActivationContext> intents, Injector injector) {
		this.sampleUtterances = sampleUtterances;
		this.intentSchema = intentSchema;
		this.intents = intents;
        this.injector = injector;
	}
	
	public SpeechletResponse processIntent(String intentName, Intent intent) throws SpeechletException {
		if (intents.containsKey(intentName) == false) {
            throw new SpeechletException("Invalid Intent " + intentName);
        }
		
		IntentActivationContext activation = intents.get(intentName);
		ActionInvocationContext action = activation.getActionInvocationContext();
		
		// Create controller
		AlexaController controller = null;
		Constructor<?> ctor;
		try {
			Class controllerClass = action.getActionMethod().getDeclaringClass();
            controller = (AlexaController)injector.getInstance(controllerClass);
		} catch (Exception e) {
			throw new SpeechletException("Could not create controller instance", e);
		}
		
		// Parse slot values
		Map<String, Object> slotValues = new HashMap<String, Object>();
		for (Entry<String, Class<?>> slot : action.getSlots().entrySet()) {
			if (intent.getSlot(slot.getKey()) == null ||
					intent.getSlot(slot.getKey()).getValue() == null ||
					intent.getSlot(slot.getKey()).getValue().equals("?")) {
				
				// Supply default values for Integer parameters
				if (slot.getValue() == int.class)
{'fraction_non_alphanumeric': 0.10335964751239214, 'fraction_numerical': 0.0, 'mean_word_length': 5.247706422018348, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6257432', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1514, 'n_words': 354}