Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

88
LINES

< > BotCompany Repo | #1029015 // deepDoubleWordIndex_search - does the full search [dev.]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Compilation Failed (4190L/26K).

// returns null if there are no letters in the query
// otherwise returns actual search result
static <A> Iterable<A> deepDoubleWordIndex_search(DoubleWordIndex<WithIntArray<A>> index, Map<A, S> texts, S query, O... _) {
  optPar bool debug;
  L<IntRange> ranges = index.wordRanges(query);
  if (empty(ranges)) null;
  int nRanges = l(ranges);
  new Map<A, MultiSetMap<Int>> theMap; // for every snippet, a map of string position to word index

  // special case, just a single word in query
  if (l(ranges) == 1 && first(ranges).start == 0 && first(ranges).end == l(query)) {
    new Set<A> seen;
    ret nestedIterator(containingIC(index.index1.words(), query), fullWord ->
      mapI_notNulls_if1(index.index1.get(fullWord), e -> addAndReturnIfNew(seen, e!)));
  }
  
  Set<A> baseSet = null;
  /*if (int iWord = 1; i < nRanges-1; i++) { // go through all "full" words
    IntRange r = ranges.get(iWord);
    S word = substring(query, r);
    Set<WithIntArray<S>> entries = index.index1.get(word);
    if (baseSet == null) baseSet = ...
  }*/
  
  for iWord to nRanges: { // go through words in query
    IntRange r = ranges.get(iWord);
    S word = substring(query, r);
    Cl<S> l; // all matching words in index
    WordIndex<WithIntArray<A>> indexToUse = index.index1;
      
    if (r.start == 0) { // look for ending of word - use reverse index
      l = prefixSubSet(index.index2.words(), reversed(word));
      if (empty(l)) ret emptyList();
      if (debug) print("word=" + word + ", fullWords=" + l);
      
      // special loop that accounts for length of actual word
      for (S fullWord : l)
        for (WithIntArray<A> entry : index.index2.index.get(fullWord)) {
          if (baseSet != null && !baseSet.contains(entry!)) continue;
          MultiSetMap<Int> msm = theMap.get(entry!);
          if (msm == null) theMap.put(entry!, msm = new MultiSetMap);
          int ofs = l(fullWord)-l(word)-r.start;
          for (int i : entry.array) {
            int idx = i+ofs;
            if (debug) print("Got idx " + idx);
            if (idx >= 0)
              msm.put(idx, iWord);
          }
        }
      continue;
    } else if (r.end == l(query)) { // look for start of word
      l = prefixSubSet(index.index1.words(), word);
    } else // look for complete word
      l = ll(word);
      
    if (empty(l)) ret emptyList();
    
    if (debug) print("word=" + word + ", fullWords=" + l);
      
    for (S fullWord : l)
      for (WithIntArray<A> entry : indexToUse.index.get(fullWord)) {
        if (baseSet != null && !baseSet.contains(entry!)) continue;
        if (debug) print("Got entry " + entry);
        MultiSetMap<Int> msm = theMap.get(entry!);
        if (msm == null) theMap.put(entry!, msm = new MultiSetMap);
        for (int i : entry.array) {
          int idx = i-r.start;
          if (debug) print("Got idx " + idx);
          if (idx >= 0)
            msm.put(idx, iWord);
        }
      }
  }
  
  if (debug) print("theMap size=" + l(theMap));
  new L<A> out;
  theLoop: for (A snippet, MultiSetMap<Int> msm : theMap) {
    if (debug) print("snippet " + snippet);
    for (int position, Set<Int> wordIndices : msm.data) {
      if (debug) print("position " + position + ": " + l(wordIndices) + "/" + nRanges);
      if (l(wordIndices) == nRanges) {
        out.add(snippet);
        continue theLoop;
      }
    }
  }
  ret out;
}

Author comment

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: #1029015
Snippet name: deepDoubleWordIndex_search - does the full search [dev.]
Eternal ID of this version: #1029015/3
Text MD5: e664b446ec80b8ae4a03b2f59000ed5f
Transpilation MD5: 59b8dad289496b5ccf11504d59da4d21
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:47:05
Source code size: 3469 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 185 / 270
Version history: 2 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)