@@ -6,20 +6,26 @@ import org.ivdnt.galahad.annotations.Term
66import org.ivdnt.galahad.export.DocumentExport
77
88/* * An iterator that saves the current item. */
9- class SmartIterator <T > : Iterator <T ?> {
10- var current: T ? = null
9+ class TermIterator : Iterator <Term ?> {
10+ var current: Term ? = null
11+ private set
12+ var chars: Int = 0
1113 private set
1214
13- private val iter: Iterator <T >
15+ private val iter: Iterator <Term ? >
1416
15- constructor (iter: Iterator <T >) {
17+ constructor (iter: Iterator <Term ? >) {
1618 this .iter = iter
1719 next()
1820 }
1921
2022 override fun hasNext (): Boolean = iter.hasNext()
2123
22- override fun next (): T ? {
24+ override fun next (): Term ? {
25+ // add length of now previous term
26+ if (current != null ) {
27+ chars + = (current as Term ).token.length
28+ }
2329 current = if (iter.hasNext()) iter.next() else null
2430 return current
2531 }
@@ -39,52 +45,32 @@ open class LayerComparison(
3945 constructor (export: DocumentExport ) : this (export.layer, export.sourceLayer)
4046
4147 val matches: MutableList <TermComparison > = ArrayList ()
42- private val hypoIter: SmartIterator < Term > = SmartIterator (hypothesis.terms.iterator())
43- private val refIter: SmartIterator < Term > = SmartIterator (reference.terms.iterator())
48+ private val hypoIter: TermIterator = TermIterator (hypothesis.terms.iterator())
49+ private val refIter: TermIterator = TermIterator (reference.terms.iterator())
4450
4551 /* * Iterate through the terms of both layers simultaneously and compare them. */
4652 init {
4753 // While non null, compare
4854 while (hypoIter.current != null && refIter.current != null ) {
49- compareTerm(TermComparison (hypoIter.current!! , refIter.current!! ))
55+ if (hypoIter.chars == refIter.chars) {
56+ match(TermComparison (hypoIter.current!! , refIter.current!! ))
57+ hypoIter.next()
58+ refIter.next()
59+ } else {
60+ if (refIter.chars < hypoIter.chars) {
61+ noMatch(refIter.current!! )
62+ refIter.next()
63+ } else {
64+ hypoIter.next()
65+ }
66+ }
5067 }
5168 // Only one or both are null. So refIter.current can be non-null.
5269 refIter.current?.let { noMatch(it) }
5370 // And add any remaining terms
5471 refIter.forEachRemaining { t -> noMatch(t!! ) }
5572 }
5673
57- private fun compareTerm (comp : TermComparison ) {
58- // Act on the comparison
59- if (comp.equalAnnotation(Annotation .TOKEN )) {
60- match(comp)
61- hypoIter.next()
62- refIter.next()
63- } else {
64- if (truncatedPCMatch(comp)) {
65- // If so, still match it.
66- match(comp)
67- // and fix iterators for the next terms
68- fixIter(comp)
69- } else {
70- noMatch(comp.refTerm)
71- }
72- }
73- }
74-
75- private fun fixIter (comp : TermComparison ) {
76- hypoIter.next()
77- refIter.next()
78- // Now, the shorter iterator needs to be advanced until it matches.
79- val hypoShorter: Boolean = comp.hypoTerm.token.length < comp.refTerm.token.length
80- val shorterIter = if (hypoShorter) hypoIter else refIter
81- val termToMatch = if (hypoShorter) refIter.current else hypoIter.current
82- while (termToMatch != null && shorterIter.current != null && ! truncatedPCMatch(termToMatch, shorterIter.current!! )) {
83- // Advance
84- shorterIter.next()
85- }
86- }
87-
8874 private fun match (comp : TermComparison ) {
8975 if (filter?.filter(comp) != false ) {
9076 matches.add(comp)
0 commit comments