unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* letter occurence in a text
@ 2012-05-22 14:26 nrichard
  2012-05-22 23:57 ` Mark Skilbeck
  2012-05-23 13:57 ` gregory benison
  0 siblings, 2 replies; 3+ messages in thread
From: nrichard @ 2012-05-22 14:26 UTC (permalink / raw)
  To: Guile-user


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))))))))
-- 
View this message in context: http://old.nabble.com/letter-occurence-in-a-text-tp33889562p33889562.html
Sent from the Gnu - Guile - User mailing list archive at Nabble.com.




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: letter occurence in a text
  2012-05-22 14:26 letter occurence in a text nrichard
@ 2012-05-22 23:57 ` Mark Skilbeck
  2012-05-23 13:57 ` gregory benison
  1 sibling, 0 replies; 3+ messages in thread
From: Mark Skilbeck @ 2012-05-22 23:57 UTC (permalink / raw)
  To: nrichard; +Cc: Guile-user

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.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: letter occurence in a text
  2012-05-22 14:26 letter occurence in a text nrichard
  2012-05-22 23:57 ` Mark Skilbeck
@ 2012-05-23 13:57 ` gregory benison
  1 sibling, 0 replies; 3+ messages in thread
From: gregory benison @ 2012-05-23 13:57 UTC (permalink / raw)
  To: nrichard; +Cc: Guile-user

On Tue, May 22, 2012 at 7:26 AM, nrichard <nnomekoorichard@gmail.com> 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))

Why store the alist keys as one-character strings, rather than just as
characters?  Storing as characters would be simpler:

;; Given an alist 'lst' containing character counts and character 'c',
;; return an alist with the count of 'c' incremented (set to 1 if it
doesn't exist).
(define (lettre-test c lst)
  (let ((current (assoc-ref lst c)))
     (assoc-set! lst c (+ 1 (or current 0)))))

Character frequency analysis can be performed with a "fold" operation:

> (fold lettre-test '() (string->list "hello, world!"))
((#\! . 1)
 (#\d . 1)
 (#\r . 1)
 (#\w . 1)
 (#\space . 1)
 (#\, . 1)
 (#\o . 2)
 (#\l . 3)
 (#\e . 1)
 (#\h . 1))

I think it would be best to separate the filtering for alphabetic
chars from the "lettre-test" function; they're separate ideas:

> (fold lettre-test '() (filter char-alphabetic? (string->list "hello, world!")))
((#\d . 1)
 (#\r . 1)
 (#\w . 1)
 (#\o . 2)
 (#\l . 3)
 (#\e . 1)
 (#\h . 1))

The drawback of this solution, as currently written, is that it can't
lazily read a file; you'd have to read the entire file into a string
first.  It should be possible to modify this to use streams rather
than lists, though.


-- 
Greg Benison <gbenison@gmail.com>
[blog] http://gcbenison.wordpress.com
[twitter] @gcbenison



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-05-23 13:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-22 14:26 letter occurence in a text nrichard
2012-05-22 23:57 ` Mark Skilbeck
2012-05-23 13:57 ` gregory benison

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).