📄 akerbladkernel.java
字号:
package net.sourceforge.akerblad;import info.jonclark.log.LogUtils;import info.jonclark.util.ArrayUtils;import info.jonclark.util.StringUtils;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.logging.Logger;public class AkerbladKernel { private final Akerblad.Config config; private final Akerblad.DocPair dp; private static final double MINS = -10; private static final double penalty01 = 0.8; private static final double penalty21 = 0.95; private static final double penalty12 = 0.95; private static final double penalty22 = 0.85; private static final double penalty31 = 0.94; private static final double penalty13 = 0.94; private static final double penalty41 = 0.92; // 0.85 private static final double penalty14 = 0.92; // 0.85 // assumed contants -- not defined in perl version private static final double penalty23 = 0.0; private static final double penalty32 = 0.0; private static final double penalty33 = 0.0; private final double[][] score; private final double[][] stScores; private final int[][] pathX; private final int[][] pathY; private static final Logger log = LogUtils.getLogger(); public AkerbladKernel(Akerblad.Config config, Akerblad.DocPair dp) { this.config = config; this.dp = dp; this.pathX = new int[dp.nx + 1][dp.ny + 1]; this.pathY = new int[dp.nx + 1][dp.ny + 1]; this.score = new double[dp.nx + 1][dp.ny + 1]; // this.stScores = new double[dp.nx][dp.ny]; this.stScores = null; for (int i = 0; i < score.length; i++) { for (int j = 0; j < score[i].length; j++) { score[i][j] = Double.NEGATIVE_INFINITY; } } } private void setScore(int x, int y, double d) { // System.err.println(x + " " + y + " " + d); score[x][y] = d; } private double getScore(int x, int y) { if (score[x][y] != Double.NEGATIVE_INFINITY) { return score[x][y]; } else { return 0; } } public int align(ArrayList<Integer> x, ArrayList<Integer> y, int nx, int ny) { // System.err.println("ALIGN: " + nx + " " + ny); // s13 not declared in perl version double s1 = 0.0, s2 = 0.0, s3 = 0.0, s4 = 0.0, s5 = 0.0, s6 = 0.0, s7 = 0.0, s8 = 0.0, s9 = 0.0, s10 = 0.0, s11 = 0.0, s12 = 0.0, s13 = 0.0; int i, j, oi, oj; double si, sj, smax; int im1, im2, im3, im4, jm1, jm2, jm3, jm4; double xyratio = (double) nx / (double) ny; for (j = 0; j <= ny; j++) { int center = (int) (j * xyratio); int windowStart = center - dp.windowSize > 0 ? center - dp.windowSize : 0; int windowEnd = center + dp.windowSize < nx ? center + dp.windowSize : nx; // System.err.println(windowStart + " " + windowEnd); for (i = windowStart; i <= windowEnd; i++) { // s1=s2=s3=s4=s5=s6=s7=s8=s9=s10=s11=s12=-100; im1 = i - 1; im2 = i - 2; im3 = i - 3; im4 = i - 4; jm1 = j - 1; jm2 = j - 2; jm3 = j - 3; jm4 = j - 4; s1 = i > 0 && j > 0 ? // 1-1 getScore(i - 1, j - 1) + matchSentences(new int[] { im1 }, new int[] { jm1 }) : MINS; s2 = i > 0 ? // 1-0 getScore(i - 1, j) + matchSentences(new int[] { im1 }, new int[0]) : MINS; s3 = j > 0 ? // 0-1 getScore(i, j - 1) + matchSentences(new int[0], new int[] { jm1 }) : MINS; s4 = i > 1 && j > 0 ? // 2-1 getScore(i - 2, j - 1) + matchSentences(new int[] { im2, im1 }, new int[] { jm1 }) : MINS; s5 = i > 0 && j > 1 ? // 1-2 getScore(i - 1, j - 2) + matchSentences(new int[] { im1 }, new int[] { jm2, jm1 }) : MINS; s6 = i > 1 && j > 1 ? // 2-2 getScore(i - 2, j - 2) + matchSentences(new int[] { im2, im1 }, new int[] { jm2, jm1 }) : MINS; if (!config.disallow3) { s7 = i > 0 && j > 2 ? // 1-3 getScore(i - 1, j - 3) + matchSentences(new int[] { im1 }, new int[] { jm3, jm2, jm1 }) : MINS; s8 = i > 2 && j > 0 ? // 3-1 getScore(i - 3, j - 1) + matchSentences(new int[] { im3, im2, im1 }, new int[] { jm1 }) : MINS; s9 = i > 1 && j > 2 ? // 2-3 getScore(i - 2, j - 3) + matchSentences(new int[] { im2, im1 }, new int[] { jm3, jm2, jm1 }) : MINS; s10 = i > 2 && j > 1 ? // 3-2 getScore(i - 3, j - 2) + matchSentences(new int[] { im3, im2, im1 }, new int[] { jm2, jm1 }) : MINS; // s11 = i>2 && j>2 ? //// 3-3 // getScore(i-3, j-3) + matchSentences("im3 im2 im1 - jm3 // jm2 jm1") // : MINS; s12 = i > 0 && j > 3 ? // 1-4 getScore(i - 1, j - 4) + matchSentences(new int[] { im1 }, new int[] { jm4, jm3, jm2, jm1 }) : MINS; s13 = i > 3 && j > 0 ? // 4-1 getScore(i - 4, j - 1) + matchSentences(new int[] { im4, im3, im2, im1 }, new int[] { jm1 }) : MINS; } smax = s1; if (s2 > smax) { smax = s2; } if (s3 > smax) { smax = s3; } if (s4 > smax) { smax = s4; } if (s5 > smax) { smax = s5; } if (s6 > smax) { smax = s6; } if (s7 > smax) { smax = s7; } if (s8 > smax) { smax = s8; } if (s9 > smax) { smax = s9; } if (s10 > smax) { smax = s10; } // if(s11>smax) { smax=s11; } if (s12 > smax) { smax = s12; } if (s13 > smax) { smax = s13; }// System.err.println("s: " + s1 + " " + s2 + " " + s3 + " " + s4 + " " + s5 + " "// + s6 + " " + s7 + " " + s8 + " " + s9 + " " + s10 + " " + s11 + " " + s12// + " " + s13); // System.err.println("MAX: " + smax); if (smax == MINS) { setScore(i, j, 0); } else if (smax == s1) { // 1-1 setScore(i, j, s1); pathX[i][j] = i - 1; pathY[i][j] = j - 1; } else if (smax == s2) { // 1-0 setScore(i, j, s2); pathX[i][j] = i - 1; pathY[i][j] = j; } else if (smax == s3) { // 0-1 setScore(i, j, s3); pathX[i][j] = i; pathY[i][j] = j - 1; } else if (smax == s4) { // 2-1 setScore(i, j, s4); pathX[i][j] = i - 2; pathY[i][j] = j - 1; } else if (smax == s5) { // 1-2 setScore(i, j, s5); pathX[i][j] = i - 1; pathY[i][j] = j - 2; } else if (smax == s6) { // 2-2 setScore(i, j, s6); pathX[i][j] = i - 2; pathY[i][j] = j - 2; } else if (smax == s7) { // 1-3 setScore(i, j, s7); pathX[i][j] = i - 1; pathY[i][j] = j - 3; } else if (smax == s8) { // 3-1 setScore(i, j, s8); pathX[i][j] = i - 3; pathY[i][j] = j - 1; } else if (smax == s9) { // 2-3 setScore(i, j, s9); pathX[i][j] = i - 2; pathY[i][j] = j - 3; } else if (smax == s10) { // 3-2 setScore(i, j, s10); pathX[i][j] = i - 3; pathY[i][j] = j - 2; // } else if (smax == s11) { // 3-3 // setScore(i,j,s11); // pathX[i][j] = i-3; // pathY[i][j] = j-3; } else if (smax == s12) { // 1-4 setScore(i, j, s12); pathX[i][j] = i - 1; pathY[i][j] = j - 4; } else if (smax == s13) { // 4-1 setScore(i, j, s13); pathX[i][j] = i - 4; pathY[i][j] = j - 1; } // System.err.println("smax: " + smax);// System.err.println("i,j: " + i + "," + j);// System.err.println("BLEH X: " + pathX[i][j]);// System.err.println("BLEH Y: " + pathY[i][j]); }// System.err.println("Finished iteration over: " + i); } // System.err.println("nx=" + nx + " ny=" + ny); int n = 0; for (i = nx, j = ny; i > 0 || j > 0; i = oi, j = oj, n++) { oi = pathX[i][j]; oj = pathY[i][j]; si = i - oi; sj = j - oj;// System.err.println("oi=" + oi + " oj=" + oj); im1 = i - 1; im2 = i - 2; im3 = i - 3; jm1 = j - 1; jm2 = j - 2; jm3 = j - 3; if (si == 1 && sj == 1) { // 1-1 dp.rAlign.add(i + " <=> " + j); } else if (si == 1 && sj == 0) { // 1-0 dp.rAlign.add(i + " <=> omitted");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -