|
1 | 1 | package com.xatkit.core.recognition; |
2 | 2 |
|
| 3 | +import com.xatkit.core.EventDefinitionRegistry; |
3 | 4 | import com.xatkit.core.XatkitBot; |
4 | 5 | import com.xatkit.core.XatkitException; |
5 | | -import com.xatkit.core.recognition.dialogflow.DialogFlowConfiguration; |
6 | | -import com.xatkit.core.recognition.dialogflow.DialogFlowIntentRecognitionProvider; |
7 | | -import com.xatkit.core.recognition.nlpjs.NlpjsConfiguration; |
8 | | -import com.xatkit.core.recognition.nlpjs.NlpjsIntentRecognitionProvider; |
9 | 6 | import com.xatkit.core.recognition.processor.InputPreProcessor; |
10 | 7 | import com.xatkit.core.recognition.processor.IntentPostProcessor; |
11 | 8 | import com.xatkit.core.recognition.regex.RegExIntentRecognitionProvider; |
|
15 | 12 | import org.apache.commons.configuration2.Configuration; |
16 | 13 |
|
17 | 14 | import javax.annotation.Nullable; |
| 15 | +import java.lang.reflect.Constructor; |
18 | 16 | import java.lang.reflect.InvocationTargetException; |
19 | 17 | import java.util.List; |
20 | 18 | import java.util.stream.Collectors; |
@@ -57,6 +55,17 @@ private IntentRecognitionProviderFactory() { |
57 | 55 |
|
58 | 56 | public static final String DEFAULT_DATABASE_MODEL = DATABASE_MODEL_MAPDB; |
59 | 57 |
|
| 58 | + /** |
| 59 | + * The intent provider that will be used for this instance of Xatkit. |
| 60 | + * <p> |
| 61 | + * If this property isn't set the {@link RegExIntentRecognitionProvider} will be used. |
| 62 | + * <p> |
| 63 | + * The value of this property is the fully-qualified name of the {@link IntentRecognitionProvider} subclass to |
| 64 | + * instantiate. Libraries providing such implementations typically provide a constant value for that in their |
| 65 | + * configuration. |
| 66 | + */ |
| 67 | + public static final String INTENT_PROVIDER_KEY = "xatkit.intent.provider"; |
| 68 | + |
60 | 69 | /** |
61 | 70 | * Returns the {@link AbstractIntentRecognitionProvider} matching the provided {@code configuration}. |
62 | 71 | * <p> |
@@ -111,19 +120,19 @@ public static IntentRecognitionProvider getIntentRecognitionProvider(@NonNull Xa |
111 | 120 |
|
112 | 121 | IntentRecognitionProvider provider; |
113 | 122 |
|
114 | | - if (baseConfiguration.containsKey(DialogFlowConfiguration.PROJECT_ID_KEY)) { |
115 | | - /* |
116 | | - * The provided configuration contains DialogFlow-related information. |
117 | | - */ |
118 | | - provider = new DialogFlowIntentRecognitionProvider(xatkitBot.getEventDefinitionRegistry(), |
119 | | - baseConfiguration, |
120 | | - recognitionMonitor); |
121 | | - } else if (baseConfiguration.containsKey(NlpjsConfiguration.AGENT_ID_KEY)) { |
122 | | - /* |
123 | | - * The provided configuration contains NLP.js-related information. |
124 | | - */ |
125 | | - provider = new NlpjsIntentRecognitionProvider(xatkitBot.getEventDefinitionRegistry(), baseConfiguration, |
126 | | - recognitionMonitor); |
| 123 | + if(baseConfiguration.containsKey(INTENT_PROVIDER_KEY)) { |
| 124 | + try { |
| 125 | + String providerClassName = baseConfiguration.getString(INTENT_PROVIDER_KEY); |
| 126 | + Class<? extends IntentRecognitionProvider> providerClass = (Class<? extends IntentRecognitionProvider>) Class.forName(providerClassName); |
| 127 | + Constructor<? extends IntentRecognitionProvider> providerConstructor = |
| 128 | + providerClass.getConstructor(EventDefinitionRegistry.class, Configuration.class, |
| 129 | + RecognitionMonitor.class); |
| 130 | + provider = providerConstructor.newInstance(xatkitBot.getEventDefinitionRegistry(), baseConfiguration, |
| 131 | + recognitionMonitor); |
| 132 | + } catch(ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | |
| 133 | + IllegalAccessException e) { |
| 134 | + throw new RuntimeException(e); |
| 135 | + } |
127 | 136 | } else { |
128 | 137 | /* |
129 | 138 | * The provided configuration does not contain any IntentRecognitionProvider information, returning a |
|
0 commit comments