Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.94 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Arrays;
  3.  
  4. public class DialogEncoder {
  5.  
  6.     private final static String analysisFileName = "analysis/war_utf8.txt";
  7.     private final static String ALPHABET = "абвгдеёжзийкламнопрстуфхцчшщъыьэюя";
  8.     private final static boolean DEBUG = true;
  9.     private final static String [] ENCODINGS = new String [] {
  10.             "cp866",
  11.             "cp1251",
  12.             "koi8-r",
  13.             "iso-8859-5",
  14.             "utf-8",
  15.             "utf-16le",
  16.             "utf-16be",
  17.     };
  18.  
  19.     private double [] simpleFrequencyVector;
  20.  
  21.     public DialogEncoder(String analysisFileName) throws
  22.             IOException
  23.     {
  24.         FileReader reader = new FileReader(analysisFileName);
  25.         StringBuilder text = new StringBuilder();
  26.         while(reader.ready())
  27.         {
  28.             char ch = (char)reader.read();
  29.             text.append(ch);
  30.         }
  31.         reader.close();
  32.         simpleFrequencyVector = getFrequencyVector(text.toString());
  33.     }
  34.  
  35.     private double [] getFrequencyVector(String text)
  36.     {
  37.         return getFrequencyVector(text.toCharArray());
  38.     }
  39.  
  40.     private double [] getFrequencyVector(char [] text)
  41.     {
  42.         double [] frequencyVector = new double [ALPHABET.length()];
  43.         int sum = 0;
  44.         for(char ch : text)
  45.         {
  46.             int idx = ALPHABET.indexOf(Character.toLowerCase(ch));
  47.             if(idx != -1)
  48.             {
  49.                 sum++;
  50.                 frequencyVector[idx]++;
  51.             }
  52.         }
  53.         for(int i = 0; i < ALPHABET.length(); i++)
  54.         {
  55.             frequencyVector[i] /= sum;
  56.         }
  57.         return frequencyVector;
  58.     }
  59.  
  60.     private double getDist(double [] first, double [] second)
  61.     {
  62.         double result = 0;
  63.         for(int i = 0; i < ALPHABET.length(); i++)
  64.         {
  65.             double value = first[i] - second[i];
  66.             result += value * value;
  67.         }
  68.         return Math.sqrt(result);
  69.     }
  70.  
  71.     private char [] encodeData(byte [] data) throws
  72.             IOException
  73.     {
  74.         double minDist = Double.MAX_VALUE;
  75.         String minEncoding = "";
  76.         byte [] buff = new byte [data.length];
  77.         for(int i = 0, j = 0; i < data.length; i++)
  78.         {
  79.             if(data[i] != '\n')
  80.             {
  81.                 buff[j++] = data[i];
  82.             }
  83.         }
  84.         for(String encoding : ENCODINGS)
  85.         {
  86.             String text = new String(buff, encoding);
  87.             double [] frequencyVector = getFrequencyVector(text);
  88.             double dist = getDist(frequencyVector, simpleFrequencyVector);
  89.             if(dist < minDist)
  90.             {
  91.                 minDist = dist;
  92.                 minEncoding = encoding;
  93.             }
  94.         }
  95.  
  96.         if(DEBUG) {
  97.             System.out.println(minEncoding);
  98.         }
  99.  
  100.         StringBuilder result = new StringBuilder();
  101.         for(int i = 0, j = 0; i < data.length; i++)
  102.         {
  103.             if(data[i] != '\n' || i + 1 == data.length)
  104.             {
  105.                 buff[j++] = data[i];
  106.             }
  107.             else
  108.             {
  109.                 result.append(new String(Arrays.copyOf(buff, j), minEncoding));
  110.                 result.append('\n');
  111.                 j = 0;
  112.             }
  113.         }
  114.  
  115.         return result.toString().toCharArray();
  116.     }
  117.  
  118.     private char [] mergeDialogs(char [] first, char [] second)
  119.     {
  120.         char [] result = new char[first.length + second.length];
  121.         int i = 0, j = 0, k = 0;
  122.         while(i < first.length || j < second.length)
  123.         {
  124.             if(k == 0 && i < first.length)
  125.             {
  126.                 result[i + j] = first[i];
  127.                 if(first[i] == '\n')
  128.                     k = 1;
  129.                 i++;
  130.             }
  131.             else if(j < second.length)
  132.             {
  133.                 result[i + j] = second[j];
  134.                 if(second[j] == '\n')
  135.                     k = 0;
  136.                 j++;
  137.             }
  138.             else
  139.             {
  140.                 k = 1 - k;
  141.             }
  142.         }
  143.         return result;
  144.     }
  145.  
  146.     private char [] cutZeros(char [] text)
  147.     {
  148.         int idx = 0;
  149.         for(; idx < text.length; idx++)
  150.         {
  151.             if(text[idx] == 0)
  152.              break;
  153.         }
  154.         return Arrays.copyOf(text, idx);
  155.     }
  156.  
  157.     public void encode(String inputFileName, String outputFileName) throws
  158.             IOException
  159.     {
  160.         File inputFile = new File(inputFileName);
  161.         int fileLength = (int)inputFile.length();
  162.         byte [] data = new byte [fileLength];
  163.         byte [][] dialogs = new byte [][] {
  164.                 new byte [fileLength],
  165.                 new byte [fileLength],
  166.         };
  167.         int [] dialogsSizes = new int [] {0, 0};
  168.         FileInputStream fileInputStream = new FileInputStream(inputFile);
  169.         fileInputStream.read(data);
  170.         fileInputStream.close();
  171.         int dialogIdx = 0;
  172.         for(byte b : data)
  173.         {
  174.             int currentDialogSize = dialogsSizes[dialogIdx];
  175.             dialogs[dialogIdx][currentDialogSize] = b;
  176.             dialogsSizes[dialogIdx]++;
  177.             if(b == '\n')
  178.                 dialogIdx = 1 - dialogIdx;
  179.         }
  180.  
  181.         char [] encodedData = cutZeros(mergeDialogs(encodeData(dialogs[0]), encodeData(dialogs[1])));
  182.         FileWriter writer = new FileWriter(outputFileName);
  183.         writer.write(encodedData);
  184.         writer.close();
  185.     }
  186.  
  187.     public static void main(String [] args) throws Exception
  188.     {
  189.         DialogEncoder encoder = new DialogEncoder(analysisFileName);
  190.  
  191.         String inputFileName;
  192.         String outputFileName;
  193.  
  194.         try {
  195.             inputFileName = args[0];
  196.             outputFileName = args[1];
  197.         } catch(ArrayIndexOutOfBoundsException ex) {
  198.             System.out.println("java DialogEncoder [input_file_name] [output_file_name]");
  199.             return;
  200.         }
  201.  
  202.         encoder.encode(inputFileName, outputFileName);
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement