Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / commands / mesures / Mesures.groovy @ 1000

History | View | Annotate | Download (4.1 kB)

1
package org.txm.scripts.functions.mesures;
2

    
3
import java.io.File;
4
import java.io.FileOutputStream;
5
import java.io.IOException;
6
import java.io.OutputStreamWriter;
7
import java.io.Writer;
8
import java.text.DecimalFormat;
9
import java.util.ArrayList;
10
import java.util.Arrays;
11

    
12
import org.txm.searchengine.cqp.corpus.Corpus;
13
import org.txm.searchengine.cqp.corpus.Partition;
14

    
15
/**
16
 * Container of mesures
17
 * 
18
 * @author mdecorde
19
 *
20
 */
21
public class Mesures extends ArrayList<Mesure>{
22

    
23
        private static final long serialVersionUID = 324346022230541761L;
24

    
25
        ArrayList<Corpus> data = new ArrayList<Corpus>();
26

    
27
        public Mesures(Corpus corpus) {
28
                data = new ArrayList<Corpus>();
29
                data.add(corpus);
30
        }
31

    
32
        public Mesures(ArrayList<Corpus> corpora) {
33
                data = corpora;
34
                println "hello TXM"
35
        }
36

    
37
        public Mesures(Partition partition) {
38
                data.addAll(partition.getParts());
39
        }
40

    
41
        public boolean add(Mesure mesure) {
42
                if (!mesure.compute(data)) {
43
                        return false;
44
                }
45

    
46
                return super.add(mesure);
47
        }
48

    
49
        public boolean export(Writer writer, String encoding, String colseparator, String txtseparator) throws IOException {
50
                String line = "Mesures";
51
                for (Corpus c : data) {
52
                        line += colseparator+txtseparator+c.getName().replaceAll(txtseparator,txtseparator+txtseparator)+txtseparator;
53
                }
54
                writer.write(line+"\n");
55

    
56
                for (Mesure m : this) {
57
                        for (MesureResult r : m) {
58
                                line = r.name;
59
                                for (int i = 0 ; i < r.data.length ; i++)
60
                                        line += colseparator+r.data[i];
61
                                writer.write(line+"\n");
62
                        }
63
                }
64

    
65
                return true;
66
        }
67

    
68
        public boolean export(File outfile, String encoding, String colseparator, String txtseparator) throws IOException {
69
                Writer writer = new OutputStreamWriter(
70
                                new FileOutputStream(outfile), encoding); //$NON-NLS-1$
71
                boolean rez = export(writer, encoding, colseparator, txtseparator);
72
                writer.close();
73
                return rez;
74
        }
75

    
76
        DecimalFormat floatFormatter = new DecimalFormat("###,###.##");
77
        DecimalFormat intFormatter = new DecimalFormat("###,###,###");
78
        public void prettyPrint() {        
79
                ArrayList<String[]> lines = new ArrayList<String[]>();
80
                int[] maxx = new int[data.size()+1];
81
                int[] maxxDecimales = new int[data.size()+1];
82

    
83

    
84
                String[] line = new String[data.size() + 1];
85
                line[0] = "Mesures";
86
                maxx[0] = line[0].length();
87
                for (int i = 1 ; i <= data.size() ; i++) {
88
                        line[i] = data.get(i-1).getName();
89
                        maxx[i] = line[i].length();
90
                }
91
                lines.add(line);
92

    
93
                // fill lines
94
                for (Mesure m : this) {
95
                        for(MesureResult r : m) {
96
                                line = new String[data.size()+1];
97
                                line[0] = r.name;
98
                                if (maxx[0] < r.name.length())
99
                                        maxx[0] = r.name.length();
100

    
101
                                int i = 1;
102
                                for (Object o : r.data) {
103
                                        String str = ""+o;
104
                                        if (o instanceof Integer) str = intFormatter.format(o);
105
                                        else if (o instanceof Float) str = floatFormatter.format(o);
106

    
107
                                        line[i] = str;
108

    
109
//                                        if (maxx[i] < str.length())
110
//                                                maxx[i] = str.length();
111

    
112
                                        int idxDecimal = str.indexOf(",");
113
                                        if (idxDecimal > 0) {
114
                                                idxDecimal = str.length() - idxDecimal;
115
                                                if (maxxDecimales[i] < idxDecimal)
116
                                                        maxxDecimales[i] = idxDecimal;
117
                                        }
118
                                        i++;
119
                                }
120
                                lines.add(line);
121
                        }
122
                }
123

    
124
//                System.out.println("maxx: "+Arrays.toString(maxx));
125
//                System.out.println("maxxDecimales: "+Arrays.toString(maxxDecimales));
126

    
127
                // fix cells right lengths (decimals)
128
                boolean notfirst = false;
129
                for (String[] l : lines) {
130
                        for (int i = 1 ; i < data.size() +1; i++) {
131
                                String str = l[i];
132
                                int nbDecimalToAdd = 0;
133

    
134
                                if (str.indexOf(",") < 0) {
135
                                        nbDecimalToAdd = maxxDecimales[i];
136
                                } else {
137
                                        nbDecimalToAdd = maxxDecimales[i] - (str.length() - str.indexOf(","));
138
                                }
139

    
140
                                if (notfirst)
141
                                for (int j = 0 ; j < nbDecimalToAdd ; j++)
142
                                        str += " ";
143

    
144
                                if (maxx[i] < str.length())
145
                                        maxx[i] = str.length();
146
                                
147
                                l[i] = str;
148
                        }
149
                        notfirst = true;
150
                }
151
                
152
                // fix cells left lengths (alignment)
153
                for (String[] l : lines) {
154
                        for (int i = 0 ; i < data.size() +1; i++) {
155
                                while (l[i].length() < maxx[i]) {
156
                                        if (i == 0)        l[i] = ""+l[i]+" ";
157
                                        else l[i] = " "+l[i];
158
                                }
159
                        }
160
                }
161

    
162
                // print cells
163
                for (String[] l : lines) {
164
                        for (int i = 0 ; i < data.size() +1; i++) {
165
                                if (i == 0) System.out.print(l[i]);
166
                                else  System.out.print(" "+l[i]);
167
                        }
168
                        System.out.println("");
169
                }
170
        }
171
}