Subversion Repositories SmartDukaan

Rev

Rev 5084 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5084 Rev 5453
Line 2... Line 2...
2
 
2
 
3
 
3
 
4
import in.shop2020.serving.utils.Utils;
4
import in.shop2020.serving.utils.Utils;
5
import java.io.File;
5
import java.io.File;
6
import java.util.ArrayList;
6
import java.util.ArrayList;
-
 
7
import java.util.HashMap;
7
import java.util.HashSet;
8
import java.util.HashSet;
8
import java.util.List;
9
import java.util.List;
9
import java.util.Map;
10
import java.util.Map;
-
 
11
import java.util.Map.Entry;
10
import java.util.Set;
12
import java.util.Set;
11
 
13
 
12
import org.apache.commons.io.FileUtils;
14
import org.apache.commons.io.FileUtils;
13
import org.apache.log4j.Logger;
15
import org.apache.log4j.Logger;
14
 
16
 
Line 35... Line 37...
35
	}
37
	}
36
	
38
	
37
	private void initialize() {
39
	private void initialize() {
38
		log.info("Starting instance of Autosuggest service");
40
		log.info("Starting instance of Autosuggest service");
39
		try {
41
		try {
40
			Set<String> titleSet = new HashSet<String>();
-
 
41
			Gson g = new Gson();
42
			Gson g = new Gson();
42
			java.lang.reflect.Type typeOfT = new TypeToken<Map<Long, List<String>>>(){}.getType();
43
			java.lang.reflect.Type typeOfT = new TypeToken<Map<String, List<String>>>(){}.getType();
43
			Map<Long, List<String>> synonyms = g.fromJson(FileUtils.readFileToString(new File(Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "autosuggest.json")), typeOfT);
44
			Map<String, List<String>> synonyms = g.fromJson(FileUtils.readFileToString(new File(Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "autosuggest.json")), typeOfT);
44
			if(synonyms != null){
45
			if(synonyms != null){
45
				for(List<String> entry : synonyms.values()){
46
				for(Entry<String, List<String>> entry : synonyms.entrySet()){
46
					for(String title : entry){
47
					for(String title : entry.getValue()){
47
						titleSet.add(title);
48
						addItemName(title, entry.getKey());
48
					}
49
					}
49
				}
50
				}
50
				List<String> titlelist = new ArrayList<String>(titleSet);
-
 
51
				java.util.Collections.sort(titlelist);
-
 
52
				for(String title : titlelist){
-
 
53
					addItemName(title);
-
 
54
				}
-
 
55
			}
51
			}
56
		} catch (Exception e) {
52
		} catch (Exception e) {
57
			log.error("", e);
53
			log.error("", e);
58
		}
54
		}
59
		log.info("Done with starting Autosuggest service");
55
		log.info("Done with starting Autosuggest service");
Line 61... Line 57...
61
 
57
 
62
	public static AutoSuggestService getAutoSuggestServiceInstance(){
58
	public static AutoSuggestService getAutoSuggestServiceInstance(){
63
		return autoSuggestService;
59
		return autoSuggestService;
64
	}
60
	}
65
 
61
 
66
	private Node addChars(char[] s, int position, Node node, String string)
62
	private Node addChars(char[] s, int position, Node node, String string, String displaystring)
67
    {
63
    {
68
        if (node == null) { 
64
        if (node == null) { 
69
        	node = new Node(s[position], null); 
65
        	node = new Node(s[position], null, null); 
70
        }
66
        }
71
        if (s[position] < node.storedChar) {
67
        if (s[position] < node.storedChar) {
72
        	node.left = addChars(s, position, node.left, string); 
68
        	node.left = addChars(s, position, node.left, string, displaystring); 
73
        }
69
        }
74
        else if (s[position] > node.storedChar) {
70
        else if (s[position] > node.storedChar) {
75
        	node.right = addChars(s, position, node.right, string); 
71
        	node.right = addChars(s, position, node.right, string, displaystring); 
76
        }
72
        }
77
        else {
73
        else {
78
        	if (position + 1 == s.length) {
74
        	if (position + 1 == s.length) {
79
        		node.addItem(string);
75
        		node.addItem(string, displaystring);
80
        	}
76
        	}
81
        	else {
77
        	else {
82
        		node.center =  addChars(s, position + 1, node.center, string);
78
        		node.center =  addChars(s, position + 1, node.center, string, displaystring);
83
        	}
79
        	}
84
        }
80
        }
85
        return node;
81
        return node;
86
    }
82
    }
87
 
83
 
88
    public void addStringPart(String part, String string)
84
    public void addStringPart(String part, String string, String displaystring)
89
    {
85
    {
90
        if (part == null || part == "") return;
86
        if (part == null || part == "") return;
91
        root = addChars(part.toCharArray(), 0, root, string);
87
        root = addChars(part.toCharArray(), 0, root, string, displaystring);
92
    }
88
    }
93
 
89
 
94
    /**
90
    /**
95
     * Add String to the tree
91
     * Add String to the tree
96
     * @param string
92
     * @param string
97
     */
93
     */
98
    private void addItemName(String string) {
94
    private void addItemName(String string, String displayString) {
99
        if (string == null || string == "") return;
95
        if (string == null || string == "") return;
100
        String[] parts = string.trim().toLowerCase().split("\\s+");
96
        String[] parts = string.trim().toLowerCase().split("\\s+");
101
        for(String part : parts){
97
        for(String part : parts){
102
        	addStringPart(part, string);
98
        	addStringPart(part, string, displayString);
103
        }
99
        }
104
    }
100
    }
105
    
101
    
106
    /**
102
    /**
107
     * Get list of matching product names.
103
     * Get list of matching product names.
Line 115... Line 111...
115
        for(String part : parts){
111
        for(String part : parts){
116
        	Node node = getPartialMatchNode(part);
112
        	Node node = getPartialMatchNode(part);
117
        	Set<String> set = new HashSet<String>();
113
        	Set<String> set = new HashSet<String>();
118
        	getChildElements(node, set, part);
114
        	getChildElements(node, set, part);
119
        	if(totalSet.isEmpty()){
115
        	if(totalSet.isEmpty()){
120
        		totalSet.addAll(set);	
116
        		totalSet.addAll(set);
121
        	}else{
117
        	}else{
122
        		totalSet.retainAll(set);
118
        		totalSet.retainAll(set);
123
        	}
119
        	}
124
        }
120
        }
125
        List<String> items = new ArrayList<String>(totalSet);
121
        List<String> items = new ArrayList<String>(totalSet);
Line 156... Line 152...
156
    
152
    
157
    private void getChildElements(Node node, Set<String>  set, String str){
153
    private void getChildElements(Node node, Set<String>  set, String str){
158
    	if(node == null){
154
    	if(node == null){
159
    		return;
155
    		return;
160
    	}
156
    	}
161
    	if(node.itemSet != null && !node.itemSet.isEmpty()){
157
    	if(node.itemMap != null && !node.itemMap.isEmpty()){
-
 
158
    		for(Entry<String, String> entry : node.itemMap.entrySet()) {
162
    		for(String item_name : node.itemSet) {
159
    			String item_name = entry.getKey();
-
 
160
    			String item_displayname = entry.getValue();
163
    			String ignore_item_name = item_name.toLowerCase();
161
    			String ignore_item_name = item_name.toLowerCase();
164
    			if(ignore_item_name.indexOf(str) != -1){
162
    			if(ignore_item_name.indexOf(str) != -1){
165
    				set.add(item_name);
163
    				set.add(item_displayname);
166
    			}
164
    			}
167
    		}
165
    		}
168
    	}
166
    	}
169
    	getChildElements(node.center, set, str);	
167
    	getChildElements(node.center, set, str);	
170
    	getChildElements(node.left, set, str);
168
    	getChildElements(node.left, set, str);
Line 177... Line 175...
177
		return "AutoSuggest [root=" + root + "]";
175
		return "AutoSuggest [root=" + root + "]";
178
	}
176
	}
179
 
177
 
180
	public static void main(String[] args) throws Exception {
178
	public static void main(String[] args) throws Exception {
181
		AutoSuggestService as = new AutoSuggestService();
179
		AutoSuggestService as = new AutoSuggestService();
182
		as.addItemName("Nokia N8");
180
		as.addItemName("Nokia N8", "Nokia N");
183
		as.addItemName("Nokia C5-00");
181
		as.addItemName("Nokia C5-00", "Nokia C5");
184
		as.addItemName("Nokia C3-00");
182
		as.addItemName("Nokia C3-00", "Nokia C3");
185
		as.addItemName("Nokia X2-01");
183
		as.addItemName("Nokia X2-01", "Nokia X2");
186
		as.addItemName("Samsung Galaxy Pro");
184
		as.addItemName("Samsung Galaxy Pro", "Samsung Galaxy Pro");
187
		as.addItemName("Samsung Galaxy Pop");
185
		as.addItemName("Samsung Galaxy Pop", "Samsung Galaxy Pop");
188
		as.addItemName("Samsung Galaxy 551 I5510");
186
		as.addItemName("Samsung Galaxy 551 I5510", "Samsung Galaxy 551");
189
		System.out.println(as.getMatchingQueries("noki", 10));
187
		System.out.println(as.getMatchingQueries("noki", 10));
190
		System.out.println(as.getMatchingQueries("nok", 10));
188
		System.out.println(as.getMatchingQueries("nok", 10));
-
 
189
		System.out.println(as.getMatchingQueries("Nokia", 10));
191
		System.out.println(as.getMatchingQueries("n8", 10));
190
		System.out.println(as.getMatchingQueries("n8", 10));
192
		System.out.println(as.getMatchingQueries("po gala", 10));
191
		System.out.println(as.getMatchingQueries("po gala", 10));
193
	}
192
	}
194
}
193
}
195
 
194
 
196
class Node
195
class Node
197
{
196
{
198
	char storedChar;
197
	char storedChar;
199
    Node left, center, right;
198
    Node left, center, right;
200
    Set<String> itemSet;
199
    Map<String, String> itemMap;
201
 
200
 
202
    public void addItem(String itemName){
201
    public void addItem(String itemName, String itemDisplayName){
203
    	if(itemSet == null || itemSet.isEmpty()){
202
    	if(itemMap == null || itemMap.isEmpty()){
204
    		itemSet = new HashSet<String>();
203
    		itemMap = new HashMap<String, String>();
205
    	}
204
    	}
206
    	itemSet.add(itemName);
205
    	itemMap.put(itemName, itemDisplayName);
207
    }
206
    }
208
    public Node(char ch, String itemName)
207
    public Node(char ch, String itemName, String itemDisplayName)
209
    {
208
    {
210
        this.storedChar = ch;
209
        this.storedChar = ch;
211
        if(itemName != null){
210
        if(itemName != null){
212
        	itemSet = new HashSet<String>();
211
        	itemMap = new HashMap<String, String>();
213
        	itemSet.add(itemName);
212
        	itemMap.put(itemName, itemDisplayName);
214
        }
213
        }
215
    }
214
    }
216
	@Override
215
	@Override
217
	public String toString() {
216
	public String toString() {
218
		return "Node [storedChar=" + storedChar + ", left=" + left
217
		return "Node [storedChar=" + storedChar + ", left=" + left
219
				+ ", center=" + center + ", right=" + right + ", itemSet="
218
				+ ", center=" + center + ", right=" + right + ", itemMap="
220
				+ itemSet + "]";
219
				+ itemMap + "]";
221
	}   
220
	}   
222
}
221
}
223
222