if (devices == null) {
throw new RuntimeException("Uninitialized device index");
}
Map<String, List<Device>> hits = new HashMap<String, List<Device>>(100);
Device winner = null;
String winnerStr = "";
if (text == null) {
return getUnknownDevice();
}
Util.debugLog("classify: '" + text + "'");
String[] parts = text.split(" |-|_|/|\\\\|\\[|\\]|\\(|\\)|;");
//generate ngrams upto size 4
for (int i = 0; i < parts.length; i++) {
String pattern = "";
for (int j = 0; j < 4 && (j + i) < parts.length; j++) {
if (parts[i + j].isEmpty()) {
continue;
}
pattern += Util.normalize(parts[i + j]);
List<Device> dlist = patterns.get(pattern);
if (dlist != null) {
hits.put(pattern, dlist);
for (Device device : dlist) {
Util.debugLog("Hit found: " + pattern + " => " + device.getId() + " " + device.getPatterns());
}
}
}
}
//look for the strongest hit
for (String hit : hits.keySet()) {
for (Device device : hits.get(hit)) {
if (!device.getPatterns().isValid(hits.keySet())) {
continue;
}
Util.debugLog("Hit candidate: " + hit + " => " + device.getId());
if (winner != null) {
if ("simple".equals(winner.getType()) && !"simple".equals(device.getType())) {
winner = device;
winnerStr = hit;
} else if (hit.length() > winnerStr.length() &&
(!"simple".equals(device.getType()) || device.getType().equals(winner.getType()))) {
winner = device;
winnerStr = hit;
}
} else {
winner = device;
winnerStr = hit;
}
}
}
if (winner != null) {
Util.debugLog("Result: " + winner);
return winner.getAttributes();
} else {
return getUnknownDevice();
}
}