Newer
Older
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
package smash.appointment.parse;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
public class CellParser {
Logger logger = Logger.getLogger(CellParser.class);
private SubjectDao subjectDao;
Pattern timePattern = Pattern.compile("^[0-9][0-9]\\:[0-9][0-9]");
public String extractTime(String content) {
String result = null;
Matcher matcher = timePattern.matcher(content);
if (matcher.find()) {
result = matcher.group();
}
return result;
}
public String removeTime(String content) {
Matcher matcher = timePattern.matcher(content);
if (matcher.find()) {
content = matcher.replaceFirst("").trim();
}
return content;
}
public AppointmentEntry parseAppointment(String query, String defaultTime) {
AppointmentEntry result = new AppointmentEntry();
String time = extractTime(query);
if (time != null) {
query = removeTime(query);
} else {
time = defaultTime;
}
result.setTime(time);
Subject subject = extractSubject(query);
result.setSubject(subject);
if (subject!=null) {
result.setLocation(subject.getToBeSeenAt());
} else {
result.setLocation("PRC");
}
AppointmentTypeCollection type = extractType(query);
result.addType(AppointmentType.OTHER);
} else {
result.addTypes(type.getTypes());
}
result.setSource(query);
return result;
}
public AppointmentTypeCollection extractType(String query) {
String simplifiedQuery = Utils.simplifyString(query);
AppointmentTypeCollection result = null;
String usedString = null;
for (AppointmentTypeCollection type : AppointmentTypeCollection.values()) {
boolean matchFound = false;
for (String string : type.getQueryStrings()) {
if (!matchFound) {
String simplifiedString = Utils.simplifyString(string);
if (simplifiedQuery.contains(simplifiedString)) {
matchFound = true;
if (result == null) {
result = type;
usedString = string;
} else {
if (string.contains(usedString)) {
result = type;
usedString = string;
} else if (usedString.contains(string)) {
// new one is a substring of old
} else { // if there is no substring then we might have a problem
AppointmentTypeCollection newType = result;
if (usedString.length() < string.length()) {
usedString = string;
}
logger.warn("More than one type possible for query: " + query + ". Type 1: " + result + ". Type 2: " + type + ". Choosing: " + newType);
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
139
}
}
}
}
}
}
return result;
}
private Subject extractSubject(String query) {
Subject result = null;
String simplifiedQuery = Utils.simplifyString(query);
SubjectIndexer[] mainIndices = new SubjectIndexer[] { //
new NameSurnameIndexer(), //
new SurnameNameIndexer(), //
new NdNumberIndexer(),//
};
result = getByIndices(query, simplifiedQuery, mainIndices);
if (result == null) {
SubjectIndexer[] secondaryIndices = new SubjectIndexer[] { //
new SurnameIndexer(), //
};
result = getByIndices(query, simplifiedQuery, secondaryIndices);
}
return result;
}
private Subject getByIndices(String query, String simplifiedQuery, SubjectIndexer[] mainIndices) {
Subject result = null;
for (Subject subject : subjectDao.getSubjects()) {
boolean matchFound = false;
for (SubjectIndexer indexer : mainIndices) {
if (!matchFound) {
if (indexer.match(subject, simplifiedQuery)) {
matchFound = true;
if (result == null) {
result = subject;
} else {
Subject newResult = result;
if (indexer.isBetter(subject, result)) {
newResult = subject;
}
logger.warn(
"[" + indexer.getClass().getSimpleName() + "]" + "More than one subject possible for query: " + query + ". Subject 1: " + result
+ ". Subject 2: " + subject + ". Choosing: " + newResult);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
result = newResult;
}
}
}
}
}
return result;
}
/**
* @return the subjectDao
* @see #subjectDao
*/
public SubjectDao getSubjectDao() {
return subjectDao;
}
/**
* @param subjectDao
* the subjectDao to set
* @see #subjectDao
*/
public void setSubjectDao(SubjectDao subjectDao) {
this.subjectDao = subjectDao;
}
}