Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2171 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.util;
5
 
6
import in.shop2020.metamodel.core.Entity;
7
import in.shop2020.metamodel.definitions.CMPBucketDefinition;
8
import in.shop2020.metamodel.definitions.Catalog;
4802 amit.gupta 9
import in.shop2020.metamodel.definitions.Category;
2171 rajveer 10
import in.shop2020.metamodel.definitions.DefinitionsContainer;
11
import in.shop2020.metamodel.util.CreationUtils;
12
import in.shop2020.metamodel.util.ExpandedCMPSlideRuleDefinition;
2657 rajveer 13
import in.shop2020.metamodel.util.ExpandedEntity;
2171 rajveer 14
import in.shop2020.metamodel.util.ExpandedSlide;
15
 
16
import java.util.HashMap;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.TreeMap;
20
 
21
/**
22
 * @author naveen
23
 *
24
 */
25
public class NewCMP {
26
 
27
	/**
28
	 * 
29
	 */
30
	//private long categoryID;
31
 
2367 rajveer 32
	List<Entity> entities;
2171 rajveer 33
 
34
 
35
	/**
36
	 * @param args
37
	 * @throws Exception 
38
	 */
39
	public static void main(String[] args) throws Exception {
40
		String usage = "Usage: CMP {Category ID}";
41
 
42
		if(args.length > 1) {
43
			System.out.println(usage);
44
			System.exit(-1);
45
		}
46
	}
47
 
48
	/**
49
	 * 
50
	 * @param entityIdItemMap
51
	 */
2367 rajveer 52
	public NewCMP(List<Entity> entities) {
53
	    this.entities = entities;
2171 rajveer 54
	}
55
 
56
	/**
57
	 * 
58
	 * @return Map<Long, Map<Long, Double>>
59
	 * @throws Exception 
60
	 */
61
	public Map<Long, Map<Long, Double>> getSlideScores() throws Exception {
62
		Map<Long, Map<Long, Double>> slideScoresByEntityID = 
63
			new TreeMap<Long, Map<Long, Double>>();
64
 
2367 rajveer 65
		for(Entity entity : entities) {
3717 mandeep.dh 66
			// Skip categories with value as -1 or categories except mobile phones and tablets
4802 amit.gupta 67
			long categoryId = entity.getCategoryID();
68
			if(categoryId == -1){
69
				continue;
2171 rajveer 70
			}
5930 amit.gupta 71
			Category category = Catalog.getInstance().getDefinitionsContainer().getCategory(categoryId);
2171 rajveer 72
 
5930 amit.gupta 73
            if(category.isComparable()) {
4802 amit.gupta 74
            	ExpandedEntity expEntity = new ExpandedEntity(entity);
75
 
8024 amit.gupta 76
            	Map<Long, Double> slideScores = this.getSlideScores(expEntity, true);
4802 amit.gupta 77
            	slideScoresByEntityID.put(entity.getID(), slideScores);
78
			}
2171 rajveer 79
		}
80
 
81
		return slideScoresByEntityID;
82
	}
7975 amit.gupta 83
 
84
	public Map<Long, Map<Long, Double>> getSlideScores(int dummy) throws Exception {
85
		Map<Long, Map<Long, Double>> slideScoresByEntityID = 
86
			new TreeMap<Long, Map<Long, Double>>();
87
 
88
		for(Entity entity : entities) {
89
			// Skip categories with value as -1 or categories except mobile phones and tablets
90
			long categoryId = entity.getCategoryID();
91
			if(categoryId == -1){
92
				continue;
93
			}
8749 amit.gupta 94
			ExpandedEntity expEntity = new ExpandedEntity(entity);
7975 amit.gupta 95
 
8749 amit.gupta 96
			Map<Long, Double> slideScores = this.getSlideScores(expEntity, false);
97
			slideScoresByEntityID.put(entity.getID(), slideScores);
7975 amit.gupta 98
		}
99
 
100
		return slideScoresByEntityID;
101
	}
2171 rajveer 102
 
103
	/**
104
	 * 
105
	 * @param entity
106
	 * @return Map<Long, Double>
107
	 * @throws Exception 
108
	 */
8024 amit.gupta 109
	private Map<Long, Double> getSlideScores(ExpandedEntity expEntity, boolean isCustomized) throws Exception {
2171 rajveer 110
		Map<Long, Double> slideScores = new HashMap<Long, Double>();
111
 
112
		DefinitionsContainer defs = 
113
			Catalog.getInstance().getDefinitionsContainer();
114
 
115
		// Python wrapper for executing rule in python script
116
		CMPJythonWrapper cmpJW = new CMPJythonWrapper();
8024 amit.gupta 117
		Map<Long, Double> customScores = CreationUtils.getCustomSlideComparisonScores(expEntity.getID());
2171 rajveer 118
 
119
		// Scores are required for parent slides only
2657 rajveer 120
		List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
121
		if(expSlides==null){
2171 rajveer 122
			return slideScores;
123
		}
8093 amit.gupta 124
		if (customScores==null) {
125
			isCustomized = false;
126
		}
2657 rajveer 127
		for(ExpandedSlide expSlide : expSlides) {
8024 amit.gupta 128
			long slideDefinitionID = expSlide.getSlideDefinitionID();
129
 
130
			if(isCustomized && customScores.containsKey(slideDefinitionID)){
131
				slideScores.put(slideDefinitionID, customScores.get(slideDefinitionID));
132
				continue;
133
			}
2171 rajveer 134
			cmpJW.reset();
135
			cmpJW.initialize();
136
 
137
			cmpJW.setExpandedSlide(expSlide);
2657 rajveer 138
			cmpJW.setCategory(expEntity.getCategory());
2171 rajveer 139
 
140
			ExpandedCMPSlideRuleDefinition expCMPSlideRuleDef = 
141
				defs.getExpandedComparisonSlideRuleDefinition(
142
						slideDefinitionID);
143
 
144
			if(expCMPSlideRuleDef == null) {
145
				Utils.warning("Could not find comparison definition for " + 
146
						"slide definition ID " + slideDefinitionID);
147
				continue;
148
			}
149
 
150
			cmpJW.setExpandedCMPSlideRuleDefinition(expCMPSlideRuleDef);
151
 
152
			cmpJW.executeRule();
153
 
154
			double score = cmpJW.getScore();
155
			Utils.info("score=" + score);
156
 
157
			slideScores.put(new Long(slideDefinitionID), new Double(score));
158
		}
159
 
160
		return slideScores;
161
	}
162
 
163
	/**
164
	 * 
165
	 * @param slideScoresByEntity
166
	 * @return Map<Long, Double>
167
	 * @throws Exception 
168
	 */
169
	public Map<Long, Double> getFinalScores(
170
			Map<Long, Map<Long, Double>> slideScoresByEntity) throws Exception {
171
		Map<Long, Double> finalScoreByEntityID = new TreeMap<Long, Double>();
172
 
173
		// Considering default Weights per Slide bucket
174
		// Slide buckets > List(Very Important, Regular, Not-so-Important)
175
		// Default Weights > List(70%, 20%, 10%)
176
		DefinitionsContainer defs = 
177
			Catalog.getInstance().getDefinitionsContainer();
178
 
179
		for(Long entityID : slideScoresByEntity.keySet()) {
180
			Map<Long, Double> slideScores = slideScoresByEntity.get(entityID);
181
 
182
			Entity entity = CreationUtils.getEntity(entityID);
183
			long categoryID = entity.getCategoryID();
184
			if(categoryID == -1){
185
				continue;
186
			}
187
			if(categoryID>10010){
188
				continue;
189
			}
190
 
191
			List<CMPBucketDefinition> bucketDefs = 
192
				defs.getComparisonBucketDefinitions(categoryID);
193
 
194
			Map<String, Double> bucketScores = new HashMap<String, Double>();
195
			Map<String, Double> bucketSlideCounts = 
196
				new HashMap<String, Double>();
197
 
198
			for(Long slideDefinitionID : slideScores.keySet()) {
199
				Double slideScore = slideScores.get(slideDefinitionID);
200
 
201
				String bucketName = null;
202
 
8749 amit.gupta 203
				bucketName = defs.getComparisonBucketName(categoryID, slideDefinitionID.longValue());
204
 
2171 rajveer 205
				if(bucketName == null) {
206
					Utils.warning("Comparison bucket not defined for " +
207
							"Category ID=" + categoryID + 
208
							" Slide Definition ID=" + slideDefinitionID);
209
 
210
					continue;
211
				}
212
 
213
				if(!bucketScores.containsKey(bucketName)) {
214
					bucketScores.put(bucketName, new Double(0));
215
				}
216
 
217
				if(!bucketSlideCounts.containsKey(bucketName)) {
218
					bucketSlideCounts.put(bucketName, new Double(0));
219
				}
220
 
221
				Double bucketScore = bucketScores.get(bucketName);
222
				Double bucketSlideCount = bucketSlideCounts.get(bucketName);
223
 
224
 
225
				Double newBucketScore =  new Double(
226
						bucketScore.intValue() + slideScore.intValue());
227
 
228
				Double newBucketSlideCount =  new Double(
229
						bucketSlideCount.intValue() + 1);
230
 
231
				bucketScores.put(bucketName, newBucketScore);
232
				bucketSlideCounts.put(bucketName, newBucketSlideCount);
233
			}
234
 
235
			int finalEntityScore = 0;
236
 
237
			// Apply weights and sum up
238
			for(CMPBucketDefinition bucketDef : bucketDefs) {
239
				String bucketName = bucketDef.getName();
240
				Utils.info("bucketName=" + bucketName);
241
 
242
				if(bucketScores.get(bucketName)==null){
243
					continue;
244
				}
245
				double aggregateBucketScore = bucketScores.get(bucketName);
246
				Utils.info("aggregateBucketScore=" + aggregateBucketScore);
247
 
248
				double bucketSlideCount = bucketSlideCounts.get(bucketName);
249
				Utils.info("bucketSlideCount=" + bucketSlideCount);
250
 
251
				double defaultWeight = bucketDef.getDefaultWeight();
252
				Utils.info("defaultWeight=" + defaultWeight);
253
 
254
				float percentageBucketScore = 
255
					(((float)aggregateBucketScore/
256
							((float)bucketSlideCount * 10))) * 100;
257
 
258
				Utils.info("percentageBucketScore=" + percentageBucketScore);
259
 
260
				float weightedBucketScore = 
261
					(float)percentageBucketScore * ((float)defaultWeight / 100);
262
				Utils.info("weightedBucketScore=" + weightedBucketScore);
263
 
264
				finalEntityScore += Math.round(weightedBucketScore);
265
			}
266
			Utils.info("finalEntityScore=" + finalEntityScore);
267
 
268
			finalScoreByEntityID.put(entityID, new Double(finalEntityScore));
269
		}
270
 
271
		return finalScoreByEntityID;
272
	}
273
}