Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active September 1, 2022 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dogweather/ab2b7a2fdc8ab0c1a586072f0daa897c to your computer and use it in GitHub Desktop.
Save dogweather/ab2b7a2fdc8ab0c1a586072f0daa897c to your computer and use it in GitHub Desktop.
Racket implementation of string operations
#lang racket
(define (clean-up s)
(first (split-into-sentences (fix-hyphenation (fix-whitespace s)))))
(define (fix-whitespace s)
(string-replace s "\n" " "))
(define (fix-hyphenation s)
(string-replace s "- " ""))
(define (split-into-sentences s)
(map ensure-ends-with-period (string-split s ". ")))
(define (ensure-ends-with-period s)
(if (char=? (string-ref s (- (string-length s) 1)) #\.)
s
(string-append s ".")))
(module+ test
(require rackunit)
(check-equal? (fix-whitespace "new\nline") "new line")
(check-equal? (fix-hyphenation "auto- mobile") "automobile")
(check-equal? (clean-up "Relating to the state transient lodging tax; creating\nnew provisions; amending ORS 284.131 and\n320.305; prescribing an effective date; and pro-\nviding for revenue raising that requires approval\nby a three-fifths majority.\nWhereas Enrolled House Bill 2267 (chapter 818,")
"Relating to the state transient lodging tax; creating new provisions; amending ORS 284.131 and 320.305; prescribing an effective date; and providing for revenue raising that requires approval by a three-fifths majority."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment