Libraryless. Click here for Pure Java version (4384L/26K).
1 | // returns a prefiltered list of elements; you still need to do a |
2 | // full-text search on those. |
3 | // If it returns null, you have to search all elements |
4 | static <A> Iterable<Pair<A, Cl<Int>>> deepDoubleWordIndex_lookupString_withPositions(DoubleWordIndex<WithIntArray<A>> index, S query, O... _) { |
5 | optPar bool debug; |
6 | L<IntRange> ranges = index.wordRanges(query); |
7 | if (empty(ranges)) null; |
8 | int nRanges = l(ranges); |
9 | new Map<A, MultiSetMap<Int>> theMap; // for every snippet, a map of string position to word index |
10 | |
11 | // special case, just a single word in query |
12 | if (l(ranges) == 1 && first(ranges).start == 0 && first(ranges).end == l(query)) { |
13 | ret nestedIterator(containingIC(index.index1.words(), query), fullWord -> { |
14 | int ofs = l(fullWord)-l(query); |
15 | ret mapI_notNulls(index.index1.get(fullWord), entry -> { |
16 | L<Int> positions = null; |
17 | for (int i : entry.array) { |
18 | int idx = i-ofs; |
19 | if (idx >= 0) { |
20 | if (positions == null) positions = new L; |
21 | positions.add(idx); |
22 | } |
23 | } |
24 | if (positions != null) |
25 | ret pair(entry!, positions); |
26 | null; |
27 | }); |
28 | }); |
29 | } |
30 | |
31 | for iWord over ranges: { // go through words in query |
32 | IntRange r = ranges.get(iWord); |
33 | S word = substring(query, r); |
34 | Cl<S> l; // all matching words in index |
35 | WordIndex<WithIntArray<A>> indexToUse = index.index1; |
36 | |
37 | if (r.start == 0) { // look for ending of word - use reverse index |
38 | l = prefixSubSet(index.index2.words(), reversed(word)); |
39 | if (empty(l)) ret emptyList(); |
40 | if (debug) print("word=" + word + ", fullWords=" + l); |
41 | |
42 | // special loop that accounts for length of actual word |
43 | for (S fullWord : l) |
44 | for (WithIntArray<A> entry : index.index2.index.get(fullWord)) { |
45 | MultiSetMap<Int> msm = theMap.get(entry!); |
46 | if (msm == null) theMap.put(entry!, msm = new MultiSetMap); |
47 | int ofs = l(fullWord)-l(word)-r.start; |
48 | for (int i : entry.array) { |
49 | int idx = i+ofs; |
50 | if (debug) print("Got idx " + idx); |
51 | if (idx >= 0) |
52 | msm.put(idx, iWord); |
53 | } |
54 | } |
55 | continue; |
56 | } else if (r.end == l(query)) { // look for start of word |
57 | l = prefixSubSet(index.index1.words(), word); |
58 | } else // look for complete word |
59 | l = ll(word); |
60 | |
61 | if (empty(l)) ret emptyList(); |
62 | |
63 | if (debug) print("word=" + word + ", fullWords=" + l); |
64 | |
65 | for (S fullWord : l) |
66 | for (WithIntArray<A> entry : indexToUse.index.get(fullWord)) { |
67 | if (debug) print("Got entry " + entry); |
68 | MultiSetMap<Int> msm = theMap.get(entry!); |
69 | if (msm == null) theMap.put(entry!, msm = new MultiSetMap); |
70 | for (int i : entry.array) { |
71 | int idx = i-r.start; |
72 | if (debug) print("Got idx " + idx); |
73 | if (idx >= 0) |
74 | msm.put(idx, iWord); |
75 | } |
76 | } |
77 | } |
78 | |
79 | /*if (debug)*/ print("theMap size=" + l(theMap)); |
80 | ret mapI_nonNulls_if1(theMap.entrySet(), e -> { |
81 | A snippet = e.getKey(); |
82 | MultiSetMap<Int> msm = e.getValue(); |
83 | |
84 | if (debug) print("snippet " + snippet); |
85 | L<Int> positions = null; |
86 | for (int position, Set<Int> wordIndices : msm.data) { |
87 | if (debug) print("position " + position + ": " + l(wordIndices) + "/" + nRanges); |
88 | if (l(wordIndices) == nRanges) { |
89 | if (positions == null) positions = new L; |
90 | positions.add(position); |
91 | } |
92 | } |
93 | if (positions != null) |
94 | ret pair(snippet, positions); |
95 | null; |
96 | }); |
97 | } |
Began life as a copy of #1029012
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1029016 |
Snippet name: | deepDoubleWordIndex_lookupString_withPositions |
Eternal ID of this version: | #1029016/10 |
Text MD5: | 281342e7b9f46b27f268a2b90a78a845 |
Transpilation MD5: | e6cee25de9098bc53f327e18a6eb063f |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-07-16 23:12:24 |
Source code size: | 3639 bytes / 97 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 220 / 317 |
Version history: | 9 change(s) |
Referenced in: | [show references] |