avoid a list operation

main
Matthew Butterick 8 years ago
parent 635a8fa41e
commit bb00063f54

@ -12,19 +12,29 @@
(define default-joiner #\u00AD)
(define (trim-points points)
;; for point list generated from a pattern,
;; drop first two elements because they represent hyphenation weight
;; before the starting "." and between "." and the first letter.
;; drop last element because it represents hyphen after last "."
;; after you drop these two, then each number corresponds to
;; whether a hyphen goes after that letter.
(drop-right (drop points 2) 1))
;; Convert the hyphenated pattern into a point array for use later.
(define (convert-exception-word exception)
(define (make-key x) (format ".~a." (string-replace x "-" "")))
(define (make-value x)
(append (list 0) (map (λ(x) (if (equal? x "-") 1 0)) (regexp-split #px"\\p{L}" x)) (list 0)))
;; use list instead of cons because value is itself a list, and consing will muddy the waters
(list (make-key exception) (make-value exception)))
(list (make-key exception) (trim-points (make-value exception))))
(module+ test
(check-equal? (convert-exception-word "snôw-man") '(".snôwman." (0 0 0 0 0 1 0 0 0 0)))
(check-equal? (convert-exception-word "snôwman") '(".snôwman." (0 0 0 0 0 0 0 0 0 0)))
(check-equal? (convert-exception-word "sn-ôw-ma-n") '(".snôwman." (0 0 0 1 0 1 0 1 0 0))))
(check-equal? (convert-exception-word "snôw-man") '(".snôwman." (0 0 0 1 0 0 0)))
(check-equal? (convert-exception-word "snôwman") '(".snôwman." (0 0 0 0 0 0 0)))
(check-equal? (convert-exception-word "sn-ôw-ma-n") '(".snôwman." (0 1 0 1 0 1 0))))
(define (add-exception-word word)
@ -90,7 +100,6 @@
(define (make-points word)
(define word-with-dots (format ".~a." (string-downcase word)))
(define max-pattern
(hash-ref! (current-word-cache) word-with-dots
(λ () ; compute pattern when missing from cache
(define word-length (string-length word-with-dots))
@ -107,15 +116,7 @@
(define left-zeroes (make-list start 0))
(define right-zeroes (make-list (- (add1 word-length) (length partial-pattern) start) 0))
(append left-zeroes partial-pattern right-zeroes)))
(calculate-max-pattern (cons default-zero-pattern matching-patterns)))))
;; for point list,
;; drop first two elements because they represent hyphenation weight
;; before the starting "." and between "." and the first letter.
;; drop last element because it represents hyphen after last "."
;; after you drop these two, then each number corresponds to
;; whether a hyphen goes after that letter.
(drop-right (drop max-pattern 2) 1))
(trim-points (calculate-max-pattern (cons default-zero-pattern matching-patterns))))))
;; Find hyphenation points in a word. This is not quite synonymous with syllables.

Loading…
Cancel
Save