From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mark Skilbeck Newsgroups: gmane.lisp.guile.user Subject: Re: letter occurence in a text Date: Wed, 23 May 2012 00:57:01 +0100 Message-ID: <20120522235701.GB2326@mark-laptop> References: <33889562.post@talk.nabble.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1337731046 25255 80.91.229.3 (22 May 2012 23:57:26 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 22 May 2012 23:57:26 +0000 (UTC) Cc: Guile-user@gnu.org To: nrichard Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed May 23 01:57:25 2012 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SWyx7-0006R7-OK for guile-user@m.gmane.org; Wed, 23 May 2012 01:57:21 +0200 Original-Received: from localhost ([::1]:43518 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWyx7-0006hJ-CX for guile-user@m.gmane.org; Tue, 22 May 2012 19:57:21 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:33941) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWyws-00069N-VF for Guile-user@gnu.org; Tue, 22 May 2012 19:57:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SWywr-00043n-54 for Guile-user@gnu.org; Tue, 22 May 2012 19:57:06 -0400 Original-Received: from li357-97.members.linode.com ([178.79.188.97]:49338 helo=mail.iammark.us) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWywq-00043P-VV for Guile-user@gnu.org; Tue, 22 May 2012 19:57:05 -0400 Original-Received: from mail.iammark.us (host86-142-81-113.range86-142.btcentralplus.com [86.142.81.113]) by mail.iammark.us (Postfix) with ESMTPSA id 681B0AB92; Wed, 23 May 2012 00:57:02 +0100 (BST) Content-Disposition: inline In-Reply-To: <33889562.post@talk.nabble.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 178.79.188.97 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:9472 Archived-At: 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.