-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditDistance1.java
169 lines (112 loc) · 3.61 KB
/
EditDistance1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.*;
public class editdistance{
static String[][] main_op;
//This method calculates cost.
public double calculation(String[][] op,String s1, String s2){
double [][] solution = new double[s1.length()+1][s2.length()+1]; //matix for cost calculation
for (int i = 0; i <=s2.length(); i++) {
solution[0][i] =i*1;
op[0][i]="insert"; //op is matrix for tracking back operations
}
for (int i = 0; i <=s1.length(); i++) {
solution[i][0] =i*1;
op[i][0]="delete";
}
int m = s1.length();
int n = s2.length();
for (int i = 1; i <=m ; i++) {
for (int j = 1; j <=n ; j++) {
if(s1.charAt(i-1)==s2.charAt(j-1)){
solution[i][j] = solution[i-1][j-1] + 0; //copy
op[i][j]="copy";
}
else if(s1.charAt(i-1)!=s2.charAt(j-1)){
double replace = solution[i-1][j-1] + 2; // replace
double insert = solution[i][j - 1] + 1; //insert
double delete = solution[i-1][j] + 1; //delete
double min;
if(replace>insert){
min=insert;
op[i][j]="insert";
solution[i][j] = min;
}
else{
min=replace;
op[i][j] = "replace";
solution[i][j] = min;
}
if(min>delete){
min = delete;
op[i][j]="delete";
solution[i][j] = min;
}
}
if(i>1 && j>1 && s1.charAt(i-1) == s2.charAt(j-2)&& s1.charAt(i-2)== s2.charAt(j-1) && (solution[i-2][j-2]+1.5)<solution[i][j]){
solution[i][j]=solution[i-2][j-2]+1.5; //twiddle
op[i][j]="twiddle";
}
} //end of j for loop
} //end of i for loop
for(int i=0; i<=m-1;i++){
if((solution[i][n]+5)<solution[m][n]){
solution[m][n]=solution[i][n]+5; //kill
op[m][n]="kill "+ (i+1);
}
}
System.out.println("sequence of operations: ");
editdistance ed1 = new editdistance();
ed1.opseq(op, s1.length(), s2.length()); //to print sequence
return solution[m][n];//return cost
}
//This method prints sequence of operations.
public void opseq(String[][]dop,int o,int p){
int o1=0;
int p1=0;
if(o==0 && p==0)
return;
if(dop[o][p]=="copy"|| dop[o][p]=="replace"){
o1=o-1;
p1=p-1;
}
else if(dop[o][p]=="twiddle"){
o1=o-2;
p1=p-2;
}
else if(dop[o][p]=="delete"){
o1=o-1;
p1=p;
}
else if(dop[o][p]=="insert"){
o1=o;
p1=p-1;
}
else {
try{
StringTokenizer st1 = new StringTokenizer(dop[o][p]);
st1.nextToken();
String k = st1.nextToken();
int k1 = Integer.parseInt(k);
k1=k1-1;
o1=k1;
p1=p;
}
catch(Exception e){
System.out.println(e);
}
}
opseq(dop,o1,p1);
System.out.println(dop[o][p]);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter first string");
String one = scan.nextLine();
System.out.println("Enter second string");
String two = scan.nextLine();
editdistance ed = new editdistance();
String[][] main_op = new String[one.length()+1][two.length()+1];
double result= ed.calculation(main_op,one, two);
System.out.println("optimal cost is : " + result);
scan.close();
}
}