-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdict-comp.scm
executable file
·85 lines (73 loc) · 2.44 KB
/
dict-comp.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/env -S guile
!#
;
; dict-comp.scm
; Compare parses produced by two different LG dictionaries over
; a sample corpus.
;
; Usage:
; guile -s dict-comp.scm <gold-dict> <test-dict> <sentence-file>
;
; <gold-dict> should be the reference dictionary; it must be a
; valid Link-Grammar dictionary
; <test-dict> is the dictionary that will be compared to the reference.
; <sentence-file> should be a file containing sentences whose parses
; will be compared.
;
; Example:
; guile -s dict-comp.scm en micro-fuzz sentences.txt
;
(use-modules (srfi srfi-1))
(use-modules (ice-9 rdelim))
(use-modules (opencog) (opencog nlp) (opencog learn))
; Check usage
(if (not (equal? 4 (length (program-arguments))))
(begin
(format #t
"Usage: ~A <gold-dict> <test-dict> <sentence-file>\n"
(first (program-arguments)))
(exit #f)))
(define gold-dict (second (program-arguments)))
(define test-dict (third (program-arguments)))
(define sent-file (fourth (program-arguments)))
; Check file access
(if (not (equal? (stat:type (stat gold-dict)) 'directory))
(begin
(format #t "Cannot find reference dictionary ~A\n" gold-dict)
(exit #f)))
(if (not (equal? (stat:type (stat test-dict)) 'directory))
(begin
(format #t "Cannot find test dictionary ~A\n" test-dict)
(exit #f)))
(if (not (access? sent-file R_OK))
(begin
(format #t "Cannot find sentence file ~A\n" sent-file)
(exit #f)))
; Perform comparison
(format #t "Comparing \"~A\" to \"~A\" with sentences from \"~A\"\n"
gold-dict test-dict sent-file)
;; Set #:INCLUDE-MISSING to #f to disable the processing of sentences
;; containing words that the dictionary does not know about (i.e. to
;; disable unknown-word guessing.)
(define compare
(make-lg-comparator (LgDictNode gold-dict) (LgDictNode test-dict) '()
#:INCLUDE-MISSING #f))
(define (process-file PORT)
(define line (read-line PORT))
(if (not (eof-object? line))
(begin
; The # symbol is a comment-card
(if (and
(< 0 (string-length line))
; % is a comment for LG, ! is a directive for LG,
; * means "bad sentence" and # is a comment for python
(not (equal? #\# (string-ref line 0)))
(not (equal? #\! (string-ref line 0)))
(not (equal? #\* (string-ref line 0)))
(not (equal? #\% (string-ref line 0))))
(compare line))
(process-file PORT))
(compare #f)))
(process-file (open sent-file O_RDONLY))
(format #t "Finiished comparing \"~A\" to \"~A\" with sentences from \"~A\"\n"
gold-dict test-dict sent-file)