-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGymIntentClassifier.java
More file actions
138 lines (124 loc) · 4.14 KB
/
GymIntentClassifier.java
File metadata and controls
138 lines (124 loc) · 4.14 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* chatbot.component is added for Assignment 3 (Language Understanding)
*
* WeatherIntentClassifier.java is added for Assignment 3 (Language
* Understanding)
*/
package chatbot.component;
public class GymIntentClassifier {
private static String[] intentDictionary;
public GymIntentClassifier() {
initializeIntentDictionary();
}
/**
* Create a dictionary of intents (under Weather domain)
*/
private void initializeIntentDictionary() {
intentDictionary = new String[]{"gym", "muscle", "exercise", "location", "time", "reps", "sets"};
System.out.print("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.
String newMessage = nowInputText.toLowerCase();
for (int i = 0; i < intentDictionary.length; i++) {
String intent = intentDictionary[i].toLowerCase();
if (newMessage.contains(intent)) {
scoreArray[i] = 1.00;
}
}
/*
String[] repsDictionary = new String[] {"reps"};
for(String repsKeyword: repsDictionary) {
if(nowInputText.toLowerCase().indexOf(repsKeyword)>=0) {
scoreArray[1] = scoreArray[1].doubleValue()+1.0;
}
}
String[] setsDictionary = new String[] {"sets"};
for(String setsKeyword: setsDictionary) {
if(nowInputText.toLowerCase().indexOf(setsKeyword)>=0) {
scoreArray[2] = scoreArray[2].doubleValue()+1.0;
}
}
String[] timeDictionary = new String[] {"time"};
for(String timeKeyword: timeDictionary) {
if(nowInputText.toLowerCase().indexOf(timeKeyword)>=0) {
scoreArray[3] = scoreArray[3].doubleValue()+1.0;
}
}*/
/*
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];
}
}