| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| 2 |
<HTML> |
| 3 |
<HEAD> |
| 4 |
<TITLE>Diff, Match and Patch: Demo of Match</TITLE> |
| 5 |
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript" SRC="../javascript/diff_match_patch.js"></SCRIPT> |
| 6 |
</HEAD> |
| 7 |
|
| 8 |
<BODY> |
| 9 |
<H1>Diff, Match and Patch</H1> |
| 10 |
<H2>Demo of Match</H2> |
| 11 |
|
| 12 |
<P>Match looks for a pattern within a larger text. |
| 13 |
This implementation of match is fuzzy, meaning it can find a match even if the |
| 14 |
pattern contains errors and doesn't exactly match what is found in the text. |
| 15 |
This implementation also accepts an expected location, near which the match should be found. |
| 16 |
The candidate matches are scored based on a) the number of spelling differences between the |
| 17 |
pattern and the text and b) the distance between the candidate match and the expected location. |
| 18 |
The match distance parameter sets the relative importance of these two metrics.</P> |
| 19 |
|
| 20 |
<FORM action="#" onsubmit="return false"> |
| 21 |
<H3>Text:</H3> |
| 22 |
<TEXTAREA ID="text" STYLE="width: 100%" ROWS=10 onChange="textchange()">'Twas brillig, and the slithy toves |
| 23 |
Did gyre and gimble in the wabe. |
| 24 |
All mimsy were the borogroves, |
| 25 |
And the mome raths outgrabe.</TEXTAREA> |
| 26 |
|
| 27 |
<H3>Fuzzy pattern:</H3> |
| 28 |
<P><INPUT ID="pattern" SIZE=32 VALUE="slimy tools"> <SPAN ID="maxlengthspan"></SPAN><BR> |
| 29 |
Aproximate pattern to search for in the text. Due to limitations of the Bitap algorithm, the pattern has a limited length.</P> |
| 30 |
|
| 31 |
<H3>Fuzzy location:</H3> |
| 32 |
<P><INPUT ID="loc" SIZE=4 MAXLENGTH=10 VALUE="30"> <SPAN ID="maxtextspan"></SPAN><BR> |
| 33 |
Aproximately where in the text is the pattern expected to be found?</P> |
| 34 |
|
| 35 |
<H3>Match distance:</H3> |
| 36 |
<P><INPUT TYPE="text" SIZE=3 MAXLENGTH=8 VALUE="1000" ID="distance"><BR> |
| 37 |
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 |
| 38 |
score as a complete mismatch. A distance of '0' requires the match be at the exact location specified, a threshold of '1000' |
| 39 |
would require a perfect match to be within 800 characters of the fuzzy location to be found using a 0.8 threshold.</P> |
| 40 |
|
| 41 |
<H3>Match threshold:</H3> |
| 42 |
<P><INPUT TYPE="text" SIZE=3 MAXLENGTH=5 VALUE="0.8" ID="threshold"><BR> |
| 43 |
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> |
| 44 |
|
| 45 |
<INPUT TYPE="button" onClick="launch()" VALUE="Compute Match"> |
| 46 |
</FORM> |
| 47 |
|
| 48 |
<DIV ID="outputdiv"></DIV> |
| 49 |
|
| 50 |
<DIV ID="datediv"></DIV> |
| 51 |
|
| 52 |
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript"> |
| 53 |
var dmp = new diff_match_patch(); |
| 54 |
|
| 55 |
function launch() { |
| 56 |
var text = document.getElementById('text').value; |
| 57 |
var pattern = document.getElementById('pattern').value; |
| 58 |
var loc = parseInt(document.getElementById('loc').value, 10); |
| 59 |
|
| 60 |
dmp.Match_Distance = parseFloat(document.getElementById('distance').value); |
| 61 |
dmp.Match_Threshold = parseFloat(document.getElementById('threshold').value); |
| 62 |
|
| 63 |
var ms_start = (new Date()).getTime(); |
| 64 |
var match = dmp.match_main(text, pattern, loc); |
| 65 |
var ms_end = (new Date()).getTime(); |
| 66 |
|
| 67 |
document.getElementById('datediv').innerHTML = 'Time: ' + (ms_end - ms_start) / 1000 + 's'; |
| 68 |
if (match == -1) { |
| 69 |
document.getElementById('outputdiv').innerHTML = 'No match found.'; |
| 70 |
} else { |
| 71 |
var quote = text.substring(match, match + pattern.length); |
| 72 |
quote = quote.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); |
| 73 |
quote = quote.replace(/\n/g, '¶'); |
| 74 |
document.getElementById('outputdiv').innerHTML = 'Match found at character ' + match + |
| 75 |
': <CODE>' + quote + '</' + 'CODE>'; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
function textchange() { |
| 80 |
document.getElementById('maxtextspan').innerHTML = '(text is currently ' + document.getElementById('text').value.length + ' characters long)'; |
| 81 |
} |
| 82 |
|
| 83 |
textchange(); |
| 84 |
document.getElementById('pattern').maxLength = dmp.Match_MaxBits; |
| 85 |
document.getElementById('maxlengthspan').innerHTML = '(maxlength in this browser: ' + dmp.Match_MaxBits + ')'; |
| 86 |
</SCRIPT> |
| 87 |
|
| 88 |
<HR> |
| 89 |
Back to <A HREF="http://code.google.com/p/google-diff-match-patch/">Diff, Match and Patch</A> |
| 90 |
|
| 91 |
</BODY> |
| 92 |
</HTML> |