我正在与Racket合作开展一个NLP项目。
此代码工作正常。但是,我希望我能追踪它。
我在跟踪generate-next函数的行为时遇到了问题。
那是我的代码:
#lang racket
(require racket/trace)
(require rackunit)
(define english-1
'((Initial (1))
(Final (9))
(From 1 to 1 by NP)
(From 1 to 2 by DET)
(From 1 to 3 by NP)
(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)
(From 9 to 2 by CNJ)
(From 9 to 3 by CNJ)
(From 9 to 6 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 (null? tape)
(if (member node (final-nodes network))
(return #t)
(return '())); 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)))))(trace recognize-next))
(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) (or (assoc label abbreviations) '())))
(list (cdr tape))
(if (eq? label '|#|)
(list tape)
null)))
我打电话的时候:
(recognize english-1 '(sandy is a happy woman))
Trace工作正常,我有预期的输出:
>(recognize-next
3
'(is a happy woman)
'((Initial (1))
(Final (9))
(From 1 to 1 by NP)
(From 1 to 2 by DET)
(From 1 to 3 by NP)
(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)
(From 9 to 2 by CNJ)
(From 9 to 3 by CNJ)
(From 9 to 6 by CNJ)))
> (recognize-next
4
'(a happy woman)
'((Initial (1))
(Final (9))
(From 1 to 1 by NP)
(From 1 to 2 by DET)
(From 1 to 3 by NP)
(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)
(From 9 to 2 by CNJ)
(From 9 to 3 by CNJ)
(From 9 to 6 by CNJ)))
> >(recognize-next
5
'(a happy woman)
'((Initial (1))
(Final (9))
(From 1 to 1 by NP)
(From 1 to 2 by DET)
(From 1 to 3 by NP)
(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)
(From 9 to 2 by CNJ)
(From 9 to 3 by CNJ)
(From 9 to 6 by CNJ)))
> > (recognize-next
6
'(happy woman)
'((Initial (1))
(Final (9))
(From 1 to 1 by NP)
(From 1 to 2 by DET)
(From 1 to 3 by NP)
(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)
(From 9 to 2 by CNJ)
(From 9 to 3 by CNJ)
(From 9 to 6 by CNJ)))
> > >(recognize-next
7
'(woman)
'((Initial (1))
(Final (9))
(From 1 to 1 by NP)
(From 1 to 2 by DET)
(From 1 to 3 by NP)
(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)
(From 9 to 2 by CNJ)
(From 9 to 3 by CNJ)
(From 9 to 6 by CNJ)))
> > > (recognize-next
9
'()
'((Initial (1))
(Final (9))
(From 1 to 1 by NP)
(From 1 to 2 by DET)
(From 1 to 3 by NP)
(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)
(From 9 to 2 by CNJ)
(From 9 to 3 by CNJ)
(From 9 to 6 by CNJ)))
#t
然而,当试图追踪它不在网络上的东西时,跟踪有一种奇怪的行为。例如,当我打电话时:
(recognize english-1 '(not defined on the network))
输出是'(),这是正确的。但我期待的信息比以下更详细:
'()
我对将命令放在哪里(跟踪识别 - 下一个)几乎存有疑问。我把它放在识别 - 下一个定义的结尾之前。不确定这是否合适。
答案 0 :(得分:0)
trace
表单修改绑定本身以插入跟踪,但通常不是您想直接使用的。相反,使用trace-define
,trace-lambda
或trace-let
通常更容易。移除您对(trace recognize-next)
的直接使用,并将recognize-next
的定义替换为使用trace-define
的定义:
(trace-define (recognize-next node tape network)
...)
这应该使痕迹按预期工作。