Subversion Repositories SmartDukaan

Rev

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