Rev 5017 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.services;import in.shop2020.serving.utils.Utils;import java.io.File;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.commons.io.FileUtils;import org.apache.log4j.Logger;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;/**** @author rajveer** Auto suggest service will help user to search some term. This is based on turnary search tree.*/public class AutoSuggestService{private Node root;private static Logger log = Logger.getLogger(AutoSuggestService.class);private static AutoSuggestService autoSuggestService;static{synchronized(AutoSuggestService.class){autoSuggestService = new AutoSuggestService();autoSuggestService.initialize();}}private void initialize() {log.info("Starting instance of Autosuggest service");try {Set<String> titleSet = new HashSet<String>();Gson g = new Gson();java.lang.reflect.Type typeOfT = new TypeToken<Map<Long, List<String>>>(){}.getType();Map<Long, List<String>> synonyms = g.fromJson(FileUtils.readFileToString(new File(Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "autosuggest.json")), typeOfT);if(synonyms != null){for(List<String> entry : synonyms.values()){for(String title : entry){titleSet.add(title);}}List<String> titlelist = new ArrayList<String>(titleSet);java.util.Collections.sort(titlelist);for(String title : titlelist){addItemName(title);}}} catch (Exception e) {log.error("", e);}log.info("Done with starting Autosuggest service");}public static AutoSuggestService getAutoSuggestServiceInstance(){return autoSuggestService;}private Node addChars(char[] s, int position, Node node, String string){if (node == null) {node = new Node(s[position], null);}if (s[position] < node.storedChar) {node.left = addChars(s, position, node.left, string);}else if (s[position] > node.storedChar) {node.right = addChars(s, position, node.right, string);}else {if (position + 1 == s.length) {node.addItem(string);}else {node.center = addChars(s, position + 1, node.center, string);}}return node;}public void addStringPart(String part, String string){if (part == null || part == "") return;root = addChars(part.toCharArray(), 0, root, string);}/*** Add String to the tree* @param string*/private void addItemName(String string) {if (string == null || string == "") return;String[] parts = string.trim().toLowerCase().split("\\s+");for(String part : parts){addStringPart(part, string);}}/*** Get list of matching product names.* @param str* @param resultCount* @return*/public List<String> getMatchingQueries(String str, int resultCount){Set<String> totalSet = new HashSet<String>();String[] parts = str.trim().toLowerCase().split("\\s+");for(String part : parts){Node node = getPartialMatchNode(part);Set<String> set = new HashSet<String>();getChildElements(node, set, part);if(totalSet.isEmpty()){totalSet.addAll(set);}else{totalSet.retainAll(set);}}List<String> items = new ArrayList<String>(totalSet);if(items.size() > resultCount){return items.subList(0, resultCount-1);}else{return items;}}/*** Return the node till which we are able to match the word.* @param str* @return*/private Node getPartialMatchNode(String str){char[] s = str.toCharArray();int pos = 0;Node node = root;while (node != null){if (s[pos] < node.storedChar) { node = node.left; }else if (s[pos] > node.storedChar) { node = node.right; }else{if (++pos == s.length){return node;}node = node.center;}}return node;}private void getChildElements(Node node, Set<String> set, String str){if(node == null){return;}if(node.itemSet != null && !node.itemSet.isEmpty()){for(String item_name : node.itemSet) {String ignore_item_name = item_name.toLowerCase();if(ignore_item_name.indexOf(str) != -1){set.add(item_name);}}}getChildElements(node.center, set, str);getChildElements(node.left, set, str);getChildElements(node.right, set, str);}@Overridepublic String toString() {return "AutoSuggest [root=" + root + "]";}public static void main(String[] args) throws Exception {AutoSuggestService as = new AutoSuggestService();as.addItemName("Nokia N8");as.addItemName("Nokia C5-00");as.addItemName("Nokia C3-00");as.addItemName("Nokia X2-01");as.addItemName("Samsung Galaxy Pro");as.addItemName("Samsung Galaxy Pop");as.addItemName("Samsung Galaxy 551 I5510");System.out.println(as.getMatchingQueries("noki", 10));System.out.println(as.getMatchingQueries("nok", 10));System.out.println(as.getMatchingQueries("n8", 10));System.out.println(as.getMatchingQueries("po gala", 10));}}class Node{char storedChar;Node left, center, right;Set<String> itemSet;public void addItem(String itemName){if(itemSet == null || itemSet.isEmpty()){itemSet = new HashSet<String>();}itemSet.add(itemName);}public Node(char ch, String itemName){this.storedChar = ch;if(itemName != null){itemSet = new HashSet<String>();itemSet.add(itemName);}}@Overridepublic String toString() {return "Node [storedChar=" + storedChar + ", left=" + left+ ", center=" + center + ", right=" + right + ", itemSet="+ itemSet + "]";}}