Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / functions / mesures / Mesures.groovy @ 148

History | View | Annotate | Download (3.9 kB)

1
package org.txm.functions.mesures;
2

    
3
import java.text.DecimalFormat
4

    
5
import org.txm.searchengine.cqp.corpus.Corpus
6
import org.txm.searchengine.cqp.corpus.Partition
7

    
8
/**
9
 * Container of mesures
10
 * 
11
 * @author mdecorde
12
 *
13
 */
14
public class Mesures extends ArrayList<Mesure>{
15

    
16
        private static final long serialVersionUID = 324346022230541761L;
17

    
18
        ArrayList<Corpus> data = new ArrayList<Corpus>();
19

    
20
        public Mesures(Corpus corpus) {
21
                data = new ArrayList<Corpus>();
22
                data.add(corpus);
23
        }
24

    
25
        public Mesures(ArrayList<Corpus> corpora) {
26
                data = corpora;
27
                println "hello TXM"
28
        }
29

    
30
        public Mesures(Partition partition) {
31
                data.addAll(partition.getParts());
32
        }
33

    
34
        public boolean add(Mesure mesure) {
35
                if (!mesure.compute(data)) {
36
                        return false;
37
                }
38

    
39
                return super.add(mesure);
40
        }
41

    
42
        public boolean export(Writer writer, String encoding, String colseparator, String txtseparator) throws IOException {
43
                String line = "Mesures";
44
                for (Corpus c : data) {
45
                        line += colseparator+txtseparator+c.getName().replaceAll(txtseparator,txtseparator+txtseparator)+txtseparator;
46
                }
47
                writer.write(line+"\n");
48

    
49
                for (Mesure m : this) {
50
                        for (MesureResult r : m) {
51
                                line = r.name;
52
                                for (int i = 0 ; i < r.data.length ; i++)
53
                                        line += colseparator+r.data[i];
54
                                writer.write(line+"\n");
55
                        }
56
                }
57

    
58
                return true;
59
        }
60

    
61
        public boolean export(File outfile, String encoding, String colseparator, String txtseparator) throws IOException {
62
                Writer writer = new OutputStreamWriter(
63
                                new FileOutputStream(outfile), encoding); //$NON-NLS-1$
64
                boolean rez = export(writer, encoding, colseparator, txtseparator);
65
                writer.close();
66
                return rez;
67
        }
68

    
69
        DecimalFormat floatFormatter = new DecimalFormat("###,###.##");
70
        DecimalFormat intFormatter = new DecimalFormat("###,###,###");
71
        public void prettyPrint() {        
72
                ArrayList<String[]> lines = new ArrayList<String[]>();
73
                int[] maxx = new int[data.size()+1];
74
                int[] maxxDecimales = new int[data.size()+1];
75

    
76

    
77
                String[] line = new String[data.size() + 1];
78
                line[0] = "Mesures";
79
                maxx[0] = line[0].length();
80
                for (int i = 1 ; i <= data.size() ; i++) {
81
                        line[i] = data.get(i-1).getName();
82
                        maxx[i] = line[i].length();
83
                }
84
                lines.add(line);
85

    
86
                // fill lines
87
                for (Mesure m : this) {
88
                        for(MesureResult r : m) {
89
                                line = new String[data.size()+1];
90
                                line[0] = r.name;
91
                                if (maxx[0] < r.name.length())
92
                                        maxx[0] = r.name.length();
93

    
94
                                int i = 1;
95
                                for (Object o : r.data) {
96
                                        String str = ""+o;
97
                                        if (o instanceof Integer) str = intFormatter.format(o);
98
                                        else if (o instanceof Float) str = floatFormatter.format(o);
99

    
100
                                        line[i] = str;
101

    
102
//                                        if (maxx[i] < str.length())
103
//                                                maxx[i] = str.length();
104

    
105
                                        int idxDecimal = str.indexOf(",");
106
                                        if (idxDecimal > 0) {
107
                                                idxDecimal = str.length() - idxDecimal;
108
                                                if (maxxDecimales[i] < idxDecimal)
109
                                                        maxxDecimales[i] = idxDecimal;
110
                                        }
111
                                        i++;
112
                                }
113
                                lines.add(line);
114
                        }
115
                }
116

    
117
//                System.out.println("maxx: "+Arrays.toString(maxx));
118
//                System.out.println("maxxDecimales: "+Arrays.toString(maxxDecimales));
119

    
120
                // fix cells right lengths (decimals)
121
                boolean notfirst = false;
122
                for (String[] l : lines) {
123
                        for (int i = 1 ; i < data.size() +1; i++) {
124
                                String str = l[i];
125
                                int nbDecimalToAdd = 0;
126

    
127
                                if (str.indexOf(",") < 0) {
128
                                        nbDecimalToAdd = maxxDecimales[i];
129
                                } else {
130
                                        nbDecimalToAdd = maxxDecimales[i] - (str.length() - str.indexOf(","));
131
                                }
132

    
133
                                if (notfirst)
134
                                for (int j = 0 ; j < nbDecimalToAdd ; j++)
135
                                        str += " ";
136

    
137
                                if (maxx[i] < str.length())
138
                                        maxx[i] = str.length();
139
                                
140
                                l[i] = str;
141
                        }
142
                        notfirst = true;
143
                }
144
                
145
                // fix cells left lengths (alignment)
146
                for (String[] l : lines) {
147
                        for (int i = 0 ; i < data.size() +1; i++) {
148
                                while (l[i].length() < maxx[i]) {
149
                                        if (i == 0)        l[i] = ""+l[i]+" ";
150
                                        else l[i] = " "+l[i];
151
                                }
152
                        }
153
                }
154

    
155
                // print cells
156
                for (String[] l : lines) {
157
                        for (int i = 0 ; i < data.size() +1; i++) {
158
                                if (i == 0) System.out.print(l[i]);
159
                                else  System.out.print(" "+l[i]);
160
                        }
161
                        System.out.println("");
162
                }
163
        }
164
}