Don't like ads? PRO users don't see any ads ;-)
Guest

Stdout :V

By: a guest on Apr 25th, 2012  |  syntax: Java  |  size: 6.09 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import com.aliasi.chunk.AbstractCharLmRescoringChunker;
  2. import com.aliasi.chunk.BioTagChunkCodec;
  3. import com.aliasi.chunk.Chunking;
  4. import com.aliasi.chunk.TagChunkCodec;
  5.  
  6. import com.aliasi.tag.Tagging;
  7. import com.aliasi.tokenizer.IndoEuropeanTokenizerFactory;
  8. import com.aliasi.tokenizer.TokenizerFactory;
  9. import com.aliasi.util.AbstractExternalizable;
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileWriter;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import java.lang.String;
  18. import java.text.DecimalFormat;
  19. import java.text.NumberFormat;
  20. import java.util.StringTokenizer;
  21. import java.util.concurrent.atomic.AtomicReference;
  22. import java.util.regex.Pattern;
  23. import java.util.regex.Matcher;
  24.  
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.apache.commons.lang3.mutable.MutableInt;
  27.  
  28. public class test {
  29.         @SuppressWarnings("rawtypes")
  30.         public static void main(String[] args) throws Exception {
  31.                 long startTime = System.currentTimeMillis();
  32.  
  33.                 String basePath = "/Users/davide/Documents/MPhil ACS/Machine Learning/ML Project/";
  34.                 File modelFolder = new File(basePath + "Models/");
  35.                 File dataFormat = new File(basePath + "Charbased model/DataFormat.raw");
  36.                 File dataFile = new File(basePath + "Charbased model/Data/sampletest1.raw");
  37.                 File evalScript = new File(basePath + "Dataset/Genia4ERtraining/evalIOB2.pl");
  38.                 File Solution = new File(basePath + "Dataset/Genia4ERtraining/sampletest1.iob2");
  39.                 String evalFolder = basePath + "Evaluations/";
  40.                 String writingloc = "";
  41.                 File[] models;
  42.                 if (modelFolder.isDirectory()) {
  43.                         models = modelFolder.listFiles();
  44.                 } else {
  45.                         models = new File[] {modelFolder};
  46.                 }
  47.                 int currfile = 0;
  48.                 int totfiles = models.length;
  49.                 for (File model : models) {
  50.                         currfile++;
  51.                         if (model.getName().equals(".DS_Store")) continue;
  52.                         System.out.println(model);
  53.                         AbstractCharLmRescoringChunker chunker = (AbstractCharLmRescoringChunker) AbstractExternalizable.readObject(model);
  54.                         TokenizerFactory tokenizerFactory
  55.                         = IndoEuropeanTokenizerFactory.INSTANCE;
  56.                         boolean enforceConsistency = true;
  57.                         TagChunkCodec tagChunkCodec
  58.                         = new BioTagChunkCodec(tokenizerFactory,
  59.                                         enforceConsistency);
  60.                         //System.out.println();
  61.  
  62.                         FileWriter myWriter = new FileWriter(basePath + "TestResults" + model.getName() + ".iob2");
  63.                         BufferedReader reader = new BufferedReader(new InputStreamReader(
  64.                                         new FileInputStream(dataFile)));
  65.                         BufferedReader formatreader = new BufferedReader(new InputStreamReader(
  66.                                         new FileInputStream(dataFormat)));
  67.                         String sentence, formatWord;
  68.                         String formatSentence;
  69.                         while ((sentence = reader.readLine()) != null) {
  70.                                 formatSentence = formatreader.readLine();
  71.                                 if (StringUtils.isEmpty(sentence)) {
  72.                                         myWriter.write("\n");
  73.                                         continue;
  74.                                 }
  75.                                 Chunking chunking = chunker.chunk(sentence); // Reading the line
  76.                                 Tagging<String> tagged = tagChunkCodec.toTagging(chunking);
  77.                                 String tg = tagged.toString();
  78.                                 tg = tg.replaceAll(" ", "\n");
  79.                                 tg = tg.replaceAll("(.*?)/([IB]_[^ \n]+|O)", "$1\t$2"); // Replace all slashes in tags, leave the others
  80.                                 tg = tg.replaceAll("\t([IB])_(.*)", "\t$1-$2"); // Replace underscores w/ dashes in tags
  81.  
  82.                                 // -------------------------------------
  83.                                 StringTokenizer st = new StringTokenizer(formatSentence);
  84.                                 String result = "";
  85.                                 MutableInt pos = new MutableInt(tg.indexOf("\t"));
  86.                                 String curWord = tg.substring(0, pos.intValue());
  87.                                 String firstTag = get_next_tag(tg, pos.intValue());
  88.                                 while (st.hasMoreTokens()) {
  89.                                         formatWord = st.nextToken();
  90.                                         while (! formatWord.equals(curWord)) {
  91.                                                 curWord += get_next_word(tg, pos);
  92.                                         }
  93.                                         result += curWord + "\t" + firstTag+"\n";
  94.                                         curWord = get_next_word(tg, pos);
  95.                                         firstTag = get_next_tag(tg, pos.intValue());
  96.                                 }
  97.                                 result.replaceAll("\t\t", "\t");
  98.                                 result.replaceAll("[.]\t\n", "[.]\tO\n");
  99.                                 result.replaceAll("(.*)\t([IB]-(.*))()\n\\(\tO$()\n(.*)\t(.*)\n\\)\tO", "$1\t$2\n\\(\tI-$3\n$6\tI-$3\n\\)\tI-$3");
  100.                                 myWriter.write(result + "\n");
  101.                                 result = "";
  102.                         }
  103.                         myWriter.flush();
  104.                         writingloc = basePath + "TestResults" + model.getName() + ".iob2";     
  105.                         //System.out.println("\nWRITINGLOC: " + writingloc + "\n");
  106.                         myWriter.close();
  107.                         String params[] = {
  108.                                         "perl", evalScript.toString(), Solution.toString(), writingloc
  109.                         };
  110.                         try {
  111.                             final Process proc = Runtime.getRuntime().exec(params);
  112.  
  113.                             try {
  114.                                 proc.waitFor();
  115.                             } catch (final InterruptedException e) {
  116.                                 e.printStackTrace();
  117.                             }
  118.  
  119.                             final BufferedReader outputReader = new BufferedReader(new InputStreamReader(proc
  120.                                     .getInputStream()));
  121.                             final BufferedReader errorReader = new BufferedReader(new InputStreamReader(proc
  122.                                     .getErrorStream()));
  123.                             FileWriter evalFile = new FileWriter(evalFolder + model.getName() + ".txt");
  124.                             String line;
  125.  
  126.                             while ((line = outputReader.readLine()) != null) {
  127.                                 evalFile.write(line);
  128.                                 evalFile.write("\n");
  129.                                 // System.out.println(line); // Works perfectly
  130.                             }
  131.  
  132.                             while ((line = errorReader.readLine()) != null) {
  133.                                 System.err.println(line);
  134.                             }
  135.                         } catch (final IOException e) {
  136.                             e.printStackTrace();
  137.                         }
  138.                 }
  139.                 //ProcessBuilder pb = new ProcessBuilder(params);
  140.                 //Process p = pb.start();
  141.                 //int exitCode = p.waitFor();
  142.                
  143.         //}
  144.  
  145.         long endTime   = System.currentTimeMillis();
  146.         NumberFormat formatter = new DecimalFormat("#0.00000");
  147.         System.out.print("All done. Execution time is " + formatter.format((endTime - startTime) / 1000d) + " seconds");
  148. } // main end
  149.  
  150. public static String get_next_word(String source, MutableInt pos) {
  151.         int a = source.indexOf("\n", pos.intValue());
  152.         int b = source.indexOf("\t", a+1);
  153.         if (b == -1) return "";
  154.         pos.setValue(b+1);
  155.         return source.substring(a+1, b);
  156. }
  157. public static String get_next_tag(String source, int pos) {
  158.         //int a = source.indexOf("\t", pos);
  159.         int a = pos;
  160.         int b = source.indexOf("\n", a+1);
  161.         if (b == -1) return "";
  162.         return source.substring(a, b);
  163. }
  164.  
  165. } // class end