001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.utilities.statistics;
021    
022    import java.io.File;
023    import java.io.Serializable;
024    import java.net.URLEncoder;
025    import java.util.ArrayList;
026    import java.util.Arrays;
027    import java.util.List;
028    
029    import org.dllearner.utilities.Files;
030    import org.dllearner.utilities.StringFormatter;
031    
032    
033    /**
034     * Class to collect results and output them as a latex table or other formats.
035     * 
036     * @author Sebastian Hellmann
037     *
038     */
039    public class Table implements Serializable{
040    
041            private static final long serialVersionUID = 0l;
042        
043        //used to give a good percentage output
044        //private DecimalFormat df = new DecimalFormat( ".00%" ); 
045        private List<TableColumn> columns = new ArrayList<TableColumn>();
046        
047        private String tableName = "";
048        private String caption = "";
049        private String label = "";
050        
051        public Table(String tableName){
052            this.tableName = tableName;
053        } 
054        
055        public static void main(String[] args) {
056                    boolean production = true;
057            if(production){
058                    String tablename = "myTable";
059                    //String tableFile = "sembib100/sofar/table";
060                    //String tableFile = "sembib100/2ndExp/table2nd.table";
061                    String tableDir = "sembib100/sofarNew1st";
062                    Table t = createTableFromSerializedColumsInDir(tablename, tableDir);
063                    Files.createFile(new File(tableDir+File.separator+tablename+".tex"), t.getLatexString());
064                    
065            }else{
066                            
067                    Table t = new Table("myTable");
068                            String tableFile = "results/table/myTable";
069                            TableColumn c1 = new TableColumn("col1", new String[]{"a","b"});
070                            TableColumn c2 = new TableColumn("col2", new String[]{"c","d"});
071                            t.addColumn(c1);
072                            System.out.println(t.getLatexString());
073                            
074                            serializeColumns(t, "results/table",tableFile );
075                            
076                            t = createTableFromSerializedColumsInFile("myTable", tableFile);
077                            System.out.println(t.getLatexString());
078                            
079                            t.addColumn(c2);
080                            serializeColumns(t, "results/table",tableFile );
081                            t = createTableFromSerializedColumsInFile("myTable", tableFile);
082                            System.out.println(t.getLatexString());
083            }       
084                    
085                    System.out.println("done");
086            }
087        
088        public String getLatexString(){
089            String tabular = "";
090            for (int i = 0; i < columns.size(); i++) {
091                            tabular+="l";
092                    }
093            
094            String headers = latexRow(getColumnHeaders());
095            headers = StringFormatter.myReplaceAll(headers, '_', "\\_");
096            headers = StringFormatter.myReplaceAll(headers, '%', "\\%");
097            
098            String table="";
099            table += "\\documentclass{article}\n";
100            table += "\\usepackage{rotating}\n";
101            table += "\\begin{document}\n";
102                    table += "\\begin{sidewaystable*}\n";
103                    table += "\t\\centering\n";
104                    table += "\t\t\\begin{tabular}{"+tabular+"}\n";
105                    table += "\\hline\n";
106                    table += headers.replaceAll("\\_", "\\_");
107                    table += "\\hline\n";
108                    // add here
109                    for (int i = 0; i < getNumberOfRows(); i++) {
110                            String tmp = getRowInLatex(i);
111                            tmp = StringFormatter.myReplaceAll(tmp, '_', "\\_");
112                            tmp = StringFormatter.myReplaceAll(tmp, '%', "\\%");
113                            table += tmp;
114                    }
115                    table += "\\end{tabular}\n";
116                    table += "\t\\caption{"+caption+"}\n";
117                    table += "\t\\label{"+label+"}\n";
118                    table += "\\end{sidewaystable*}\n\n";
119                    table += "\\end{document} \n\n";
120    
121                    //List<String> myList = new ArrayList<String>({""});
122                    
123                    //List<String> list = Arrays.asList( "","" ); 
124            return table;
125            
126        }
127        
128        public String getRowInLatex(int index){
129            List<String> l = new ArrayList<String>();
130            for(TableColumn c: columns){
131                    l.add(c.getEntry(index));
132            }
133            return latexRow(l);
134        }
135        
136        public int getNumberOfRows(){
137            if(columns.isEmpty())return 0;
138            else return columns.get(0).getSize();
139        }
140        
141        public void removeColumn(String header){
142            for (int i = 0; i < columns.size(); i++) {
143                            if(columns.get(i).getHeader().equals(header)){
144                                    columns.remove(i);
145                                    return;
146                            }
147                    }
148        }
149        
150        
151        public List<String> getColumnHeaders(){
152            List<String> entries = new ArrayList<String>();
153            for (TableColumn c : columns) {
154                             entries.add(c.getHeader());
155                    }
156            return entries;
157        }
158        
159        public String latexRow(List<String> entries){
160            String ret="";
161            for (String one : entries) {
162                            ret+=" "+one+"\t& ";
163                    }
164            ret = ret.substring(0,ret.length()-3);
165            ret+="\t\\\\\n";
166            return ret;
167        }
168        
169        public void addColumn(TableColumn c){
170            if(columns.isEmpty()){
171                    columns.add(c);
172            }else{
173                    if(getNumberOfRows()!=c.getSize()){
174                            System.out.println("ERROR: size of columns doesn't match");
175                            System.exit(0);
176                    }else{
177                            columns.add(c);
178                    }
179            }
180        }
181        
182        public static Table createTableFromSerializedColumsInFile(String tableName, String tableFile){
183            String[] columnFiles=new String[]{};
184                    try{
185                            columnFiles = Files.readFileAsArray(new File(tableFile));
186                    }catch (Exception e) {
187                                     e.printStackTrace();
188                            }
189                    return createTable(tableName, columnFiles);
190            
191        }
192        
193        public static Table createTableFromSerializedColumsInDir(String tableName, String columnDir){
194            String[] columnFiles= new File(columnDir).list();
195                    Arrays.sort(columnFiles);
196                    for (int i=0; i< columnFiles.length;i++) {
197                                    columnFiles[i]=columnDir+File.separator+columnFiles[i];
198                            System.out.println(columnFiles[i]);
199                            }
200                    //System.exit(0);
201                    return createTable(tableName, columnFiles);
202            
203        }
204        
205    
206        private static Table createTable(String tableName, String[] columnFiles){
207            Table ret = new Table(tableName);
208            try{
209                    
210                    
211                    for (String filename : columnFiles) {
212                            if(!filename.endsWith(".column")){continue;}
213                            if(filename.replaceAll(" ", "").length()==0)continue;
214                            TableColumn col = TableColumn.deSerialize(new File(filename));
215                            //TableColumn col = (TableColumn) Files.readObjectfromFile(new File(filename));
216                                    ret.addColumn(col);
217                            }
218            //      FileWriter fw =  new FileWriter ();
219            }catch (Exception e) {
220                            e.printStackTrace();
221                    }
222            return ret;
223        }
224        
225        public static void serializeColumns(Table t, String dir, String tableFile){
226            String column = ".column";
227            String content = "";
228            dir = StringFormatter.checkIfDirEndsOnSlashAndRemove(dir);
229            Files.mkdir(dir);
230            
231            try{
232                    int i=0;
233                    for(TableColumn c:t.getColumns()){
234                            String header = URLEncoder.encode(c.getHeader(),"UTF-8");
235                            String columnFileName = dir+File.separator+t.getTableName()+(i++)+header+column;
236                            c.serialize(new File(columnFileName));
237                            //Files.writeObjectToFile(c, new File(filename));
238                            content += columnFileName+System.getProperty("line.separator");
239                    }
240                    Files.createFile(new File(tableFile), content);
241                    //
242                    //FileWriter fw =  new FileWriter ();
243            }catch (Exception e) {
244                    e.printStackTrace();
245                    }
246        }
247    
248    
249            public List<TableColumn> getColumns() {
250                    return columns;
251            }
252    
253    
254            public String getTableName() {
255                    return tableName;
256            }
257    
258    
259            public void setTableName(String tableName) {
260                    this.tableName = tableName;
261            }
262        
263        
264    }