Skip to content

Commit 50c0eed

Browse files
committed
Copy ahocorasick code to project
1 parent 5f84ed3 commit 50c0eed

40 files changed

+1826
-21
lines changed
13.7 KB
Binary file not shown.
13.6 KB
Binary file not shown.

gradle/libs.versions.toml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
[versions]
2-
ahocorasickVersion="0.6.3"
32
androidGifDrawableVersion="1.2.23"
43
androidsvgAarVersion="1.4"
5-
appcompatVersion="1.6.1"
4+
appcompatVersion="1.7.0"
65
bcprovJdk15onVersion="1.70"
76
coil="3.0.0-alpha06"
87
coreKtxVersion="1.13.1"
98
exoplayerVersion="2.19.1"
10-
firebaseCrashlyticsGradleVersion="2.9.9"
11-
fragmentKtxVersion="1.7.0"
9+
firebaseCrashlyticsGradleVersion="3.0.1"
10+
fragmentKtxVersion="1.8.0"
1211
glideVersion="4.16.0"
13-
googleServicesVersion="4.4.1"
12+
googleServicesVersion="4.4.2"
1413
gradle= "8.3.2"
1514
accompanistDrawablepainterVersion = "0.34.0"
16-
cameraCoreVersion = "1.4.0-alpha04"
15+
cameraCoreVersion = "1.4.0-beta02"
1716
apollo = "3.2.1"
18-
compose = "1.6.7"
17+
compose = "1.6.8"
1918
activityComposeVersion = "1.9.0"
20-
composeBom = "2024.05.00"
19+
composeBom = "2024.06.00"
2120
coreSplashscreenVersion = "1.0.1"
2221
coreVersion = "3.5.3"
2322
datastorePreferencesVersion = "1.1.1"
24-
firebaseCrashlyticsKtxVersion = "18.6.3"
25-
firebaseBomVersion = "32.8.0"
23+
firebaseCrashlyticsKtxVersion = "19.0.1"
24+
firebaseBomVersion = "33.1.0"
2625
gsonVersion="2.10.1"
2726
guavaVersion="33.0.0-jre"
2827
jsoupVersion="1.15.3"
@@ -34,12 +33,12 @@ kotlinxCoroutinesVersion ="1.8.1"
3433
ktor = "3.0.0-beta-1"
3534
leakcanaryAndroidVersion = "2.12"
3635
lifecycleExtensionsVersion="2.2.0"
37-
lifecycleViewmodelKtxVersion="2.8.0"
36+
lifecycleViewmodelKtxVersion="2.8.2"
3837
markwon="4.6.2"
3938
materialVersion="1.12.0"
4039
media3 = "1.3.1"
4140
navigationComposeVersion = "2.7.7"
42-
materialIconsExtendedVersion = "1.7.0-alpha05"
41+
materialIconsExtendedVersion = "1.7.0-beta03"
4342
material3Version = "1.2.1"
4443
okhttpVersion="4.12.0"
4544
openaiClientVersion = "3.6.2"
@@ -50,14 +49,13 @@ room = "2.6.1"
5049
snakeYamlVersion = "v1.18-android"
5150
subsamplingScaleImageViewAndroidxVersion="3.10.0"
5251
transitionVersion="1.5.0"
53-
viewpager2Version = "1.0.0"
52+
viewpager2Version = "1.1.0"
5453
workRuntimeKtxVersion = "2.9.0"
5554
ztZipVersion = "1.16"
5655
devtoolsKspVersion = "1.9.23-1.0.20"
5756
pagingVersion = "3.3.0"
5857

5958
[libraries]
60-
ahocorasick = { module = "org.ahocorasick:ahocorasick", version.ref = "ahocorasickVersion" }
6159
android-gif-drawable = { module = "pl.droidsonroids.gif:android-gif-drawable", version.ref = "androidGifDrawableVersion" }
6260
androidsvg-aar = { module = "com.caverock:androidsvg-aar", version.ref = "androidsvgAarVersion" }
6361
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompatVersion" }

