带球拍的NLP
我正在学习NLP和Racket博士 我正在使用此代码:带球拍的NLP,nlp,lisp,racket,Nlp,Lisp,Racket,我正在学习NLP和Racket博士 我正在使用此代码: #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
#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))
代码似乎基本上是好的。但是,我不断收到与识别移动功能相关的错误消息:
member: not a proper list: #f
我以为我在处理清单。。。如何解决此问题?此表单存在问题:
(member (car tape) (assoc label abbreviations))
如果assoc
未找到任何内容,则结果为#f
<代码>(成员“任何东西”#f)都不起作用。在公共Lisp中,false与空列表相同,因此false上的成员
将起作用,但在Scheme中不起作用。您也许可以确定它是这样一个列表:
(member (car tape) (or (assoc label abbreviations) '()))
此表单存在问题:
(member (car tape) (assoc label abbreviations))
如果assoc
未找到任何内容,则结果为#f
<代码>(成员“任何东西”#f)都不起作用。在公共Lisp中,false与空列表相同,因此false上的成员
将起作用,但在Scheme中不起作用。您也许可以确定它是这样一个列表:
(member (car tape) (or (assoc label abbreviations) '()))
这是从CommonLisp翻译过来的代码。在CL中,
nil
为false,也是空列表,()
。在球拍中,#f
为假,与()
不同assoc
如果未找到匹配项,则希望返回false:由于CL双关false和空列表的方式,这意味着(成员…(assoc…)
将始终有效。在Racket中不会:您需要检查assoc是否未能找到匹配项。这是从Common Lisp翻译的代码。在CL中,nil
为false,也是空列表,()
。在球拍中,#f
为假,与()
不同assoc
如果未找到匹配项,则希望返回false:由于CL双关false和空列表的方式,这意味着(成员…(assoc…)
将始终有效。在Racket中不会:您需要检查assoc
是否未能找到匹配项