Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Exact token pattern matching
public class ExactMatchPOC {
private static final String TOKEN_DELIMITER = " ";
private static final String PHRASE_DELIMITER = ",";
public static void main(String[] args) {
String[] phraseTokens = buildTokenHashArray(args[0]);
String[][] labelTokens = buildMultiTokenHashArray(args[1]);
int i;
int[] t = new int[labelTokens.length];
Integer[] m = new Integer[phraseTokens.length];
fill(m, -1);
for (i = 0; i < phraseTokens.length; i++) {
for (int x = 0; x < labelTokens.length; x++) {
if (t[x] != labelTokens[x].length) {
if (phraseTokens[i].equals(labelTokens[x][t[x]])) {
t[x]++;
if (t[x] == labelTokens[x].length) {
for (int a = i; a > i - t[x]; a--) {
if (m[a] < 0) {
m[a] = x;
}
}
}
}
else {
t[x] = 0;
}
}
}
}
for (int a = 0; a < m.length; a++) {
if (m[a] > -1)
System.out.print(labelTokens[m[a]][a % labelTokens[m[a]].length] + "(" + m[a] + ") ");
else
System.out.print("- ");
}
System.out.println("\nFound token(s) in " + i + " iteration(s)");
}
private static <T> void fill(T[] arr, T v) {
for (int i = 0; i < arr.length; i++)
arr[i] = v;
}
private static String[] buildTokenHashArray(String phrase) {
return phrase.split(TOKEN_DELIMITER);
}
private static String[][] buildMultiTokenHashArray(String multiPhrases) {
String[] phrases = multiPhrases.split(PHRASE_DELIMITER);
String[][] multiTokenHashes = new String[phrases.length][];
for (int i = 0; i < phrases.length; i++)
multiTokenHashes[i] = buildTokenHashArray(phrases[i]);
return multiTokenHashes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment