Subversion Repositories SmartDukaan

Rev

Rev 7975 | Rev 8093 | 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
		}
2657 rajveer 148
		for(ExpandedSlide expSlide : expSlides) {
8024 amit.gupta 149
			long slideDefinitionID = expSlide.getSlideDefinitionID();
150
 
151
			if(isCustomized && customScores.containsKey(slideDefinitionID)){
152
				slideScores.put(slideDefinitionID, customScores.get(slideDefinitionID));
153
				continue;
154
			}
2171 rajveer 155
			cmpJW.reset();
156
			cmpJW.initialize();
157
 
158
			cmpJW.setExpandedSlide(expSlide);
2657 rajveer 159
			cmpJW.setCategory(expEntity.getCategory());
2171 rajveer 160
 
161
			ExpandedCMPSlideRuleDefinition expCMPSlideRuleDef = 
162
				defs.getExpandedComparisonSlideRuleDefinition(
163
						slideDefinitionID);
164
 
165
			if(expCMPSlideRuleDef == null) {
166
				Utils.warning("Could not find comparison definition for " + 
167
						"slide definition ID " + slideDefinitionID);
168
				continue;
169
			}
170
 
171
			cmpJW.setExpandedCMPSlideRuleDefinition(expCMPSlideRuleDef);
172
 
173
			cmpJW.executeRule();
174
 
175
			double score = cmpJW.getScore();
176
			Utils.info("score=" + score);
177
 
178
			slideScores.put(new Long(slideDefinitionID), new Double(score));
179
		}
180
 
181
		return slideScores;
182
	}
183
 
184
	/**
185
	 * 
186
	 * @param slideScoresByEntity
187
	 * @return Map<Long, Double>
188
	 * @throws Exception 
189
	 */
190
	public Map<Long, Double> getFinalScores(
191
			Map<Long, Map<Long, Double>> slideScoresByEntity) throws Exception {
192
		Map<Long, Double> finalScoreByEntityID = new TreeMap<Long, Double>();
193
 
194
		// Considering default Weights per Slide bucket
195
		// Slide buckets > List(Very Important, Regular, Not-so-Important)
196
		// Default Weights > List(70%, 20%, 10%)
197
		DefinitionsContainer defs = 
198
			Catalog.getInstance().getDefinitionsContainer();
199
 
200
		for(Long entityID : slideScoresByEntity.keySet()) {
201
			Map<Long, Double> slideScores = slideScoresByEntity.get(entityID);
202
 
203
			Entity entity = CreationUtils.getEntity(entityID);
204
			long categoryID = entity.getCategoryID();
205
			if(categoryID == -1){
206
				continue;
207
			}
208
			if(categoryID>10010){
209
				continue;
210
			}
211
 
212
			List<CMPBucketDefinition> bucketDefs = 
213
				defs.getComparisonBucketDefinitions(categoryID);
214
 
215
			Map<String, Double> bucketScores = new HashMap<String, Double>();
216
			Map<String, Double> bucketSlideCounts = 
217
				new HashMap<String, Double>();
218
 
219
			for(Long slideDefinitionID : slideScores.keySet()) {
220
				Double slideScore = slideScores.get(slideDefinitionID);
221
 
222
				Slide slide = CreationUtils.getSlide(entityID, slideDefinitionID);
223
 
224
				String bucketName = null;
225
				if(CreationUtils.isBorrowedSlide(slide.getSlideDefinitionID(), entity.getCategoryID())) {
226
					long borrowedCategoryID = CreationUtils.getBorrowedCategoryID(slide.getSlideDefinitionID(), entity.getCategoryID());
227
 
228
					bucketName = defs.getComparisonBucketName(borrowedCategoryID, slideDefinitionID.longValue());
229
				}
230
				else {
231
					bucketName = defs.getComparisonBucketName(categoryID, 
232
						slideDefinitionID.longValue());
233
				}
234
 
235
				if(bucketName == null) {
236
					Utils.warning("Comparison bucket not defined for " +
237
							"Category ID=" + categoryID + 
238
							" Slide Definition ID=" + slideDefinitionID);
239
 
240
					continue;
241
				}
242
 
243
				if(!bucketScores.containsKey(bucketName)) {
244
					bucketScores.put(bucketName, new Double(0));
245
				}
246
 
247
				if(!bucketSlideCounts.containsKey(bucketName)) {
248
					bucketSlideCounts.put(bucketName, new Double(0));
249
				}
250
 
251
				Double bucketScore = bucketScores.get(bucketName);
252
				Double bucketSlideCount = bucketSlideCounts.get(bucketName);
253
 
254
 
255
				Double newBucketScore =  new Double(
256
						bucketScore.intValue() + slideScore.intValue());
257
 
258
				Double newBucketSlideCount =  new Double(
259
						bucketSlideCount.intValue() + 1);
260
 
261
				bucketScores.put(bucketName, newBucketScore);
262
				bucketSlideCounts.put(bucketName, newBucketSlideCount);
263
			}
264
 
265
			int finalEntityScore = 0;
266
 
267
			// Apply weights and sum up
268
			for(CMPBucketDefinition bucketDef : bucketDefs) {
269
				String bucketName = bucketDef.getName();
270
				Utils.info("bucketName=" + bucketName);
271
 
272
				if(bucketScores.get(bucketName)==null){
273
					continue;
274
				}
275
				double aggregateBucketScore = bucketScores.get(bucketName);
276
				Utils.info("aggregateBucketScore=" + aggregateBucketScore);
277
 
278
				double bucketSlideCount = bucketSlideCounts.get(bucketName);
279
				Utils.info("bucketSlideCount=" + bucketSlideCount);
280
 
281
				double defaultWeight = bucketDef.getDefaultWeight();
282
				Utils.info("defaultWeight=" + defaultWeight);
283
 
284
				float percentageBucketScore = 
285
					(((float)aggregateBucketScore/
286
							((float)bucketSlideCount * 10))) * 100;
287
 
288
				Utils.info("percentageBucketScore=" + percentageBucketScore);
289
 
290
				float weightedBucketScore = 
291
					(float)percentageBucketScore * ((float)defaultWeight / 100);
292
				Utils.info("weightedBucketScore=" + weightedBucketScore);
293
 
294
				finalEntityScore += Math.round(weightedBucketScore);
295
			}
296
			Utils.info("finalEntityScore=" + finalEntityScore);
297
 
298
			finalScoreByEntityID.put(entityID, new Double(finalEntityScore));
299
		}
300
 
301
		return finalScoreByEntityID;
302
	}
303
}