package com.kryptnostic.api.v1.indexing.analysis;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kryptnostic.kodex.v1.indexing.QueryAnalyzer;
import com.kryptnostic.kodex.v1.indexing.analysis.Analyzer;

/**
 * Basic tokenizer that uses a regular expression to parse a source string. 
 * @author Matthew Tamayo-Rios &lt;matthew@kryptnostic.com&gt;
 */
public class TokenizingWhitespaceAnalyzer implements Analyzer,QueryAnalyzer {
	private static final Pattern onlyWords = Pattern.compile("([a-zA-Z0-9]+)");
	//TODO: Make a generic analyzer that takes in a pattern and indexes on resulting tokens.
	public Map<String, List<Integer>> analyze( String source ) {
		Matcher m = onlyWords.matcher( source );
		Map<String, List<Integer>> hits = Maps.newHashMap();
		while( m.find() ) {
			int location = m.start();
			String s = m.group();
			List<Integer> locations = hits.get( s );
			if( locations == null ) {
				locations = Lists.newArrayList();
				hits.put( s.toLowerCase() ,  locations );
			} 
			locations.add( location );
		}
		
		return hits;
	}
    
	@Override
    public Set<String> analyzeQuery(String query) {
        return ImmutableSet.copyOf( analyze( query ).keySet() );
    }	
}

@@ -498,18 +498,21 @@
  string.
+%0A *
  %0A
-
  * @auth
@@ -632,16 +632,17 @@
 nalyzer,
+ 
 QueryAna
@@ -649,17 +649,20 @@
 lyzer %7B%0A
-%09
+    
 private 
@@ -710,16 +710,17 @@
 compile(
+ 
 %22(%5Ba-zA-
@@ -731,14 +731,20 @@
 %5D+)%22
+ 
 );%0A
-%09
+%0A    
 //
+ 
 TODO
@@ -826,17 +826,20 @@
 tokens.%0A
-%09
+    
 public M
@@ -891,18 +891,24 @@
 rce ) %7B%0A
-%09%09
+        
 Matcher 
@@ -940,18 +940,24 @@
 urce );%0A
-%09%09
+        
 Map%3CStri
@@ -1005,15 +1005,22 @@
 ();%0A
-%09%09
+        
 while
+ 
 ( m.
@@ -1030,19 +1030,28 @@
 d() ) %7B%0A
-%09%09%09
+            
 int loca
@@ -1068,19 +1068,28 @@
 tart();%0A
-%09%09%09
+            
 String s
@@ -1100,21 +1100,44 @@
 .group()
-;%0A%09%09%09
+.toLowerCase();%0A            
 List%3CInt
@@ -1173,13 +1173,23 @@
  );%0A
-%09%09%09
+            
 if
+ 
 ( lo
@@ -1208,20 +1208,32 @@
 ull ) %7B%0A
-%09%09%09%09
+                
 location
@@ -1262,63 +1262,76 @@
 ();%0A
-%09%09%09%09hits.put( s.toLowerCase() ,  locations );%0A%09%09%09%7D %0A%09%09%09
+                hits.put( s, locations );%0A            %7D%0A            
 loca
@@ -1357,17 +1357,27 @@
  );%0A
-%09%09%7D%0A%09%09%0A%09%09
+        %7D%0A%0A        
 retu
@@ -1389,17 +1389,19 @@
 ts;%0A
-%09%7D
+    %7D%0A
 %0A    
-%0A%09
 @Ove
@@ -1442,16 +1442,17 @@
 zeQuery(
+ 
 String q
@@ -1455,16 +1455,17 @@
 ng query
+ 
 ) %7B%0A    
@@ -1529,13 +1529,12 @@
  );%0A
+
     %7D
-%09
 %0A%7D%0A

fix lowercase bug in analyzer