-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherIntentClassifier.java
More file actions
110 lines (98 loc) · 3.25 KB
/
WeatherIntentClassifier.java
File metadata and controls
110 lines (98 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* chatbot.component is added for Assignment 3 (Language Understanding)
*
* WeatherIntentClassifier.java is added for Assignment 3 (Language
* Understanding)
*/
package chatbot.component;
public class WeatherIntentClassifier {
private static String[] intentDictionary;
public WeatherIntentClassifier() {
initializeIntentDictionary();
}
/**
* Create a dictionary of intents (under Weather domain)
*/
private void initializeIntentDictionary() {
intentDictionary = new String[]{"WeatherReport", "Snow", "Rain"};
System.out.print("Weather Intents: (");
for(int i=0;i<intentDictionary.length;i++) {
System.out.print(intentDictionary[i]);
if(i!=intentDictionary.length-1) {
System.out.print(", ");
}
}
System.out.println(")");
}
/**
* Calculate the given meesage's score for each intent. The chatbot will
* select the intent with the *highest* score.
*
* The initial score of each domain is 0.0.
*
* @param nowInputText An English message sent from the user.
* @return An Double array that contains the score of each
* intent.
*/
private Double[] calculateIntentScores(String nowInputText) {
//DO NOT change the following 4 lines
//initiate all the scores to 0.0
Double[] scoreArray = new Double[intentDictionary.length];
for(int i=0;i<scoreArray.length;i++) {
scoreArray[i] = Double.valueOf(0.0);
}
//============= Please Modify Here (begins) ===============
//The following is the part you need to modify.
//This current version just assign random values to each intent.
// Loop through each intent's keywords and update the score if found in input
String message = nowInputText.toLowerCase();
for (int i = 0; i < intentDictionary.length; i++) {
String intent = intentDictionary[i].toLowerCase();
if (message.contains(intent)) {
scoreArray[i] = 1.00;
}
}
//============= Please Modify Here (ends) ===============
//do not change the following lines
if(scoreArray.length!=intentDictionary.length) {
System.err.println("The score array size does not equal to the intent array size.");
System.exit(1);
}
for(Double nowValue: scoreArray) {
if(nowValue==null) {
System.err.println("The score array contains null values.");
System.exit(1);
}
}
return scoreArray;
}
/**
* Input:
* nowInputText: the message that the user sent to your chatbot
*
* Output:
* the label (intent) name string
*
* @param nowInputText An English message sent from the user.
* @return The name of the intent.
*
*/
public String getLabel(String nowInputText) {
Double[] intentScores = calculateIntentScores(nowInputText);
Double nowMaxScore = null;
int nowMaxIndex = -1;
System.out.print("Intent Scores: (");
for(int i=0;i<intentScores.length;i++){
System.out.print(intentScores[i].doubleValue());
if(i!=intentScores.length-1) {
System.out.print(", ");
}
if(nowMaxScore==null||nowMaxIndex==-1||intentScores[i].doubleValue()>nowMaxScore.doubleValue()) {
nowMaxIndex = i;
nowMaxScore = intentScores[i].doubleValue();
}
}
System.out.println(")");
return intentDictionary[nowMaxIndex];
}
}