From 939d7b2d541fab7749986797f5903036798835bb Mon Sep 17 00:00:00 2001 From: "D. Ben Knoble" Date: Tue, 26 Sep 2023 15:12:05 -0400 Subject: [PATCH 1/2] prototype comparison of anonymized appraisal files Outputs a CSV for import into spreadsheet tools. --- dia/compare-anonymous-appraisal.rkt | 50 +++++++++++++++++++++++++++++ dia/info.rkt | 1 + dia/tree-parser.rkt | 13 ++++++++ 3 files changed, 64 insertions(+) create mode 100644 dia/compare-anonymous-appraisal.rkt diff --git a/dia/compare-anonymous-appraisal.rkt b/dia/compare-anonymous-appraisal.rkt new file mode 100644 index 0000000..80f5231 --- /dev/null +++ b/dia/compare-anonymous-appraisal.rkt @@ -0,0 +1,50 @@ +#lang racket + +;; Run with `racket compare-anonymous-appraisal.rkt` +;; or `racket -l- abe/compare-anonymous-appraisal` + +(require "tree-parser.rkt") + +(define (get-tree ip) + (when (regexp-match-peek #rx"#lang" ip) + (read-line ip 'any)) + (read-anonymous-attribution-tree2 ip)) + +(define (make-rows trees) + (let ([trees (map flatten trees)]) + (reverse + (let loop ([result '()] + [trees trees]) + (match trees + [(list '() ...) result] + [(list (cons (? string? x) rest) ...) + #:when (apply equal? x) + (loop (cons (list (car x)) result) + rest)] + [(list (cons (? number? n) rest) ...) + (loop (cons (append (car result) n) + (cdr result)) + rest)]))))) + +(define (make-table headers trees) + (cons headers (make-rows trees))) + +(module+ main + (require csv-writing) + (command-line + #:usage-help + "" + "Compares anonymous appraisals in ." + "Outputs CSV, one row per appraisal item." + "" + "All should be in either #lang abe/attribution2" + "or #lang abe/ideas2 format and should have the same items" + "in the same order." + "" + "Example:" + " compare-anonymous-appraisal /path/to/appraisals/*/capital.md" + #:args files + (display-table + (make-table (cons "Item" files) + (map (λ (f) (call-with-input-file f get-tree)) + files))))) diff --git a/dia/info.rkt b/dia/info.rkt index 6b8f9b3..75f2a9b 100644 --- a/dia/info.rkt +++ b/dia/info.rkt @@ -3,6 +3,7 @@ (define version "0.0") (define collection "abe") (define deps '("base" + "csv-writing" "qi-lib" "relation-lib")) (define build-deps '()) diff --git a/dia/tree-parser.rkt b/dia/tree-parser.rkt index 8106daf..a4043ea 100644 --- a/dia/tree-parser.rkt +++ b/dia/tree-parser.rkt @@ -2,6 +2,7 @@ (provide read-attribution-tree read-attribution-tree2 + read-anonymous-attribution-tree2 read-idea-attribution-tree read-idea-antecedents-tree) @@ -32,6 +33,12 @@ corresponding kind of tree. (make-indent-based-tree (flow (regexp-replace* #px"[[:blank:]]" _ " "))) (tree-map label->attribution leaf->attribution2))) +(define-flow (read-anonymous-attribution-tree2 ip) + (~> port->lines + (filter non-empty-string? _) + (make-indent-based-tree (flow (regexp-replace* #px"[[:blank:]]" _ " "))) + (tree-map label->attribution leaf->anonymous-attribution2))) + (define-flow (read-idea-attribution-tree ip) (~> port->lines (filter non-empty-string? _) @@ -136,6 +143,12 @@ on format. (if (~> cdr null?) car _)) attribution)])) +(define (leaf->anonymous-attribution2 x) + (match x + [(regexp #px"^\\s*\\* (.*)\\[([[:digit:].]+)%\\]" + (list _ line (app string->number attribution))) + (cons line attribution)])) + (define (label->attribution x) (match x [(regexp #px"^\\s*\\* (.*) \\[([[:digit:].]+)%\\]" From 69da9eeaf9905ade256f4220f478d329fc733320 Mon Sep 17 00:00:00 2001 From: "D. Ben Knoble" Date: Wed, 4 Oct 2023 16:37:02 -0400 Subject: [PATCH 2/2] fix apply: equal? is not variadic --- dia/compare-anonymous-appraisal.rkt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dia/compare-anonymous-appraisal.rkt b/dia/compare-anonymous-appraisal.rkt index 80f5231..ed6a894 100644 --- a/dia/compare-anonymous-appraisal.rkt +++ b/dia/compare-anonymous-appraisal.rkt @@ -18,7 +18,7 @@ (match trees [(list '() ...) result] [(list (cons (? string? x) rest) ...) - #:when (apply equal? x) + #:when (apply string=? x) (loop (cons (list (car x)) result) rest)] [(list (cons (? number? n) rest) ...)