diff --git a/src/mentionmatch/MatrixComparison.java b/src/mentionmatch/MatrixComparison.java index 58f7f6b..a0e21c3 100644 --- a/src/mentionmatch/MatrixComparison.java +++ b/src/mentionmatch/MatrixComparison.java @@ -134,7 +134,6 @@ class MatrixComparison { } dumpMatrix("tried to match"); -/* Forget about this now; this doesn't work. // There may still be an occasional swap, like with 0.1/0.0/0.35/0.25 above. // On the measurements, this looks like this: @@ -143,56 +142,78 @@ class MatrixComparison { // This may either be due to inconsistent stretching (in which case UX/VW // should be turned into UW/VX), or one new wire break (U) and an old one // that wasn't recognized (X). We assume it's inconsistent stretching and - // swap the matches if the rations AU/AW, UV/WX and VB/XB are close to 1.0, + // swap the matches if the rations MU/MW, UV/WX and VN/XN are close to 1.0, // and assume it's new/old breaks if they aren't. // Note we can also have something like // M---------------A-B-C-D----------------N // M-----------P-Q-R-S--------------------N // where we get AR/BS/CQ/DP but want AP/BQ/CR/DS. + // Algorithm: find a swapped pair of x/y values. Expand left and right + // to the surrounding rectangle as long as distances are small. + // Sort the y values within. Arrays.sort(matches); - float maxAllowedStretch = ratio(matches[matches.length-1].x, matches[matches.length-1].y)*1.1f; // allow 10% stretch - for (int i=0; i matches[i+1].y) { // i=B, i+1=C in above case - // find the last pair that has decrementing Y (DP) - int decrementingLength=1; - int minInvolvedY=matches[i+1].y; - while (i+decrementingLength < matches.length-1 - && matches[i].y > matches[i+decrementingLength+1].y) { - if (minInvolvedY > matches[i+decrementingLength+1].y) { - minInvolvedY = matches[i+decrementingLength+1].y; + + boolean foundShiftedStreaks; + do { + foundShiftedStreaks = false; + for (int i=0; i matches[i+1].y && xAxisVal[matches[i].x] > xAxisVal[matches[i+1].x] - 0.03) { + foundShiftedStreaks = true; + int minx = i; + int maxx = i+1; + int miny = matches[maxx].y; + int maxy = matches[minx].y; + boolean expanded; + do { + // find largest y difference between start/end spots + float deltax = 0; + for (int j=minx; j deltax) { + deltax = thisdelta; + } + } + // and allow 1.5 times this for the rest + deltax = deltax * 3 / 2; + // but only 3% of the whole length + if (deltax > 0.03f) { + deltax = 0.03f; + } + System.out.println("shifted streak found from "+minx+" to "+maxx+", miny="+miny+", maxy="+maxy+", maximum x delta is "+deltax); + expanded = false; + // expand right + while (maxx < matches.length-1 && matches[maxx+1].y < maxy && xAxisVal[maxx+1] < xAxisVal[maxx]+deltax) { + maxx++; + expanded = true; + if (miny > matches[maxx].y) { + miny = matches[maxx].y; + } + } + // expand left + while (minx > 0 && matches[minx-1].y > miny && xAxisVal[minx-1] > xAxisVal[minx]-deltax) { + minx--; + expanded = true; + if (maxy < matches[minx].y) { + maxy = matches[minx].y; + } + } + } while (expanded); + System.out.println("expansions ended"); + // As the X values are sorted anyway, sort the Y values as well, and assign them to the X values in order + int[] sortedY = new int[maxx-minx+1]; + for (int j=0; j= minInvolvedY - && matches[j].y <= matches[i].y) { - matchingYs++; + Arrays.sort(sortedY); + for (int j=0; j