home *** CD-ROM | disk | FTP | other *** search
-
- (**** Gewichtete Levenshtein-Distanz ****)
- (**** mit Berücksichtigung von Wildcards ****)
- (**** (geschwindigkeitsoptimierte Version) ****)
- (**** Ergebnis: WLD, falls WLD <= limit ****)
- (**** maxlen sonst ****)
- (**** Autor : Jörg Michael, Hannover ****)
- (**** Datum : 4. Februar 1993 ****)
-
- const maxlen = 50;
- p0 = 1; (**** Ersetzen ****)
- q0 = 1; (**** Einfügen ****)
- r0 = 1; (**** Löschen ****)
-
- type maxstring = string [maxlen];
- var wort,muster, zahl: maxstring;
- limit : integer;
-
- function MIN (x,y,z: integer): integer;
- begin
- if (x < y) then y := x;
- if (y < z) then MIN := y else MIN := z
- end;
-
-
- function isnormal (c: char): boolean;
- begin
- if (c <> '*') and (c <> '?')
- then isnormal := true else isnormal := false
- end;
-
- function WLD (var wort,muster: maxstring; limit: integer):integer;
- var c: char;
- i,j,lw,lm: integer;
- pp,p,q,r: integer;
- d1,d2,spmin: integer;
- d: array [0..maxlen] of integer;
-
- begin
- lw := length (wort);
- lm := length (muster);
- spmin := 0;
-
- if (limit = 0) then
- begin
- (**** Anfangsbuchstaben vergleichen ****)
- if (lm >= 1) and (lw >= 1)
- and (isnormal (muster[1])) and (muster[1] <> wort[1])
- then spmin := maxlen
- end;
-
- if (limit = 1) then
- begin
- (**** die ersten zwei Buchstaben vergleichen ****)
- if (lm >= 2) and (lw >= 2)
- and (isnormal (muster[1])) and (isnormal (muster[2]))
- and (muster[1] <> wort[1]) and (muster[1] <> wort[2])
- and (muster[2] <> wort[1]) and (muster[2] <> wort[2])
- then spmin := maxlen
- end;
-
- if (spmin <= limit) then
- begin
- (**** Sternchen zählen ****)
- j := 0;
- for i := 1 to lm do
- begin
- if (muster[i] = '*') then j := j+1
- end;
-
- (**** Wortlängen prüfen ****)
- i := lm-j -lw;
- if (i * q0 > limit) or ((j = 0) and (i * r0 < -limit))
- then spmin := maxlen
- end;
-
- if (spmin <= limit) then
- begin
- (**** Anfangswerte berechnen ****)
- if (lm = 0) then
- begin
- for i := 0 to lw do d[i] := i * r0
- end
- else if (muster[1] = '*') then
- begin
- for i := 0 to lw do d[i] := 0
- end
-
- else
- begin
- if (muster[1] = '?') then p := 0
- else p := MIN (p0, p0, r0+q0);
- d[0] := q0;
- d[1] := q0;
- d[2] := q0;
-
- for i := 1 to lw do
- begin
- if (wort[i] = muster[1]) then p := 0;
- d[i] := (i-1) * r0 + p
- end;
- spmin := MIN (d[0], d[1], d[2])
- end
- end;
-
- (**** Distanzmatrix durchrechnen ****)
- j := 1;
- while (j < lm) and (spmin <= limit) do
- begin
- j := j+1;
- c := muster[j];
- if (c = '*') or (c = '?') then p := 0 else p := p0;
- if (c = '*') then q := 0 else q := q0;
- if (c = '*') then r := 0 else r := r0;
- d2 := d[0];
- d[0] := d[0] + q;
- spmin := d[0];
-
- for i := 1 to lw do
- begin
- (**** d[i] := Minimum dreier Zahlen ****)
- d1 := d2;
- d2 := d[i];
- if (wort[i] = c) then pp := 0 else pp := p;
- d[i] := MIN (d1+pp, d2+q, d[i-1]+r);
- if (d[i] < spmin) then spmin := d[i]
- end
- end;
-
- if (spmin <= limit) and (d[lw] <= limit)
- then WLD := d[lw] else WLD := maxlen
- end;
-
- begin
- (**** Hauptprogramm ****)
- write ('Wort : '); readln (wort);
- write ('Muster : '); readln (muster);
- write ('Limit : '); readln (zahl);
- limit := ord (zahl[1]) - ord('0');
- write ('Distanz: ', WLD (wort,muster, limit))
- end.
-
-