001package conexp.fx.core.importer; 002 003import java.io.BufferedReader; 004 005/* 006 * #%L 007 * Concept Explorer FX 008 * %% 009 * Copyright (C) 2010 - 2023 Francesco Kriegel 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU General Public License as 013 * published by the Free Software Foundation, either version 3 of the 014 * License, or (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU General Public 022 * License along with this program. If not, see 023 * <http://www.gnu.org/licenses/gpl-3.0.html>. 024 * #L% 025 */ 026 027import java.io.File; 028import java.io.FileReader; 029import java.io.IOException; 030import java.io.UncheckedIOException; 031import java.util.Iterator; 032import java.util.LinkedList; 033import java.util.List; 034 035import conexp.fx.core.context.MatrixContext; 036 037public final class CSVImporter { 038 039 public static final List<String[]> getTuples(final File file) { 040 final CSVImporter csvImporter = new CSVImporter(file); 041 csvImporter.readFile(); 042 return csvImporter.getTuples(); 043 } 044 045 public static final List<String[]> getTuples(final File file, final String delimiter) { 046 final CSVImporter csvImporter = new CSVImporter(file, delimiter); 047 csvImporter.readFile(); 048 return csvImporter.getTuples(); 049 } 050 051 public static final void importContext( 052 final File file, 053 final MatrixContext<String, String> context, 054 final String delimiter) throws Exception { 055 final CSVImporter csvImporter = new CSVImporter(file, delimiter); 056 csvImporter.readFile(); 057 csvImporter.toContext2(context); 058 } 059 060 private final File file; 061 private final String delimiter; 062 private final List<String[]> tuples; 063 064 public CSVImporter(final File file) { 065 this(file, ","); 066 } 067 068 public CSVImporter(final File file, final String delimiter) { 069 super(); 070 this.file = file; 071 this.delimiter = delimiter; 072 this.tuples = new LinkedList<String[]>(); 073 } 074 075 public final void readFile() { 076 try { 077 final BufferedReader reader = new BufferedReader(new FileReader(file)); 078 final Iterator<String> it = reader.lines().iterator(); 079 while (it.hasNext()) { 080 final String[] tuple = it.next().split(delimiter); 081 if (tuple.length > 0) 082 tuples.add(tuple); 083 } 084 reader.close(); 085 } catch (IOException e) { 086 throw new UncheckedIOException(e); 087 } 088 } 089 090 public final List<String[]> getTuples() { 091 return tuples; 092 } 093 094 public final void toContext2(final MatrixContext<String, String> context) { 095 for (String[] row : tuples) 096 context.add(row[0], row[1]); 097 } 098 099 public final void toContext(final MatrixContext<String, String> context) throws Exception { 100 final String[][] array = tuples.toArray(new String[][] {}); 101 final int rows = array.length - 1; 102 final int cols = array[0].length - 2; 103 final List<String> objects = new LinkedList<String>(); 104 final List<String> attributes = new LinkedList<String>(); 105 for (int r = 1; r < rows + 1; r++) 106 objects.add("(" + array[r][0] + "," + array[r][1] + ")"); 107 final String[] header = array[0]; 108 for (int c = 2; c < cols + 2; c++) { 109 if (attributes.contains(header[c])) 110 throw new Exception("Duplicate attribute name."); 111 attributes.add(header[c]); 112 } 113 context.rowHeads().addAll(objects); 114 context.colHeads().addAll(attributes); 115 for (int r = 1; r < rows + 1; r++) 116 for (int c = 2; c < cols + 2; c++) 117 if (Integer.valueOf(array[r][c].trim()).equals(1)) 118 context.matrix().setBoolean(true, r - 1, c - 2); 119 } 120 121 public final MatrixContext<String, String> toContext() throws Exception { 122 MatrixContext<String, String> context = new MatrixContext<String, String>(false); 123 toContext(context); 124 return context; 125 } 126}