CYK algorithm implementation java -
i'm trying implement cyk algorithm based on wikipedia pseudocode. when test string "a b" grammar input:
s->a b
a->a
b->b
gives me false, , think should true. have arraylist called allgrammar contains rules. example above contain:
[0]: s->a b
[1]: a->a
[2]: b->b
for example s->hello , input string hello gives me true should. more complex tests (more productions) gives me false :s
public static boolean cyk(string entrada) { int n = entrada.length(); int r = allgrammar.size(); //vector<string> startingsymbols = getsymbols(allgrammar); string[] ent = entrada.split("\\s"); n = ent.length; system.out.println("length of entry" + n); //let p[n,n,r] array of booleans. initialize elements of p false. boolean p[][][] = initialize3dvector(n, r); //n-> number of words of string entrada, //r-> number of nonterminal symbols //this grammar contains subset rs set of start symbols (int = 1; < n; i++) { for(int j = 0; j < r; j++) { string[] rule = (string[]) allgrammar.get(j); if (rule.length == 2) { if (rule[1].equals(ent[i])) { system.out.println("entrou"); system.out.println(rule[1]); p[i][1][j + 1] = true; } } } } for(int = 2; < n; i++) { system.out.println("first:" + i); for(int j = 1; j < n - + 1; j++) { system.out.println("second:" + j); for(int k = 1; k < - 1; k++) { system.out.println("third:" + k); for(int g = 0; g < r; g++) { string[] rule = (string[]) allgrammar.get(g); if (rule.length > 2) { int = returnpos(rule[0]); int b = returnpos(rule[1]); int c = returnpos(rule[2]); system.out.println("a" + a); system.out.println("b" + b); system.out.println("c" + c); if (a!=-1 && b!=-1 && c!=-1) { if (p[j][k][b] && p[j + k][i - k][c]) { system.out.println("entrou2"); p[j][i][a] = true; } } } } } } } for(int x = 0; x < r; x++) { if(p[1][n][x]) return true; } return false; }
as compared cyk algorithm:
- you have indexing starting @ 1, arrays appear start @ 0
- the function returnpos() not defined, , it's not obvious does.
it seem problems basic in the use of indexes. if new language, might want refresher.
Comments
Post a Comment