forked from google/diff-match-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.html
92 lines (73 loc) · 3.95 KB
/
match.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>Diff, Match and Patch: Demo of Match</TITLE>
<SCRIPT SRC="../javascript/diff_match_patch.js"></SCRIPT>
</HEAD>
<BODY>
<H1>Diff, Match and Patch</H1>
<H2>Demo of Match</H2>
<P>Match looks for a pattern within a larger text.
This implementation of match is fuzzy, meaning it can find a match even if the
pattern contains errors and doesn't exactly match what is found in the text.
This implementation also accepts an expected location, near which the match should be found.
The candidate matches are scored based on a) the number of spelling differences between the
pattern and the text and b) the distance between the candidate match and the expected location.
The match distance parameter sets the relative importance of these two metrics.</P>
<FORM action="#" onsubmit="return false">
<H3>Text:</H3>
<TEXTAREA ID="text" STYLE="width: 100%" ROWS=10 onChange="textchange()">'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.
All mimsy were the borogroves,
And the mome raths outgrabe.</TEXTAREA>
<H3>Fuzzy pattern:</H3>
<P><INPUT ID="pattern" SIZE=32 VALUE="slimy tools"> <SPAN ID="maxlengthspan"></SPAN><BR>
Approximate pattern to search for in the text. Due to limitations of the Bitap algorithm, the pattern has a limited length.</P>
<H3>Fuzzy location:</H3>
<P><INPUT ID="loc" SIZE=4 MAXLENGTH=10 VALUE="30"> <SPAN ID="maxtextspan"></SPAN><BR>
Approximately where in the text is the pattern expected to be found?</P>
<H3>Match distance:</H3>
<P><INPUT TYPE="text" SIZE=3 MAXLENGTH=8 VALUE="1000" ID="distance"><BR>
Determines how close the match must be to the fuzzy location (specified above). An exact letter match which is 'distance' characters away from the fuzzy location would
score as a complete mismatch. A distance of '0' requires the match be at the exact location specified, a threshold of '1000'
would require a perfect match to be within 800 characters of the fuzzy location to be found using a 0.8 threshold.</P>
<H3>Match threshold:</H3>
<P><INPUT TYPE="text" SIZE=3 MAXLENGTH=5 VALUE="0.8" ID="threshold"><BR>
At what point does the match algorithm give up. A threshold of '0.0' requires a perfect match (of both letters and location), a threshold of '1.0' would match anything.</P>
<INPUT TYPE="button" onClick="launch()" VALUE="Compute Match">
</FORM>
<DIV ID="outputdiv"></DIV>
<DIV ID="datediv"></DIV>
<SCRIPT>
var dmp = new diff_match_patch();
function launch() {
var text = document.getElementById('text').value;
var pattern = document.getElementById('pattern').value;
var loc = parseInt(document.getElementById('loc').value, 10);
dmp.Match_Distance = parseFloat(document.getElementById('distance').value);
dmp.Match_Threshold = parseFloat(document.getElementById('threshold').value);
var ms_start = (new Date()).getTime();
var match = dmp.match_main(text, pattern, loc);
var ms_end = (new Date()).getTime();
document.getElementById('datediv').innerHTML = 'Time: ' + (ms_end - ms_start) / 1000 + 's';
if (match == -1) {
document.getElementById('outputdiv').innerHTML = 'No match found.';
} else {
var quote = text.substring(match, match + pattern.length);
quote = quote.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
quote = quote.replace(/\n/g, '¶');
document.getElementById('outputdiv').innerHTML = 'Match found at character ' + match +
': <CODE>' + quote + '</' + 'CODE>';
}
}
function textchange() {
document.getElementById('maxtextspan').innerHTML = '(text is currently ' + document.getElementById('text').value.length + ' characters long)';
}
textchange();
document.getElementById('pattern').maxLength = dmp.Match_MaxBits;
document.getElementById('maxlengthspan').innerHTML = '(maxlength in this browser: ' + dmp.Match_MaxBits + ')';
</SCRIPT>
<HR>
Back to <A HREF="https://github.com/google/diff-match-patch">Diff, Match and Patch</A>
</BODY>
</HTML>