lib/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ dependencies {
7979
// https://github.com/davemorrissey/subsampling-scale-image-view
8080
api(libs.subsampling.scale.image.view.androidx)
8181

82-
implementation(libs.ahocorasick) // For pinyin
8382
implementation(libs.bcprov.jdk15on)
8483
implementation(libs.bcpkix.jdk15on)
8584
api(libs.ktor.client.core)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.ismartcoding.lib.ahocorasick.interval;
2+
3+
4+
/**
5+
* Responsible for tracking the start and end bounds, which are reused by
6+
* both {@link Emit} and {@link PayloadEmit}.
7+
*/
8+
public class Interval implements Intervalable {
9+
10+
private final int start;
11+
private final int end;
12+
13+
/**
14+
* Constructs an interval with a start and end position.
15+
*
16+
* @param start The interval's starting text position.
17+
* @param end The interval's ending text position.
18+
*/
19+
public Interval(final int start, final int end) {
20+
this.start = start;
21+
this.end = end;
22+
}
23+
24+
/**
25+
* Returns the starting offset into the text for this interval.
26+
*
27+
* @return A number between 0 (start of text) and the text length.
28+
*/
29+
@Override
30+
public int getStart() {
31+
return this.start;
32+
}
33+
34+
/**
35+
* Returns the ending offset into the text for this interval.
36+
*
37+
* @return A number between getStart() + 1 and the text length.
38+
*/
39+
@Override
40+
public int getEnd() {
41+
return this.end;
42+
}
43+
44+
/**
45+
* Returns the length of the interval.
46+
*
47+
* @return The end position less the start position, plus one.
48+
*/
49+
@Override
50+
public int size() {
51+
return end - start + 1;
52+
}
53+
54+
/**
55+
* Answers whether the given interval overlaps this interval
56+
* instance.
57+
*
58+
* @param other the other interval to check for overlap
59+
* @return true The intervals overlap.
60+
*/
61+
public boolean overlapsWith(final Interval other) {
62+
return this.start <= other.getEnd() &&
63+
this.end >= other.getStart();
64+
}
65+
66+
public boolean overlapsWith(int point) {
67+
return this.start <= point && point <= this.end;
68+
}
69+
70+
@Override
71+
public boolean equals(Object o) {
72+
if (!(o instanceof Intervalable)) {
73+
return false;
74+
}
75+
Intervalable other = (Intervalable) o;
76+
return this.start == other.getStart() &&
77+
this.end == other.getEnd();
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return this.start % 100 + this.end % 100;
83+
}
84+
85+
@Override
86+
public int compareTo(Object o) {
87+
if (!(o instanceof Intervalable)) {
88+
return -1;
89+
}
90+
Intervalable other = (Intervalable) o;
91+
int comparison = this.start - other.getStart();
92+
return comparison != 0 ? comparison : this.end - other.getEnd();
93+
}
94+
95+
/**
96+
* Returns the starting offset and ending offset separated
97+
* by a full colon (:).
98+
*
99+
* @return A non-null String, never empty.
100+
*/
101+
@Override
102+
public String toString() {
103+
return this.start + ":" + this.end;
104+
}
105+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.ismartcoding.lib.ahocorasick.interval;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class IntervalNode {
8+
9+
private enum Direction {LEFT, RIGHT}
10+
11+
private IntervalNode left;
12+
private IntervalNode right;
13+
private int point;
14+
private List<Intervalable> intervals = new ArrayList<>();
15+
16+
public IntervalNode(final List<Intervalable> intervals) {
17+
this.point = determineMedian(intervals);
18+
19+
final List<Intervalable> toLeft = new ArrayList<>();
20+
final List<Intervalable> toRight = new ArrayList<>();
21+
22+
for (Intervalable interval : intervals) {
23+
if (interval.getEnd() < this.point) {
24+
toLeft.add(interval);
25+
} else if (interval.getStart() > this.point) {
26+
toRight.add(interval);
27+
} else {
28+
this.intervals.add(interval);
29+
}
30+
}
31+
32+
if (toLeft.size() > 0) {
33+
this.left = new IntervalNode(toLeft);
34+
}
35+
if (toRight.size() > 0) {
36+
this.right = new IntervalNode(toRight);
37+
}
38+
}
39+
40+
public int determineMedian(final List<Intervalable> intervals) {
41+
int start = -1;
42+
int end = -1;
43+
for (Intervalable interval : intervals) {
44+
int currentStart = interval.getStart();
45+
int currentEnd = interval.getEnd();
46+
if (start == -1 || currentStart < start) {
47+
start = currentStart;
48+
}
49+
if (end == -1 || currentEnd > end) {
50+
end = currentEnd;
51+
}
52+
}
53+
return (start + end) / 2;
54+
}
55+
56+
public List<Intervalable> findOverlaps(final Intervalable interval) {
57+
final List<Intervalable> overlaps = new ArrayList<>();
58+
59+
if (this.point < interval.getStart()) {
60+
// Tends to the right
61+
addToOverlaps(interval, overlaps, findOverlappingRanges(this.right, interval));
62+
addToOverlaps(interval, overlaps, checkForOverlapsToTheRight(interval));
63+
} else if (this.point > interval.getEnd()) {
64+
// Tends to the left
65+
addToOverlaps(interval, overlaps, findOverlappingRanges(this.left, interval));
66+
addToOverlaps(interval, overlaps, checkForOverlapsToTheLeft(interval));
67+
} else {
68+
// Somewhere in the middle
69+
addToOverlaps(interval, overlaps, this.intervals);
70+
addToOverlaps(interval, overlaps, findOverlappingRanges(this.left, interval));
71+
addToOverlaps(interval, overlaps, findOverlappingRanges(this.right, interval));
72+
}
73+
74+
return overlaps;
75+
}
76+
77+
protected void addToOverlaps(
78+
final Intervalable interval,
79+
final List<Intervalable> overlaps,
80+
final List<Intervalable> newOverlaps) {
81+
for (final Intervalable currentInterval : newOverlaps) {
82+
if (!currentInterval.equals(interval)) {
83+
overlaps.add(currentInterval);
84+
}
85+
}
86+
}
87+
88+
protected List<Intervalable> checkForOverlapsToTheLeft(final Intervalable interval) {
89+
return checkForOverlaps(interval, Direction.LEFT);
90+
}
91+
92+
protected List<Intervalable> checkForOverlapsToTheRight(final Intervalable interval) {
93+
return checkForOverlaps(interval, Direction.RIGHT);
94+
}
95+
96+
protected List<Intervalable> checkForOverlaps(
97+
final Intervalable interval, final Direction direction) {
98+
final List<Intervalable> overlaps = new ArrayList<>();
99+
100+
for (final Intervalable currentInterval : this.intervals) {
101+
switch (direction) {
102+
case LEFT:
103+
if (currentInterval.getStart() <= interval.getEnd()) {
104+
overlaps.add(currentInterval);
105+
}
106+
break;
107+
case RIGHT:
108+
if (currentInterval.getEnd() >= interval.getStart()) {
109+
overlaps.add(currentInterval);
110+
}
111+
break;
112+
}
113+
}
114+
115+
return overlaps;
116+
}
117+
118+
protected List<Intervalable> findOverlappingRanges(IntervalNode node, Intervalable interval) {
119+
return node == null
120+
? Collections.<Intervalable>emptyList()
121+
: node.findOverlaps(interval);
122+
}
123+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.ismartcoding.lib.ahocorasick.interval;
2+
3+
import java.util.List;
4+
import java.util.Set;
5+
import java.util.TreeSet;
6+
7+
import static java.util.Collections.sort;
8+
9+
public class IntervalTree {
10+
11+
private final IntervalNode rootNode;
12+
13+
public IntervalTree(List<Intervalable> intervals) {
14+
this.rootNode = new IntervalNode(intervals);
15+
}
16+
17+
public List<Intervalable> removeOverlaps(final List<Intervalable> intervals) {
18+
19+
// Sort the intervals on size, then left-most position
20+
sort(intervals, new IntervalableComparatorBySize());
21+
22+
final Set<Intervalable> removeIntervals = new TreeSet<>();
23+
24+
for (final Intervalable interval : intervals) {
25+
// If the interval was already removed, ignore it
26+
if (removeIntervals.contains(interval)) {
27+
continue;
28+
}
29+
30+
// Remove all overallping intervals
31+
removeIntervals.addAll(findOverlaps(interval));
32+
}
33+
34+
// Remove all intervals that were overlapping
35+
for (final Intervalable removeInterval : removeIntervals) {
36+
intervals.remove(removeInterval);
37+
}
38+
39+
// Sort the intervals, now on left-most position only
40+
sort(intervals, new IntervalableComparatorByPosition());
41+
42+
return intervals;
43+
}
44+
45+
public List<Intervalable> findOverlaps(final Intervalable interval) {
46+
return rootNode.findOverlaps(interval);
47+
}
48+
49+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.ismartcoding.lib.ahocorasick.interval;
2+
3+
public interface Intervalable extends Comparable {
4+
5+
int getStart();
6+
7+
int getEnd();
8+
9+
int size();
10+
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ismartcoding.lib.ahocorasick.interval;
2+
3+
import java.util.Comparator;
4+
5+
public class IntervalableComparatorByPosition implements Comparator<Intervalable> {
6+
7+
@Override
8+
public int compare(final Intervalable intervalable, final Intervalable intervalable2) {
9+
return intervalable.getStart() - intervalable2.getStart();
10+
}
11+
12+
}

0 commit comments

Comments
 (0)