Advertisement
n0nick

MEDCalculator.java

Apr 11th, 2011
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. /**
  2.  * Fancy Minimal Edit Distance Calculator.
  3.  *
  4.  * @author sagiemao
  5.  * @author gittitda
  6.  *
  7.  */
  8. public class MEDCalculator {
  9.  
  10.     /**
  11.      * Given inserted characters, returns cost of its insertion.
  12.      *
  13.      * @param c
  14.      *            Character inserted
  15.      * @return Insertion cost
  16.      */
  17.     private static int ins_cost(char c) {
  18.         return 1;
  19.     }
  20.  
  21.     /**
  22.      * Given deleted character, returns cost of its deletion.
  23.      *
  24.      * @param c
  25.      *            Character deleted
  26.      * @return Deletion cost
  27.      */
  28.     private static int del_cost(char c) {
  29.         return 1;
  30.     }
  31.  
  32.     /**
  33.      * Given two characters replacing each other, returns cost of substitution.
  34.      *
  35.      * @param c1
  36.      *            Original character
  37.      * @param c2
  38.      *            Replaced character
  39.      * @return Substitution cost
  40.      */
  41.     private static int subst_cost(char c1, char c2) {
  42.         if (c1 == c2) {
  43.             return 0;
  44.         } else {
  45.             return 2;
  46.         }
  47.     }
  48.  
  49.     /**
  50.      * Prints a given MED table.
  51.      *
  52.      * @param s1
  53.      *            Original text
  54.      * @param s2
  55.      *            New text
  56.      * @param table
  57.      *            MED table
  58.      */
  59.     private static void print_table(String s1, String s2, int[][] table) {
  60.         int length1 = s1.length() + 1;
  61.         int length2 = s2.length() + 1;
  62.  
  63.         System.out.print("  # ");
  64.         for (int i = 1; i < length1; i++) {
  65.             System.out.print("" + s1.charAt(i - 1) + ' ');
  66.         }
  67.         System.out.println();
  68.  
  69.         for (int j = 0; j < length2; j++) {
  70.             if (j == 0) {
  71.                 System.out.print("# ");
  72.             } else {
  73.                 System.out.print("" + s2.charAt(j - 1) + ' ');
  74.             }
  75.  
  76.             for (int i = 0; i < length1; i++) {
  77.                 System.out.print("" + table[i][j] + ' ');
  78.             }
  79.             System.out.println();
  80.         }
  81.         System.out.println();
  82.     }
  83.  
  84.     /**
  85.      * Builds and returns MED table for two given texts
  86.      *
  87.      * @param s1
  88.      *            Original text
  89.      * @param s2
  90.      *            New text
  91.      * @return MED table
  92.      */
  93.     public static int[][] med_table(String s1, String s2) {
  94.         int length1 = s1.length() + 1;
  95.         int length2 = s2.length() + 1;
  96.  
  97.         int[][] table = new int[length1][length2];
  98.  
  99.         for (int i = 0; i < length1; i++) {
  100.             table[i][0] = i * ins_cost(s1.charAt(0));
  101.         }
  102.         for (int i = 0; i < length2; i++) {
  103.             table[0][i] = i * ins_cost(s2.charAt(0));
  104.         }
  105.  
  106.         for (int j = 1; j < length2; j++) {
  107.             for (int i = 1; i < length1; i++) {
  108.                 char c1 = s1.charAt(i - 1);
  109.                 char c2 = s2.charAt(j - 1);
  110.  
  111.                 table[i][j] = Math.min(Math.min(
  112.                         table[i - 1][j] + del_cost(c2),
  113.                         table[i][j - 1] + ins_cost(c1)),
  114.                         table[i - 1][j - 1] + subst_cost(c1, c2));
  115.             }
  116.         }
  117.  
  118.         return table;
  119.     }
  120.  
  121.     /**
  122.      * Main application procedure, expects 2 texts as arguments.
  123.      *
  124.      * @param args
  125.      *            Array of 2 strings to compare
  126.      */
  127.     public static void main(String[] args) {
  128.         if (args.length == 2) {
  129.             String s1 = args[0];
  130.             String s2 = args[1];
  131.  
  132.             int[][] table = med_table(s1, s2);
  133.  
  134.             System.out.println(String.format(
  135.                     "Minimal Edit Distance table for %s, %s:", s1, s2));
  136.             System.out.println();
  137.             print_table(s1, s2, table);
  138.  
  139.             System.out.println(String.format("Distance = %d.",
  140.                     table[s1.length()][s2.length()]));
  141.         } else {
  142.             System.err.println("Error! Missing two strings to compare.");
  143.         }
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement