1  /** @author Amit Kumar
  2  * @date 03/20/2008
  3  * @since 1.2M1
  4  * @extends Workbench.component.Component
  5  * @description This component displays workset comparison
  6  * @class Monk.component.WorksetComparisonComponent
  7  * @extends Workbench.component.Component
  8  */
  9 
 10  
 11 String.prototype.ellipse = function(maxLength){
 12     if(this.length > maxLength){
 13         return this.substr(0, maxLength-3) + '...';
 14     }
 15     return this;
 16 };
 17 Monk.component.WorksetComparisonComponent = function(args) {
 18   this.searchParams = {'corpusCriterion':'*','limit':1200,'start':0};
 19   this.finishedDocumentAnalysis=false;
 20    var worksetReader=new Ext.data.XmlReader({record : 'workset', id : '@id'}
 21                 , new Ext.data.Record.create([
 22                 {name : 'value', mapping : '@id'},
 23                 {name: 'label', mapping:'label', convert: function(value){return value.ellipse(10)}},
 24                 {name: 'type',mapping: 'label', convert: function(value){return 'workset'}},
 25                 {name:'count', mapping:'workList', type: 'int', convert: function(value){
 26                     if(value==null){
 27                         return 0;
 28                     }
 29                     return value.split(',').length;
 30                 }}
 31                 ]));
 32                 
 33      this.filterLemmaStore=new Ext.data.SimpleStore({
 34      fields: ['id','label','label']
 35      , data : [['2','2',"2"],
 36      ["3","3","3"],["4","4","4"],
 37      ["5","5","5"],["6","6","6"],
 38      ["7","7","7"],["8","8","8"]
 39      ]
 40     });
 41   
 42      this.featureLenStore=new Ext.data.SimpleStore({
 43      fields: ['id','label','label']
 44      , data : [['2','2',"2"],
 45      ["3","3","3"],["4","4","4"],
 46      ["5","5","5"],["6","6","6"],
 47      ["7","7","7"],["8","8","8"]
 48      ]
 49     });
 50     
 51     this.featureFreqStore=new Ext.data.SimpleStore({
 52      fields: ['id','label','label']
 53      , data : [['2','2',"2"],
 54      ["3","3","3"],["4","4","4"],
 55      ["5","5","5"],["6","6","6"],
 56      ["7","7","7"],["8","8","8"]
 57      ]
 58     });
 59     
 60      this.featureFreqDocumentStore=new Ext.data.SimpleStore({
 61      fields: ['id','label','label']
 62      , data : [['2','2',"2"],
 63      ["5","5","5"],["10","10","10"],
 64      ["15","15","15"],["20","20","20"],
 65      ["25","25","25"],["50","50","50"],
 66      ["100","100","100"],
 67      ["200","200","200"]
 68      ]
 69     });
 70      
 71                 
 72 
 73   
 74   
 75     this.featureCountStore=new Ext.data.SimpleStore({
 76          fields: ['value','label']
 77          , data : [['10','10'],
 78          ['20','20'],
 79          ["30","30"],
 80          ["50","50"],
 81          ["100","100","100"],
 82          ["200","200","200"],
 83          ["500","500","500"]
 84          ]
 85     });
 86     
 87     
 88     this.methodStore=new Ext.data.SimpleStore({
 89          fields: ['value','label']
 90          , data : [
 91          ['Dunnings','Dunnings: First workset as ref set'],
 92          ['Dunnings:swap','Dunnings: Second workset as ref set'],
 93          ['FrequencyCount','Frequency Comparison'],
 94          ['SimilarFeatures','IDF: First workset as training set'],
 95          ['SimilarFeatures:swap','IDF: Second workset as training set']
 96          ]
 97     });
 98   
 99 
100   this.featureTypeStore=new Ext.data.SimpleStore({
101           fields: ['value','label'],
102           data : [
103                   ['spelling','Spelling'],
104                   ['lemma','Lemma']
105                  ]
106         });
107 
108    var WordClassRecord= new Ext.data.Record.create([
109                 {name : 'label', mapping : '@id'},
110                 {name: 'value', mapping: '@id'}
111                 ])
112    this.wordclassStore = new Ext.data.Store({
113             url: Monk.data.PROXY_URL + "get/AnalyticsManager.getMajorWordClasses",
114             reader : new Ext.data.XmlReader({record : 'class'}
115                 ,WordClassRecord),
116             sortInfo: {field:'label', direction:"ASC"},
117            autoLoad: true
118      });
119 
120     // add the all Word Class
121     this.wordclassStore.on('load',function(){
122      var newRecord = new WordClassRecord({"label": 'All','value':'*'});
123      this.wordclassStore.addSorted(newRecord);
124     },this);
125 
126     this.groupRecord = new Ext.data.Record.create([
127         {name : 'type', mapping : 'type'},
128         {name : 'value', mapping : 'value'},
129         {name : 'count', mapping : 'count'},
130         {name : 'label', mapping : 'type'}
131     ]);
132 
133     this.groupStore = new Ext.data.Store({
134             url: Monk.data.PROXY_URL + 'get/SearchManager.getResultGroupsWithFeatures',
135             baseParams: this.searchParams,
136             reader: new Ext.data.XmlReader({record : 'key'}, this.groupRecord),
137             remoteSort: false,
138             //sortInfo: {field: 'type', direction: 'DESC'},
139             autoLoad: false
140      });
141     
142     this.groupStore.on('load', function(store, records, options){
143         store.each(function(record, options){
144             var type = record.get('type');
145             var value = record.get('value');
146             var count = record.get('count');
147             if (type == 'circulation') {
148                 var start = value;
149                 var end = parseInt(value)+9;
150                 value = start + '-'+ end;
151             }
152             var label =  type +': '+value +' ('+count+' works)';
153             record.set('value', type+':'+value);
154             record.set('label', label);
155         },this);
156         Ext.getCmp('compareOption1Id').reset();
157         Ext.getCmp('compareOption2Id').reset();
158         this.worksetStore.load();
159     }, this);
160     
161     this.worksetStore = new Ext.data.Store({
162         reader: worksetReader,
163         url: Monk.data.PROXY_URL + 'get/ProjectManager.getWorksets',
164         baseParams: {projectId: Monk.component.dataManager.getProjectId()},
165         autoLoad: false,
166         listeners: {
167             load: {
168                 fn: function(store, records, options) {
169                     store.each(function(record, options){
170                         var type = record.get('type');
171                         var value = record.get('value');
172                         var count = record.get('count');
173                         var name = record.get('label'); 
174                         var label =  type +': '+name +' ('+count+' works)';
175                         record.set('value', 'worksetIdCriterion:'+value);
176                         record.set('label', label);
177                     },this);
178                     
179                     this.groupStore.on('add', function(store, records, index){
180                         store.on('datachanged', function(store){
181                             var worksetId = Monk.component.dataManager.getWorksetId();
182                             if (worksetId != null) {
183                                 var combo = Ext.getCmp('compareOption1Id');
184                                 combo.setValue('worksetIdCriterion:'+worksetId);
185                             }
186                         }, this, {single: true});
187                         
188                         store.sort('value', 'ASC');
189                         store.sort('type', 'DESC');
190                     }, this, {single: true});
191                     this.groupStore.add(records);
192                 },
193                 scope: this
194             },
195             loadexception: {
196                 fn: function() {
197                     this.groupStore.load();
198                 },
199                 scope: this
200             }
201         }
202     });
203 
204   Monk.component.WorksetComparisonComponent.superclass.constructor.call(this, args);
205 }
206 
207 
208 
209 Workbench.extend(Monk.component.WorksetComparisonComponent, Workbench.component.Component, {
210 label : "Workset Comparison",
211 description : "This component is for workset comparison",
212 "window" : this.window,
213 beforeExit : function() {
214      
215 },
216 
217 isValid: function(){
218 	if(this.finishedDocumentAnalysis){
219 		return true;
220 	}
221 	return 'Document analysis either did not complete or aborted.';
222 },
223 chooseMethod :function(){
224 	 
225 	 var methodVal=Ext.getCmp("methodId").getValue();
226 	 
227 	if (methodVal == "SimilarFeatures" || methodVal=="SimilarFeatures:swap") {
228 	       Ext.getCmp('minTermFreqId').enable();
229            Ext.getCmp('minDocFreqId').enable();
230            Ext.getCmp('minWordLenId').enable();
231     }else{
232     	   Ext.getCmp('minTermFreqId').disable();
233            Ext.getCmp('minDocFreqId').disable();
234            Ext.getCmp('minWordLenId').disable();
235     }
236    var cutoffDom= Ext.getCmp("cutoffId").el.dom.parentNode.parentNode.parentNode;
237    var children=cutoffDom.childNodes;
238    
239    
240    for(var i=0; i< children.length; i++){
241    	if(children[i].getAttribute("for")=="cutoffId" && children[i].className=="x-form-item-label"){
242    	    if(methodVal =="Dunnings" || methodVal =="Dunnings:swap"){	
243    		   children[i].innerHTML="Minimum Frequency:";
244    	    }else{
245    	    	children[i].innerHTML = "Number of Features:";
246    	    }
247    	
248    	}
249    }
250    
251    
252     
253 },
254 
255 sendCompareNotification: function(form){
256 	
257 	Ext.apply(this.searchParams, form.getValues());
258 	this.finishedDocumentAnalysis=false;
259     this.searchParams.topNCount = this.searchParams.cutoff
260     if (this.searchParams.method == "Dunnings"){
261     	this.searchParams.firstComparisonCriterionAsReference = true;
262     } else if(this.searchParams.method == "Dunnings:swap"){
263         this.searchParams.firstComparisonCriterionAsReference = false;
264     } else if(this.searchParams.method == "SimilarFeatures" || this.searchParams.method == "SimilarFeatures:swap"){
265     	this.searchParams.format="json";
266     	if (this.searchParams.method == "SimilarFeatures"){
267             this.searchParams.firstComparisonCriterionAsReference = true;
268         } else if(this.searchParams.method == "SimilarFeatures:swap"){
269             this.searchParams.firstComparisonCriterionAsReference = false;
270         }
271         
272     }
273    
274     Workbench.component.manager.notify(new Monk.event.workbench.WorksetFeatureComparison({
275        label: 'workset feature comparison: '
276     }), this.searchParams);
277    
278     // reset the method so that workset document comparison can be called.
279      if (this.searchParams.method == "Dunnings"){
280         this.searchParams.firstComparisonCriterionAsReference = true;
281         this.searchParams.method ="SimilarFeatures"
282     } else if(this.searchParams.method == "Dunnings:swap"){
283         this.searchParams.firstComparisonCriterionAsReference = false;
284         this.searchParams.method ="SimilarFeatures:swap"
285      } else if(this.searchParams.method == "SimilarFeatures" || 
286        this.searchParams.method == "SimilarFeatures:swap"){
287        	
288         this.searchParams.format="json";
289         if (this.searchParams.method == "SimilarFeatures"){
290             this.searchParams.firstComparisonCriterionAsReference = true;
291         } else if(this.searchParams.method == "SimilarFeatures:swap"){
292             this.searchParams.firstComparisonCriterionAsReference = false;
293         }
294         
295     } else{
296     	this.searchParams.method ="SimilarFeatures"
297     	 this.searchParams.format="json"
298     }
299     
300     Workbench.component.manager.notify(new Monk.event.workbench.WorksetDocumentComparison({
301        label: 'workset document comparison: '
302     }), this.searchParams);
303     
304 },
305 
306 
307 handle : function(monkEvent, data) {
308     if(monkEvent.instanceOf(Monk.event.workbench.AdvancedSearchQuery)){
309         this.searchParams = data[0];
310         this.searchParams.limit = 1200;
311         this.searchParams.start = 0;
312         this.groupStore.baseParams = this.searchParams;
313         this.groupStore.load();
314     }else if(monkEvent.instanceOf(Monk.event.chunk.ChunksSimilarRetrieved)){
315     	Workbench.console.info("Got similarity results...");
316     	this.finishedDocumentAnalysis=true;
317     }
318 },
319 init: function(){
320  this.groupStore.reload();
321  this.finishedDocumentAnalysis=false;	
322  var minWordLen = {
323      id: 'minWordLenId',
324      name: 'minWordLen',
325      hiddenName: 'minWordLen',
326      labelAlign : 'right',
327      fieldLabel : 'Min. Character length',
328      blankText : 'Minimum Feature Length',
329      emptyText: 'Minimum Feature Length',
330      xtype: 'combo',
331      valueField: 'id',
332      displayField : 'label',
333      disabled: false,
334      editable: true,
335      value: 5,
336      triggerAction: 'all',
337      store : this.featureLenStore,
338      mode : 'local',
339      allowBlank: false,
340      regex: /^\d+$/,
341      width: 120,
342      regexText: 'Please specify a numeric value.'
343  };
344 
345  var minTermFreq = {
346      id: 'minTermFreqId',
347      name: 'minTermFreq',
348      hiddenName: 'minTermFreq',
349      labelAlign : 'right',
350      fieldLabel : 'At least how many times should the feature occur in each training document',
351      blankText : 'Feature Frequency',
352      emptyText: 'Feature Frequency',
353      xtype: 'combo',
354      valueField: 'id',
355      displayField : 'label',
356      disabled: false,
357      editable: true,
358      value: 3,
359      triggerAction: 'all',
360      store : this.featureFreqStore,
361      mode : 'local',
362      allowBlank: false,
363      regex: /^\d+$/,
364      width: 120,
365      regexText: 'Please specify a numeric value.'
366    };
367 
368 
369  var minDocFreq = {
370      id: 'minDocFreqId',
371      name: 'minDocFreq',
372      hiddenName: 'minDocFreq',
373      labelAlign : 'right',
374      fieldLabel : 'Feature should occur in at least this many training documents',
375      blankText : 'Feature Document Frequency',
376      emptyText: 'Feature Document Frequency',
377      xtype: 'combo',
378      valueField: 'id',
379      displayField : 'label',
380      disabled: false,
381      editable: true,
382      value: 2,
383      triggerAction: 'all',
384      store : this.featureFreqDocumentStore,
385      mode : 'local',
386      allowBlank: false,
387      regex: /^\d+$/,
388      width: 120,
389      regexText: 'Please specify a numeric value.'
390    };
391 	
392 	
393 	
394  var group1 = {
395         id: 'compareOption1Id',
396         name: 'compareOption1',
397         hiddenName: 'compareOption1',
398         fieldLabel: '<span style="background-color:#9C9CDB">First Workset</span>',
399         tooltip: {title:'First Workset',text:'Select First Workset'},
400         triggerAction: 'all',
401         xtype: 'combo',
402         emptyText: 'Workset 1',
403         valueField : 'value',
404         displayField : 'label',
405         allowBlank : false,
406         width: 220,
407         mode: 'local',
408         style:'background-color:#9C9CDB',
409         store: this.groupStore
410     };
411         
412         
413  var group2= {
414         id: 'compareOption2Id',
415         name: 'compareOption2',
416         hiddenName: 'compareOption2',
417         fieldLabel: '<span style="background-color:#8BA353">Second Workset</span>',
418         tooltip: {title:'Second Workset',text:'Select Second Workset'},
419         triggerAction: 'all',
420         xtype: 'combo',
421         emptyText: 'Workset 2',
422         valueField : 'value',
423         displayField : 'label',
424         allowBlank : false,
425         width: 220,
426         mode: 'local',
427         style:'background-color:#8BA353',
428         store: this.groupStore
429     };
430     
431  var featureCount= {
432         xtype: 'combo',
433         fieldLabel: 'Number of Features',
434         id: 'cutoffId',
435         name: 'cutoff',
436         hiddenName: 'cutoff',
437         tooltip: {title:'Select Count',text:'Select the number of feature Spelling/Lemma'},
438         triggerAction: 'all',
439         emptyText: 'Feature Count',
440         valueField: 'value',
441         displayField: 'label',
442         mode: 'local',
443         allowBlank: false,
444         width: 120,
445         store: this.featureCountStore
446     };   
447     
448  var analysisMethod={
449         xtype: 'combo',
450         fieldLabel: 'Analysis Methods',
451         id: 'methodId',
452         name: 'method',
453         hiddenName: 'method',
454         tooltip: {title:'Select Method',text:'Select the Method'},
455         triggerAction: 'all',
456         emptyText: 'Method',
457         valueField: 'value',
458         displayField: 'label',
459         mode: 'local',
460         allowBlank: false,
461         width: 200,
462         store: this.methodStore,
463         listeners : {
464             select : {fn:  this.chooseMethod}
465         }
466     };
467 
468 
469 
470  var featureType= {
471         xtype: 'combo',
472         fieldLabel: 'Feature',
473         id: 'featureId',
474         name: 'feature',
475         hiddenName: 'feature',
476         tooltip: {title:'Select feature',text:'Select the feature Spelling/Lemma'},
477         triggerAction: 'all',
478         emptyText: 'Feature',
479         valueField: 'value',
480         displayField: 'label',
481         mode: 'local',
482         allowBlank: false,
483         width: 120,
484         store: this.featureTypeStore
485     };
486     
487  var majorWordClasses= {
488         xtype: 'combo',
489         fieldLabel: 'Feature  Class',
490         tooltip: {title:'Major Word Classes',text:'Select Major Word Class.'},
491         id: 'wordClassId',
492         name: 'wordClass',
493         hiddenName: 'wordClass',
494         triggerAction: 'all',
495         emptyText: 'Word Class',
496         valueField: 'value',
497         displayField: 'label',
498         mode: 'local',
499         allowBlank : false,
500         width: 120,
501         store: this.wordclassStore
502     };
503         
504 	
505  this.viewport = new Ext.Viewport({
506         id: 'viewport',
507         layout: 'fit',
508         renderTo: 'container',
509         items: {
510             border: false,
511             items: {
512                 xtype: 'form',
513                 id: 'featureFormPanel',
514                 bodyStyle:'padding: 10px',
515                 labelAlign: 'right',
516                 labelWidth: 125,
517                 autoScroll: true,
518                 border: false,
519                 items: [
520                     group1,
521                     group2,
522                     analysisMethod,
523                     featureType,
524                     featureCount,
525                     majorWordClasses,
526                     minTermFreq,
527                     minDocFreq,
528                     minWordLen
529                 ],
530                  buttons: [{
531                     id: 'gobutton'
532                     ,xtype: 'button'
533                     ,fieldLabel: 'Click to Compare.'
534                     ,text: 'Compare'
535                     ,tooltip: 'Compare'
536                     ,listeners :{
537                         click :{fn : function(){
538                             var form = Ext.getCmp('featureFormPanel').getForm();
539                             if (form.isValid()) {
540                                 this.sendCompareNotification(form);
541                             } else {
542                                 Monk.component.messenger.show({
543                                     title: 'Workset Comparison',
544                                     msg: 'The form is not valid.  Please correct the highlighted field(s) and try again.',
545                                     width: 240,
546                                     modal: true
547                                });
548                             }
549                         }, scope: this}
550                     }
551                 }]
552             }
553         }
554 	});
555 	
556   Ext.getCmp("minTermFreqId").disable();
557   Ext.getCmp("minDocFreqId").disable();
558   Ext.getCmp("minWordLenId").disable();
559 }
560 
561 });
562 
563