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