Subversion Repositories SmartDukaan

Rev

Rev 5084 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3283 rajveer 1
package in.shop2020.serving.services;
2
 
3
 
5084 phani.kuma 4
import in.shop2020.serving.utils.Utils;
5
import java.io.File;
3283 rajveer 6
import java.util.ArrayList;
5453 phani.kuma 7
import java.util.HashMap;
3283 rajveer 8
import java.util.HashSet;
9
import java.util.List;
5084 phani.kuma 10
import java.util.Map;
5453 phani.kuma 11
import java.util.Map.Entry;
3283 rajveer 12
import java.util.Set;
13
 
5084 phani.kuma 14
import org.apache.commons.io.FileUtils;
3283 rajveer 15
import org.apache.log4j.Logger;
16
 
5084 phani.kuma 17
import com.google.gson.Gson;
18
import com.google.gson.reflect.TypeToken;
19
 
3283 rajveer 20
/**
21
 * 
22
 * @author rajveer
23
 * 
24
 * Auto suggest service will help user to search some term. This is based on turnary search tree. 
25
 */
26
public class AutoSuggestService
27
{
28
	private Node root;
29
	private static Logger log = Logger.getLogger(AutoSuggestService.class);
30
	private static AutoSuggestService autoSuggestService;
31
 
32
	static{
33
		synchronized(AutoSuggestService.class){
34
			autoSuggestService = new AutoSuggestService();
35
			autoSuggestService.initialize();
36
		}
37
	}
5084 phani.kuma 38
 
3283 rajveer 39
	private void initialize() {
40
		log.info("Starting instance of Autosuggest service");
41
		try {
5084 phani.kuma 42
			Gson g = new Gson();
5453 phani.kuma 43
			java.lang.reflect.Type typeOfT = new TypeToken<Map<String, List<String>>>(){}.getType();
44
			Map<String, List<String>> synonyms = g.fromJson(FileUtils.readFileToString(new File(Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "autosuggest.json")), typeOfT);
5084 phani.kuma 45
			if(synonyms != null){
5453 phani.kuma 46
				for(Entry<String, List<String>> entry : synonyms.entrySet()){
47
					for(String title : entry.getValue()){
48
						addItemName(title, entry.getKey());
5084 phani.kuma 49
					}
50
				}
51
			}
52
		} catch (Exception e) {
53
			log.error("", e);
3283 rajveer 54
		}
55
		log.info("Done with starting Autosuggest service");
56
	}
57
 
58
	public static AutoSuggestService getAutoSuggestServiceInstance(){
59
		return autoSuggestService;
60
	}
61
 
5453 phani.kuma 62
	private Node addChars(char[] s, int position, Node node, String string, String displaystring)
3283 rajveer 63
    {
64
        if (node == null) { 
5453 phani.kuma 65
        	node = new Node(s[position], null, null); 
3283 rajveer 66
        }
4975 phani.kuma 67
        if (s[position] < node.storedChar) {
5453 phani.kuma 68
        	node.left = addChars(s, position, node.left, string, displaystring); 
3283 rajveer 69
        }
4975 phani.kuma 70
        else if (s[position] > node.storedChar) {
5453 phani.kuma 71
        	node.right = addChars(s, position, node.right, string, displaystring); 
4975 phani.kuma 72
        }
73
        else {
74
        	if (position + 1 == s.length) {
5453 phani.kuma 75
        		node.addItem(string, displaystring);
3283 rajveer 76
        	}
4975 phani.kuma 77
        	else {
5453 phani.kuma 78
        		node.center =  addChars(s, position + 1, node.center, string, displaystring);
3283 rajveer 79
        	}
4975 phani.kuma 80
        }
3283 rajveer 81
        return node;
82
    }
83
 
5453 phani.kuma 84
    public void addStringPart(String part, String string, String displaystring)
3283 rajveer 85
    {
86
        if (part == null || part == "") return;
5453 phani.kuma 87
        root = addChars(part.toCharArray(), 0, root, string, displaystring);
3283 rajveer 88
    }
89
 
90
    /**
91
     * Add String to the tree
92
     * @param string
93
     */
5453 phani.kuma 94
    private void addItemName(String string, String displayString) {
3283 rajveer 95
        if (string == null || string == "") return;
96
        String[] parts = string.trim().toLowerCase().split("\\s+");
97
        for(String part : parts){
5453 phani.kuma 98
        	addStringPart(part, string, displayString);
3283 rajveer 99
        }
100
    }
101
 
102
    /**
103
     * Get list of matching product names.
104
     * @param str
105
     * @param resultCount
106
     * @return
107
     */
108
    public List<String> getMatchingQueries(String str, int resultCount){
109
    	Set<String> totalSet = new HashSet<String>();
110
    	String[] parts = str.trim().toLowerCase().split("\\s+");
111
        for(String part : parts){
112
        	Node node = getPartialMatchNode(part);
113
        	Set<String> set = new HashSet<String>();
5017 phani.kuma 114
        	getChildElements(node, set, part);
3283 rajveer 115
        	if(totalSet.isEmpty()){
5453 phani.kuma 116
        		totalSet.addAll(set);
3283 rajveer 117
        	}else{
118
        		totalSet.retainAll(set);
119
        	}
120
        }
121
        List<String> items = new ArrayList<String>(totalSet);
122
        if(items.size() > resultCount){
123
        	return items.subList(0, resultCount-1);
124
        }else{
125
        	return items;	
126
        }
127
    }
128
 
129
    /**
130
     * Return the node till which we are able to match the word.
131
     * @param str
132
     * @return
133
     */
134
    private Node getPartialMatchNode(String str){
135
	    char[] s = str.toCharArray();
136
	    int pos = 0;
137
	    Node node = root;
138
	    while (node != null)
139
	    {
140
	        if (s[pos] < node.storedChar) { node = node.left; }
141
	        else if (s[pos] > node.storedChar) { node = node.right; }
142
	        else
143
	        {
144
	            if (++pos == s.length){
4980 phani.kuma 145
	            	return node;
3283 rajveer 146
	            }
147
	            node = node.center;
148
	        }
149
	    }
150
	    return node;
151
    }
152
 
5017 phani.kuma 153
    private void getChildElements(Node node, Set<String>  set, String str){
3283 rajveer 154
    	if(node == null){
155
    		return;
156
    	}
5453 phani.kuma 157
    	if(node.itemMap != null && !node.itemMap.isEmpty()){
158
    		for(Entry<String, String> entry : node.itemMap.entrySet()) {
159
    			String item_name = entry.getKey();
160
    			String item_displayname = entry.getValue();
5017 phani.kuma 161
    			String ignore_item_name = item_name.toLowerCase();
162
    			if(ignore_item_name.indexOf(str) != -1){
5453 phani.kuma 163
    				set.add(item_displayname);
5017 phani.kuma 164
    			}
165
    		}
3283 rajveer 166
    	}
5017 phani.kuma 167
    	getChildElements(node.center, set, str);	
168
    	getChildElements(node.left, set, str);
169
    	getChildElements(node.right, set, str);
3283 rajveer 170
    }
171
 
172
 
173
	@Override
174
	public String toString() {
175
		return "AutoSuggest [root=" + root + "]";
176
	}
177
 
178
	public static void main(String[] args) throws Exception {
179
		AutoSuggestService as = new AutoSuggestService();
5453 phani.kuma 180
		as.addItemName("Nokia N8", "Nokia N");
181
		as.addItemName("Nokia C5-00", "Nokia C5");
182
		as.addItemName("Nokia C3-00", "Nokia C3");
183
		as.addItemName("Nokia X2-01", "Nokia X2");
184
		as.addItemName("Samsung Galaxy Pro", "Samsung Galaxy Pro");
185
		as.addItemName("Samsung Galaxy Pop", "Samsung Galaxy Pop");
186
		as.addItemName("Samsung Galaxy 551 I5510", "Samsung Galaxy 551");
3283 rajveer 187
		System.out.println(as.getMatchingQueries("noki", 10));
188
		System.out.println(as.getMatchingQueries("nok", 10));
5453 phani.kuma 189
		System.out.println(as.getMatchingQueries("Nokia", 10));
3283 rajveer 190
		System.out.println(as.getMatchingQueries("n8", 10));
191
		System.out.println(as.getMatchingQueries("po gala", 10));
192
	}
193
}
194
 
195
class Node
196
{
197
	char storedChar;
198
    Node left, center, right;
5453 phani.kuma 199
    Map<String, String> itemMap;
3283 rajveer 200
 
5453 phani.kuma 201
    public void addItem(String itemName, String itemDisplayName){
202
    	if(itemMap == null || itemMap.isEmpty()){
203
    		itemMap = new HashMap<String, String>();
3283 rajveer 204
    	}
5453 phani.kuma 205
    	itemMap.put(itemName, itemDisplayName);
3283 rajveer 206
    }
5453 phani.kuma 207
    public Node(char ch, String itemName, String itemDisplayName)
3283 rajveer 208
    {
209
        this.storedChar = ch;
210
        if(itemName != null){
5453 phani.kuma 211
        	itemMap = new HashMap<String, String>();
212
        	itemMap.put(itemName, itemDisplayName);
3283 rajveer 213
        }
214
    }
215
	@Override
216
	public String toString() {
217
		return "Node [storedChar=" + storedChar + ", left=" + left
5453 phani.kuma 218
				+ ", center=" + center + ", right=" + right + ", itemMap="
219
				+ itemMap + "]";
3283 rajveer 220
	}   
221
}