Subversion Repositories SmartDukaan

Rev

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