nlplispracket

NLP with Racket


I am studying NLP with Racket and Dr. Racket.

I am working with this code:

#lang racket

(define english-1
  '((Initial (1))
    (Final (9))
    (From 1 to 3 by NP)
    (From 1 to 2 by DET)
    (From 2 to 3 by N)
    (From 3 to 4 by BV)
    (From 4 to 5 by ADV)
    (From 4 to 5 by |#|)
    (From 5 to 6 by DET)
    (From 5 to 7 by DET)
    (From 5 to 8 by |#|)
    (From 6 to 7 by ADJ)    
    (From 6 to 6 by MOD)
    (From 7 to 9 by N)
    (From 8 to 8 by MOD)
    (From 8 to 9 by ADJ)
    (From 9 to 4 by CNJ)
    (From 9 to 1 by CNJ)))

(define (getf x y)
  (if (eq? (car x) y)
      (cadr x)
      (getf (cdr x) y)))

(define (initial-nodes network)
  (list-ref (assoc 'Initial network) 1))

(define (final-nodes network)
  (list-ref  (assoc 'Final network) 1))

(define (transitions network)
  (filter (lambda (x) (eq? (car x) 'From)) network))

(define (trans-node transition)
  (getf transition 'From))

(define(trans-newnode transition)
  (getf transition 'to))

(define (trans-label transition)
  (getf transition 'by))

(define abbreviations
  '((NP kim sandy lee)
    (DET a the her)
    (N consumer man woman)
    (BV is was)
    (CNJ and or)
    (ADJ happy stupid)
    (MOD very)
  (ADV often always sometimes)))

(define (recognize network tape)
  ;; returns t if sucessfully recognizes tape - nil otherwise
  (call/cc (lambda (return)
             (define (recognize-next node tape network)
               (if (and (null? tape) (member node (final-nodes network)))
                   (return #t) ; success
                   (for ([transition (transitions network)])
                           ;; try each transition of the network
                           (when (equal? node (trans-node transition)) ; if it starts at the right node
                               (for ([newtape (recognize-move (trans-label transition) tape)])
                                       ;; try each possible new value of tape
                                 (recognize-next (trans-newnode transition) newtape network))))))
             (for ([initialnode (initial-nodes network)])
               (recognize-next initialnode tape network))
             null))) ; failed to recognize

(define (recognize-move label tape)
  (if (or (eq? label (car tape))
          (member (car tape) (assoc label abbreviations)))
      (list (cdr tape))
      (if (eq? label '|#|)
          (list tape)
          null)))

(require racket/trace)
(trace recognize-move)
(recognize-move english-1 '(hahaha))

The code seems to be mostly fine. However, I keep getting a error messaging related to the recognize-move function:

member: not a proper list: #f

And I thought I was dealing with lists... How can I solve this?


Solution

  • The problem is with this form:

    (member (car tape) (assoc label abbreviations))
    

    If assoc does not find anything the result is #f. (member 'anything #f) will not work. In Common Lisp false is the same as an empty list so member on false will work, but not in Scheme. You can perhaps make sure it's a list like this:

    (member (car tape) (or (assoc label abbreviations) '()))