2016-10-25 4 views
0

Je travaille dans un projet NLP avec Racket.Comment utiliser trace dans ce code Racket?

Ce code fonctionne correctement. Cependant, j'aimerais pouvoir le retrouver.

J'ai des problèmes pour tracer le comportement de la fonction generate-next.

C'est mon code:

#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))) 

Quand j'appelle:

(recognize english-1 '(sandy is a happy woman)) 

Trace fonctionne bien et j'ai le résultat attendu:

>(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 

Cependant, lorsque vous essayez de tracer quelque chose que ce n'est pas sur le réseau, trace a un comportement étrange. Par exemple, lorsque j'appelle:

(recognize english-1 '(not defined on the network)) 

La sortie est '() qui est correcte. Mais je m'attendais à une information plus détaillée que:

'() 

J'avais beaucoup de doutes sur l'endroit où mettre la commande (trace recogn-next). Je l'ai mis juste avant la fin de la reconnaissance-prochaine définition. Je ne sais pas si c'est l'endroit approprié.

Répondre

0

La forme trace modifie une liaison elle-même pour insérer le suivi, mais ce n'est généralement pas quelque chose que vous voulez utiliser directement. Au lieu de cela, il est généralement plus facile d'utiliser trace-define, trace-lambda ou trace-let. Retirez votre utilisation directe de (trace recognize-next) et remplacer votre définition recognize-next avec celui qui utilise trace-define:

(trace-define (recognize-next node tape network) 
    ...) 

Cela devrait rendre les traces fonctionnent comme prévu.

+0

merci! Cela a bien fonctionné. Il y a une autre chose que je ne peux pas comprendre. Puisque kim est sur ma définition d'abréviations, pourquoi appeler (reconnaître english-1 '(kim)) retourne vide'()? –

+0

@fallowzito J'avoue que je n'en ai aucune idée - je n'ai lu aucun code, juste les parties liées à 'racket/trace'. ;) Je suis sûr que vous pouvez comprendre, cependant: j'essayerais de jouer avec vos fonctions dans le REPL pour comprendre où ils vont mal. Vous pouvez également jeter un coup d'oeil sur le paquet 'debug', qui fournit des formulaires utiles pour aider les programmes de débogage. –