Subversion Repositories SmartDukaan

Rev

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