MODULE PZPrintCands EXPORTS Main; (* Prints in "stdout" a list of candidate matchings with their curvature chains. *) (* Last edited on 1999-09-04 06:35:09 by hcgl *) IMPORT ParseParams, FileRd, Wr, Fmt, Process, OSError, Thread, Stdio; IMPORT PZSymbolChain, PZSegment, PZCandidate, PZMatch; FROM Stdio IMPORT stdout, stderr; FROM PZTypes IMPORT LONG, NAT, BOOL; <* FATAL Wr.Failure, Thread.Alerted, OSError.E *> TYPE Options = RECORD input: TEXT; (* Input candidate file name. *) chainDir: TEXT; (* Directory where chains reside. *) chainPrefix: TEXT; (* Invariant chain file name prefix. *) band: NAT; (* Nominal band width (lambda) for file names. *) pairing: BOOL; (* TRUE to print the matching. *) maxCands: NAT; (* MAX num candidates to print. *) END; PROCEDURE Main() = BEGIN WITH o = GetOptions() DO Wr.PutText(stderr, "PZPrintCands " & o.input & "\n"); IF o.maxCands > 0 THEN WITH candData = PZCandidate.Read(FileRd.Open(o.input & ".can")), cand = candData.c^, curves = PZCandidate.GetCurves(cand)^, chData = PZSymbolChain.ReadAll( o.chainPrefix, o.band, ".cvc", sel := curves, headerOnly := FALSE ) DO PrintCandidates(chData.chain^, cand, o.band, o.maxCands); END; END END; END Main; PROCEDURE GetOptions(): Options = VAR o: Options; BEGIN WITH pp = NEW(ParseParams.T).init(stderr) DO TRY pp.getKeyword("-input"); o.input := pp.getNext(); IF pp.keywordPresent("-chainDir") THEN o.chainDir := pp.getNext() ELSE o.chainDir := "." END; pp.getKeyword("-chainPrefix"); o.chainPrefix := pp.getNext(); pp.getKeyword("-band"); o.band := pp.getNextInt(); o.pairing := pp.keywordPresent("-pairing"); IF pp.keywordPresent("-maxCands") THEN o.maxCands := pp.getNextInt(1) ELSE o.maxCands := 1000 END; pp.finish(); EXCEPT | ParseParams.Error => Wr.PutText(stderr, "Usage: PZPrintCands \\\n"); Wr.PutText(stderr, " -input NAME \\\n"); Wr.PutText(stderr, " [ -chainDir DIR ] -chainPrefix NAME \\\n"); Wr.PutText(stderr, " -band NUMBER \\\n"); Wr.PutText(stderr, " [ -pairing ] \\\n"); Wr.PutText(stderr, " [ -maxCands NUMBER ]\n"); Process.Exit(1); END; END; RETURN o END GetOptions; PROCEDURE PrintCandidates ( READONLY ch: ARRAY OF PZSymbolChain.ReadData; READONLY cand : PZCandidate.List; band: NAT; maxCands: NAT; )= BEGIN WITH nCands = NUMBER(cand) DO Wr.PutText(stdout, "=== band = " & Fmt.Int(band) & " ===\n"); FOR i := 0 TO MIN(nCands, maxCands)-1 DO WITH c = cand[i] DO Wr.PutText(stdout, "cand[" & Fmt.Int(i) & "]\n"); PrintCandidate(stdout, c, ch[c.seg[0].cvx].c, ch[c.seg[1].cvx].c); FOR i := 1 TO 80 DO Wr.PutChar(stdout, '-') END; Wr.PutText(stdout, "\n"); END; END END; END PrintCandidates; PROCEDURE PrintCandidate( wr: Wr.T; READONLY cand: PZCandidate.T; READONLY ch0, ch1: REF PZSymbolChain.T; pairing: BOOL := TRUE; ) = <* FATAL Wr.Failure, Thread.Alerted *> PROCEDURE PrintHalf( j: [0..1]; s: PZSegment.T; READONLY ch: REF PZSymbolChain.T; ) = BEGIN Wr.PutText(wr, " "); Wr.PutText(wr, "seg [" & Fmt.Int(j) & "]: "); PZSegment.Print(wr, s); IF ch # NIL THEN <* ASSERT s.tot = NUMBER(ch^) *> Wr.PutText(wr, " = "); PZSymbolChain.PrintSegment(wr, ch^, s) END; Wr.PutText(wr, "\n"); END PrintHalf; BEGIN Wr.PutText(wr, "curves = ("); Wr.PutText(wr, Fmt.Int(cand.seg[0].cvx) & "," & Fmt.Int(cand.seg[1].cvx)); Wr.PutText(wr, ") "); Wr.PutText(wr,"mismatch = " & FLR(cand.mismatch, 6, 2)); Wr.PutText(wr, " "); Wr.PutText(wr,"length = " & FLR(cand.length, 9, 4)); Wr.PutText(wr, " "); Wr.PutText(wr,"matchedLength = " & FLR(cand.matchedLength, 9, 4) & "\n"); PrintHalf(0, cand.seg[0], ch0); PrintHalf(1, cand.seg[1], ch1); IF pairing AND cand.pm # NIL THEN PZMatch.WritePacked(wr, cand.pm^); Wr.PutText(wr, "\n"); END; FOR i := 1 TO 80 DO Wr.PutChar(wr, '-') END; Wr.PutText(wr, "\n"); END PrintCandidate; PROCEDURE FLR(x: LONG; w, p: NAT): TEXT = BEGIN RETURN Fmt.Pad(Fmt.LongReal(x, Fmt.Style.Fix, prec := p), w) END FLR; BEGIN Main() END PZPrintCands. (* Copyright © 2001 Universidade Estadual de Campinas (UNICAMP). Authors: Helena C. G. Leitão and Jorge Stolfi. This file can be freely distributed, used, and modified, provided that this copyright and authorship notice is preserved, and that any modified versions are clearly marked as such. This software has NO WARRANTY of correctness or applicability for any purpose. Neither the authors nor their employers chall be held responsible for any losses or damages that may result from its use. *)