Advertisement
Guest User

Get delimiter from CSV file

a guest
Jul 29th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1.     public char getDelimeter(String file) {
  2.         FileReader reader;
  3.         try {
  4.             reader = new FileReader(file);
  5.         } catch (FileNotFoundException e) {
  6.             return 0;
  7.         }
  8.         BufferedReader breader = new BufferedReader(reader);
  9.         int linesRead = 0;
  10.         // char counts for each line
  11.         int[] charCounts = new int[256 * 100];
  12.         int i;
  13.         for (i = 0; i < charCounts.length; i++) {
  14.             charCounts[i] = 0;
  15.         }
  16.         try {
  17.             int prevChar = 0;
  18.             // now we count how much each char occures on each line
  19.             while (linesRead < 100) {
  20.                 int which;
  21.                 which = breader.read();
  22.                 if (which >= 256)
  23.                     continue;
  24.                 if (which == -1)
  25.                     break;
  26.                 if (which == '\n' || which == '\r') {
  27.                     if (prevChar != '\n' && prevChar != '\r')
  28.                         linesRead++;
  29.                     prevChar = which;
  30.                     continue;
  31.                 }
  32.                 charCounts[which + 256 * linesRead]++;
  33.             }
  34.         } catch (IOException e) {
  35.             // TODO Auto-generated catch block
  36.             e.printStackTrace();
  37.         }
  38.         try {
  39.             breader.close();
  40.         } catch (IOException e) {
  41.         }
  42.         float[] averages = new float[256];
  43.         int[] peaky = new int[256];
  44.         int j;
  45.         // first get the averages
  46.         for (j = 0; j < 256; j++) {
  47.             peaky[j] = 0;
  48.             averages[j] = 0;
  49.             for (i = 0; i < linesRead; i++) {
  50.                 averages[j] += charCounts[j + i * 256];
  51.             }
  52.             averages[j] /= (float) linesRead;
  53.         }
  54.  
  55.         // now we calculate the "peakyness". We assume that the delimiter will
  56.         // appear
  57.         // a constant number of times on each line, and hence its peakyness will
  58.         // be 0
  59.         for (j = 0; j < 256; j++) {
  60.             peaky[j] = 0;
  61.             // calculate peakiness
  62.             for (i = 0; i < linesRead; i++) {
  63.                 float p = charCounts[j + i * 256] - averages[j];
  64.                 peaky[j] += p * p;
  65.             }
  66.         }
  67.  
  68.         // if the character never appears then we ignore it.
  69.         // The peakyness will be 0
  70.         boolean[] valid = new boolean[256];
  71.         for (j = 0; j < 256; j++) {
  72.             valid[j] = false;
  73.             for (i = 0; i < linesRead; i++) {
  74.                 if (charCounts[i * 256 + j] > 0) {
  75.                     valid[j] = true;
  76.                     break;
  77.                 }
  78.             }
  79.         }
  80.  
  81.         // find the smallest peakyness character
  82.         int smallest_char = -1;
  83.         for (i = 0; i < 256; i++) {
  84.             if (valid[i] == false)
  85.                 continue;
  86.  
  87.             if (smallest_char == -1 || peaky[i] < peaky[smallest_char]) {
  88.                 smallest_char = i;
  89.             }
  90.         }
  91.         return Character.toChars(smallest_char)[0];
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement