Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active Sep 1, 2022
Embed
What would you like to do?
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