Skip to content

Commit

Permalink
added edit distance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
yokurang committed Aug 12, 2024
1 parent 52c9c1a commit 8e9edec
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 108 deletions.
26 changes: 26 additions & 0 deletions lib/WordDiff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ let lcs xs' ys' =
done;
a.(0).(0)

let edit_distance (type a) (compare : a -> a -> bool) (s : a array)
(t : a array) : int =
let memo = Hashtbl.create ((Array.length s + 1) * (Array.length t + 1)) in

let rec edit_distance_helper i j =
match (i, j) with
| 0, x | x, 0 -> x
| i, j -> (
match Hashtbl.find_opt memo (i, j) with
| Some result -> result
| None ->
let result =
let cost_to_drop_both =
if compare s.(i - 1) t.(j - 1) then 0 else 1
in
min
(min
(edit_distance_helper (i - 1) j + 1)
(edit_distance_helper i (j - 1) + 1))
(edit_distance_helper (i - 1) (j - 1) + cost_to_drop_both)
in
Hashtbl.add memo (i, j) result;
result)
in
edit_distance_helper (Array.length s) (Array.length t)

let diff_words (s1 : string) (s2 : string) : line_content * line_content =
let words1 = Array.to_list (string_to_words s1) in
let words2 = Array.to_list (string_to_words s2) in
Expand Down
1 change: 1 addition & 0 deletions lib/WordDiff.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ val compute : string Block.t -> line_content Block.t
(* for tests *)
val lcs : 'a list -> 'a list -> 'a list
val diff_words : string -> string -> line_content * line_content
val edit_distance : ('a -> 'a -> bool) -> 'a array -> 'a array -> int
162 changes: 76 additions & 86 deletions test/cram/test.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ This is a cram test for the new executable.
1 addition, 1 removal
Modification of bin/dune
@@ -1,4 +1,4 @@
1 1 (executable
2 2 (public_name diffcessible)
3 3 (name main)
4 - (libraries diffcessible cmdliner))
4 + (libraries diffcessible cmdliner patch))
1 1 (executable
2 2 (public_name diffcessible)
3 3 (name main)
4 - - (libraries diffcessible cmdliner))
4 + + (libraries diffcessible cmdliner patch))



Expand All @@ -21,47 +21,37 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode
$ dummy_terminal example.diff n
Operation 2 of 14, 1 hunk
3 additions, 1 removal
Modification of bin/main.ml
@@ -1,7 +1,9 @@
1 1 open Diffcessible
2 2
3 3 let main () =
4 - Interactive_viewer.start ()
4 + let s = In_channel.input_all In_channel.stdin in
5 + let patch = Patch.to_diffs s in
6 + Interactive_viewer.start patch
5 7
6 8 open Cmdliner
7 9
1 1 open Diffcessible
2 2
3 3 let main () =
4 - Interactive_viewer.start ()
4 + let s = In_channel.input_all In_channel.stdin in
5 + let patch = Patch.to_diffs s in
6 + Interactive_viewer.start patch
5 7
6 8 open Cmdliner
7 9





Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
1 1 open Diffcessible
2 2
3 3 let main () =
4 - - Interactive_viewer.start ()
4 + + let s = In_channel.input_all In_channel.stdin in let patch = Patch.to_diffs s in Interactive_viewer.start patch
5 5
6 6 open Cmdliner
7 7







[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode
$ dummy_terminal example.diff n n
Operation 3 of 14, 1 hunk
1 addition, 1 removal
Modification of lib/dune
@@ -1,3 +1,3 @@
1 1 (library
2 2 (name diffcessible)
3 - (libraries notty nottui lwd))
3 + (libraries notty nottui lwd patch))
1 1 (library
2 2 (name diffcessible)
3 - - (libraries notty nottui lwd))
3 + + (libraries notty nottui lwd patch))



Expand All @@ -73,7 +63,7 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode
$ dummy_terminal example.diff h
Help Panel:

Expand All @@ -100,9 +90,8 @@ This is a cram test for the new executable.
2 additions, 1 removal
Modification of file.txt
@@ -2,1 +2,2 @@
2 - Hi everyone!
2 + Hello World!
3 + This is the diffcessible project.
1 - -Hi everyone!
1 + +Hello World! This is the diffcessible project.



Expand All @@ -115,15 +104,16 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.

[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode
$ dummy_terminal more-examples.diff n
Operation 2 of 2, 1 hunk
2 additions, 1 removal
Modification of file.txt
@@ -3,1 +5,2 @@
3 - This file starts at line 3.
5 + This file starts at line 5.
6 + This is the second test case in this file.
1 - -This file starts at line 3.
1 + +This file starts at line 5. This is the second test case in this file.




Expand All @@ -136,7 +126,7 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode



Expand All @@ -149,8 +139,8 @@ This is a cram test for the new executable.
2 additions, 1 removal
Modification of file.txt
@@ -2,1 @@ @@ +2,2 @@
1 - Hi everyone! 1 + Hello World!
2 + This is the diffcessible project.
1 Hi everyone! 1 Hello World!
2 This is the diffcessible project.



Expand All @@ -164,15 +154,15 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode

$ dummy_terminal more-examples.diff n t
Operation 2 of 2, 1 hunk
2 additions, 1 removal
Modification of file.txt
@@ -3,1 @@ @@ +5,2 @@
1 - This file starts at line 3. 1 + This file starts at line 5.
2 + This is the second test case in this file.
1 This file starts at line 3. 1 This file starts at line 5.
2 This is the second test case in this file.



Expand All @@ -186,7 +176,7 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode

$ dummy_terminal example.diff n n t
Operation 3 of 14, 1 hunk
Expand All @@ -195,7 +185,7 @@ This is a cram test for the new executable.
@@ -1,3 @@ @@ +1,3 @@
1 (library 1 (library
2 (name diffcessible) 2 (name diffcessible)
3 - (libraries notty nottui lwd)) 3 + (libraries notty nottui lwd patch))
3 (libraries notty nottui lwd)) 3 (libraries notty nottui lwd patch))



Expand All @@ -208,7 +198,7 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode

$ dummy_terminal example.diff n n n t
Operation 4 of 14, 1 hunk
Expand All @@ -217,20 +207,20 @@ This is a cram test for the new executable.
@@ -1,39 @@ @@ +1,25 @@
1 open Nottui 1 open Nottui
2 module W = Nottui_widgets 2 module W = Nottui_widgets
3 - open Lwd_infix 3 + (* open Lwd_infix *)
4 -
5 - type patch = unit
3 open Lwd_infix 3 (* open Lwd_infix *)
4
5 type patch = unit
6 4
7 let pure_str s = Lwd.pure (W.string s) 5 let pure_str s = Lwd.pure (W.string s)
8 -
9 - let string_of_counter c =
10 - let$ c = c in
11 - W.string (string_of_int c)
12 -
8
9 let string_of_counter c =
10 let$ c = c in
11 W.string (string_of_int c)
12
13 let quit = Lwd.var false 6 let quit = Lwd.var false
14 - let counter = Lwd.var 0 7 + let string_of_operation = Format.asprintf "%a" (Patch.pp_operation ~
15 - let counter_d = Lwd.get counter
Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
14 let counter = Lwd.var 0 7 let string_of_operation = Format.asprintf "%a" (Patch.pp_operation ~gi
15 let counter_d = Lwd.get counter
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode


$ dummy_terminal example.diff n n n n t
Expand All @@ -240,9 +230,9 @@ This is a cram test for the new executable.
@@ -1,5 @@ @@ +1,3 @@
1 (** Render and navigate through a diff. *) 1 (** Render and navigate through a diff. *)
2 2
3 - type patch = unit 3 + val start : Patch.t list -> unit
4 -
5 - val start : patch -> unit
3 type patch = unit 3 val start : Patch.t list -> unit
4
5 val start : patch -> unit



Expand All @@ -253,15 +243,15 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode


$ dummy_terminal example.diff n n n n n t
Operation 6 of 14, 1 hunk
1 addition, 1 removal
Rename with modifications dir1/file.txt to dir2/file.txt
@@ -1,1 @@ @@ +1,1 @@
1 - This is the original content. 1 + This is the modified content.
1 This is the original content. 1 This is the modified content.



Expand All @@ -276,15 +266,15 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode

$ dummy_terminal example.diff n n n n n n n t
Operation 8 of 14, 1 hunk
2 additions, 1 removal
Rename with modifications dir1/file.txt to dir2/file.txt
@@ -1,1 @@ @@ +1,2 @@
1 - This is the original content. 1 + Here is some additional line.
2 + Deleted line 1 and added this.
1 This is the original content. 1 Here is some additional line.
2 Deleted line 1 and added this.



Expand All @@ -298,14 +288,14 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode

$ dummy_terminal example.diff n n n n n n n n n t
Operation 10 of 14, 1 hunk
0 additions, 1 removal
Deletion of dir1/file.txt
@@ -1,1 @@ @@ +0,0 @@
1 - some text
@@ -1,1 @@ @@ +1,0 @@
1 some text



Expand All @@ -320,15 +310,15 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode

$ dummy_terminal example.diff n n n n n n n n n n n n t
Operation 13 of 14, 1 hunk
2 additions, 0 removals
Creation of dir2/sample.txt
@@ -0,0 @@ @@ +1,2 @@
1 + some text
2 + lorem ipsum
@@ -1,0 @@ @@ +1,2 @@
1 some text
2 lorem ipsum



Expand All @@ -342,14 +332,14 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode

$ dummy_terminal example.diff n n n n n n n n n n n n n n n t
Operation 14 of 14, 1 hunk
1 addition, 0 removals
Rename with modifications dir1/file.txt to dir2/file.txt
@@ -0,0 @@ @@ +1,1 @@
1 + new text
@@ -1,0 @@ @@ +1,1 @@
1 new text



Expand All @@ -364,15 +354,15 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode


$ dummy_terminal example.diff n n n n n n n n n n n n n n n n t
Operation 14 of 14, 1 hunk
1 addition, 0 removals
Rename with modifications dir1/file.txt to dir2/file.txt
@@ -0,0 @@ @@ +1,1 @@
1 + new text
@@ -1,0 @@ @@ +1,1 @@
1 new text



Expand All @@ -387,7 +377,7 @@ This is a cram test for the new executable.



Type 'h' for help, 'q' to quit, 'n' for next, 'p' for previous, 't' to toggle view mode, 'r' to toggle render mode.
[h]elp [q]uit [n/p]avigate [t]oggle view [r]ender mode



Expand Down
Loading

0 comments on commit 8e9edec

Please sign in to comment.