Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package timeflow.data.analysis;
 
import timeflow.util.*;
import timeflow.data.time.*;
import timeflow.data.analysis.DBAnalysis.InterestLevel;
import timeflow.data.db.*;
import java.util.*;
 
public class FrequencyAnalysis implements FieldAnalysis {
   String[] description;
   
   @Override
   public String getName() {
       return "Frequency Of Values";
   }
 
   @Override
   public String[] getResultDescription() {
       return description;
   }
 
   @Override
   public InterestLevel perform(ActList acts, Field field) {
       Bag<Object> bag=new Bag<Object>();
       if (field.getType()==String[].class)
       {
           for (Act a: acts)
           {
               String[] tags=a.getTextList(field);
               if (tags!=null)
                   for (String tag:tags)
                       bag.add(tag);
           }
       }
       else
           for (Act a: acts)
               bag.add(a.get(field));
       
       int numItems=acts.size();
       int numDistinctVals=bag.size();
       int numNullVals=bag.num(null)+bag.num("");
       
       if (numItems==numDistinctVals)
           description=new String[] {"All values are defined and unique."};
       else if (numItems==numDistinctVals+numNullVals-1)
           description=new String[] {"All defined values are unique."};
       else if (numDistinctVals==1)
           description=new String[] {"This field is always equal to "+string(bag.list().get(0))};
       else if (numDistinctVals<4)
       {
           List<Object> all=bag.list();
           description=new String[] {"This field takes only "+all.size()+" values.",
               "which are: "+all};
       }
       else
       {
           List<Object> all=bag.list();
           description=new String[] {"There are "+numDistinctVals+" distinct values.",
                   "Most common: \""+string(all.get(0))+"\", occurring "+bag.num(all.get(0))+" times."};
       }
       return InterestLevel.INTERESTING;
   }
   
   private static String[] empty=new String[0];
   static String string(Object o)
   {
       if (o==null || "".equals(o) || empty.equals(o))
           return "[missing]";
       return o.toString();
   }
 
   @Override
   public boolean canHandleType(Class type) {
       return type==Double.class || type==String.class || type==String[].class;
   }
}