guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: letter occurence in a text


From: Mark Skilbeck
Subject: Re: letter occurence in a text
Date: Wed, 23 May 2012 00:57:01 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

On Tue, May 22, 2012 at 07:26:09AM -0700, nrichard wrote:
> 
> hello my problem is to count occurence of letter in a text and come out with
> an assoc-list like
> '((a.16) (b.10) ... (z.5))
> i started a fee function but they don't work so please help me
> 
> ;; an alist
> (define lettre '((""."") ) )
> (define lettre2 '(("a". 1) ("b". 1) ("c". 1) ) )
> 
> ;; this function take a key and an alist
> ;; add the key to the alist if it is not a space,a newline,and does not
> already exist
> ;; update the value of the given key
> (define (lettre-test x alist)
>       (cond ((and 
>               ( and (not (char=?  x #\space))(not (char=?  x #\newline)))   
>                               (eq? (assoc (make-string 1 x) alist) #f))  
>               (set! alist
>                       (assoc-set! alist (make-string 1 x) 0 )))
>               ((char=?  x #\space) '())
>               ((char=?  x #\newline) '())              
>               (else
>               (set! alist   
>                               (assoc-set! alist (make-string 1 x) (+ 
> (assoc-ref alist
> (make-string 1 x)) 1))))))
>       
> ;; this function open a file
> ;; read the caracter and count the occurence of each character        
> (define (compte-char source alist)    
> (let ((p (open-port source)))
>   (let f ((x (read-char p)))
>     (cond ((eof-object? x)
>         (begin
>           (close-input-port p)
>           '()))        
>          (else
>         (cons (lettre-test x alist) (f (read-char p))))))))

Hi!

I did a short frequency-analysis function some time ago and after a
little digging found it. With a little haqqing you could modify it to
your needs:

(code)
;; Frequency analysis. Not Frequently Analed.

(define (frequency-analysis text)
  (if (null? text)
      '()
      (let loop ((table (list (cons (car text) 1)))
                 (text (cdr text)))
        (cond ((null? text) table)
              ((not (assoc (car text) table))
               (loop (append table
                             (list (cons (car text) 1)))
                     (cdr text)))
              (else
               (set-cdr! (assoc (car text) table)
                         (+ (cdr (assoc (car text) table)) 1))
               (loop table (cdr text)))))))
(/code)

I should probably talk you through the code. But, if think it's simple
enough and I am drunk enough not to require it!

Good day!

- mgsk.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]