/* * Main.java * * Created on 5 febbraio 2007, 10.51 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package fortunedescriptor; import java.io.*; import java.util.StringTokenizer; /** * * @author Francesco Laurita */ public class Main { /** Creates a new instance of Main */ public Main() { } /** * @param args the command line arguments */ public static void main(String[] args) throws Exception { String cwd = System.getProperty("user.dir"); File f = new File(cwd); int m; String[] list = f.list(); StringBuffer buff, tmp; int total_sent = 0; FileWriter fw = new FileWriter(cwd+File.separator+"descriptor.txt"); for(int i = 0; i < list.length; i++){ m = 0; buff = new StringBuffer(); tmp = new StringBuffer(); if (list[i].equals("descriptor.txt")) continue; if (!(new File(list[i]).isFile())) continue; //System.out.println(formatFile(new File(list[i]))); System.err.print("Processing file: " + list[i]); String newFile = formatFile(new File(list[i])); FileWriter tfw = new FileWriter(new File(list[i])); tfw.write(newFile); tfw.flush(); tfw.close(); buff.append(list[i] + " " +tmp.toString().length()); BufferedReader in = new BufferedReader(new FileReader(list[i])); String str; while ((str = in.readLine()) != null) { tmp.append(str+"\n"); if (str.startsWith("%",0)){ //System.err.println("Mark found at " + tmp.toString().length()); total_sent++; buff.append("_"+(tmp.toString().length()-1)); } m++; } in.close(); System.err.printf(" found %d sentences\n",m); //System.out.println(buff.toString()); fw.write(buff.toString()+"\n"); fw.flush(); } System.err.printf("Total sentences: %d\n",total_sent); fw.close(); System.out.println("Descriptor file crated"); } public static String removeWs(String in, char cc){ StringBuffer buff = new StringBuffer(); in = in.replace('\t',' '); StringTokenizer token = new StringTokenizer(in," "); while(token.hasMoreTokens()){ buff.append(token.nextToken() + " "); } return buff.toString(); } public static String formatFile(File f) throws Exception{ BufferedReader in = new BufferedReader(new FileReader(f)); StringBuffer buff = new StringBuffer(); String str; while ((str = in.readLine()) != null) { if (str.startsWith("%",0)){ buff.append("\n"+str+"\n"); }else{ str += " "; buff.append(removeWs(str,' ')); } } return buff.toString(); } }