我写了一些实现CYK Algorithm的代码。我的代码有些问题,但我不知道哪个错了。我也是asked about the pseudo-code of CYK Algorithm。
你能帮我解释一下这段代码:
import java.util.ArrayList;
public class cyk {
public static int findIndex(ArrayList<String[]> Grammar, String symbol) {
for (int i = 0; i < Grammar.size(); i++) {
String[] rule = Grammar.get(i);
if (rule.length == 2) {
if (symbol.equals(Grammar.get(i)[0]))
return i;
}
}
for (int i = 0; i < Grammar.size(); i++) {
if (symbol.equals(Grammar.get(i)[0]))
return i;
}
return -1;
}
public static boolean cyk(String S, ArrayList<String[]> Grammar) {
int n = S.length();
int r = Grammar.size();
boolean P[][][] = new boolean[n][n][r];
initializeP(P, n, r);
for (int i = 0; i < n; i++) {
for (int j = 0; j < r; j++) {
String[] rule = (String[]) Grammar.get(j);
if (rule.length == 2) {
if (rule[1].equals(String.valueOf(S.charAt(i)))) {
P[i][0][j] = true;
//System.out.println(S.charAt(i));
}
}
}
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i + 1; j++) {
for (int k = 0; k < i - 1; k++) {
// for each production RA -> RB RC
for (int m = 0; m < r; m++) {
String[] rule = Grammar.get(m);
if (rule.length > 2) {
int A = findIndex(Grammar, rule[0]);
int B = findIndex(Grammar, rule[1]);
int C = findIndex(Grammar, rule[2]);
if (P[i][k][B] && P[j + k][i - k][C])
P[j][i][A] = true;
}
}
}
}
}
// for (int i = 0; i < n; i++) {
// for (int k = 0; k < r; k++) {
// System.out.print(P[1][i][k] + " ");
// }
// System.out.println();
// }
for (int i = 0; i < r; i++) {
if (P[0][n-1][i])
return true;
}
return false;
}
public static void initializeP(boolean P[][][], int n, int r) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < r; k++) {
P[i][j][k] = false;
}
}
}
}
public static void main(String args[]) {
/*
* If size of production is >2 its not unit production
*/
String input = "aabb";
ArrayList<String[]> Grammar = new ArrayList<String[]>();
String[] rule = { "A", "a" };
Grammar.add(rule);
String[] rule1 = { "A", "A", "A" };
Grammar.add(rule1);
String[] rule2 = { "B", "B", "B" };
Grammar.add(rule2);
String[] rule5 = { "B", "b" };
Grammar.add(rule5);
String[] rule3 = { "S", "A", "B" };
Grammar.add(rule3);
System.out.println(cyk(input, Grammar));
}
}
谢谢你们!
编辑:
调试代码一段时间后,我发现代码的一部分无效。
是:
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i + 1; j++) {
for (int k = 0; k < i - 1; k++) {
// for each production RA -> RB RC
for (int m = 0; m < r; m++) {
String[] rule = Grammar.get(m);
if (rule.length > 2) {
int A = findIndex(Grammar, rule[0]);
int B = findIndex(Grammar, rule[1]);
int C = findIndex(Grammar, rule[2]);
if (P[i][k][B] && P[j + k][i - k][C])
P[j][i][A] = true;
}
}
}
}
}
我怀疑这个错误与我的第一个问题有关。这很可能与我解释A B和C的索引的方式有关。有人可以看看findIndex是否是找到索引的正确方法?