Subversion Repositories SmartDukaan

Rev

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