#lang racket ; file: a02io.rkt ; purpose: starter for assignment 02 ; author: Robert Keller (require htdp/testing) ;; go is a top-level user command. It reads the lines from input, ;; as one title per line, computes a KWIC index, and displays the result. ;; These definitions define the width of text on the left and right used by go. (define go-left-width 30) (define go-right-width 30) (define go-noise '(a am an and are by for if in is of the those to)) (define (go) (make-index go-left-width go-right-width go-noise)) ; Example: If the input is the five lines below (without the semicolons): ;It is easier to fight for one's principles than to live up to them. ;The only normal people are the ones you don't know very well. ;No good deed goes unpunished. ;I either want less corruption, or more chance to participate in it. ;My opinions may have changed, but not the fact that I am right. ; ;Then the output displayed is as follows (without semicolons): ; ; My opinions may have changed, but not the fact that I am rig : 4 ; want less corruption, or more chance to participate in it. : 3 ; My opinions may have changed, but not the fact that : 4 ; I either want less corruption, or more chance to : 3 ; No good deed goes unpunished. : 2 ;normal people are the ones you don't know very well. : 1 ; It is easier to fight for one's prin : 0 ; I either want less corruption, o : 3 ; may have changed, but not the fact that I am right. : 4 ; It is easier to fight for one's principles tha : 0 ; No good deed goes unpunished. : 2 ; No good deed goes unpunished. : 2 ; My opinions may have changed, but not the fact : 4 ;changed, but not the fact that I am right. : 4 ; I either want less corruption, : 3 ; It is easier to fight for one' : 0 ; more chance to participate in it. : 3 ; people are the ones you don't know very well. : 1 ; I either want less corruption, or more chanc : 3 ;t for one's principles than to live up to them. : 0 ; My opinions may have changed, but not the : 4 ;ither want less corruption, or more chance to participate in : 3 ; My opinions may have changed, : 4 ; No good deed goes unpunished. : 2 ; The only normal people are the ones you : 1 ;opinions may have changed, but not the fact that I am right. : 4 ; It is easier to fight for one's principles than to live : 0 ;The only normal people are the ones you don't know very well. : 1 ; The only normal people are the one : 1 ; My opinions may have changed, but : 4 ;I either want less corruption, or more chance to participate : 3 ; corruption, or more chance to participate in it. : 3 ; The only normal people are the ones you don't : 1 ;t is easier to fight for one's principles than to live up to : 0 ;ed, but not the fact that I am right. : 4 ; to fight for one's principles than to live up to them. : 0 ;have changed, but not the fact that I am right. : 4 ; principles than to live up to them. : 0 ; No good deed goes unpunished. : 2 ; one's principles than to live up to them. : 0 ;le are the ones you don't know very well. : 1 ; I either want less corruption, or more : 3 ;e the ones you don't know very well. : 1 ;nly normal people are the ones you don't know very well. : 1 ; ;0 : It is easier to fight for one's principles than to live up to them. ;1 : The only normal people are the ones you don't know very well. ;2 : No good deed goes unpunished. ;3 : I either want less corruption, or more chance to participate in it. ;4 : My opinions may have changed, but not the fact that I am right. ;; make-index makes a KWIC inde by reading lines from input. ;; left and right are widths. ;; noise is a list of symbols to be interpreted as noise words. (define (make-index left right noise) (let ( (lines (read-input)) ) (begin (display-lines (format left right (kwic noise lines)))) (newline) (display-numbered lines 0))) ;; read-input reads input line-by-line and returns a list ;; of the lines read, each as a separate string. (define (read-input) (read-input-helper '())) ;; read-input-helper reads a line and conses it to the ;; accumulated-lines. When end-of-file is reached, it returns ;; the lines as a list, in the order they were read. (define (read-input-helper accumulated-lines) (let ( (line-read (read-line)) ) (if (eof-object? line-read) (reverse accumulated-lines) (read-input-helper (cons line-read accumulated-lines))))) ;; display-lines displays the lines of the quick index. ;; It expects a list of strings as an argument and displays one string per line. (define (display-lines lines) (if (null? lines) #t (begin (display (first lines)) (newline) (display-lines (rest lines))))) ;; display-numbered displays each line in list lines preceded by a number, ;; beginning with the number argument and continuing with next number, etc. (define (display-numbered lines number) (if (null? lines) #t (begin (display number) (display " : ") (display (first lines)) (newline) (display-numbered (rest lines) (+ 1 number))))) ;; kwic is the main API function ;; Given a list of titles, it creates a list of triples, and sorts them by ;; right-hand components. ;; It then drops any triples, the right-hand component of which begins with ;; a noise word. It then converts the lists of words to single strings. (define (kwic noise list-of-titles) '(to be developed by student)) (check-expect (kwic '(the to is it thy) '("It is easier to fight." "The only normal people." "Love thy neighbor." )) '(("easier to fight." "It is" 0) ("fight." "It is easier to" 0) ("Love thy neighbor." "" 2) ("neighbor." "Love thy" 2) ("normal people." "The only" 1) ("only normal people." "The" 1) ("people." "The only normal" 1))) ;; format is the formatting function in the API (define (format left right triples) '(to be developed by student)) (generate-report)