;;; -*- Mode: LISP; Syntax: Common-Lisp; Package: LKB -*-

(unless (lkb-version-after-p "2006/07/05 12:00:00")
  (error "this version of the ERG requires an LKB version >= 2006/07/05"))
  
(set-coding-system utf-8)

(time
 (progn
  (lkb-load-lisp (parent-directory) "Version.lsp" t nil)
  (setf *grammar-directory* (parent-directory))
  (load-lkb-preferences (this-directory) "user-prefs.lsp")
  (lkb-load-lisp (this-directory) "globals.lsp" nil nil)
  (lkb-load-lisp (this-directory) "user-fns.lsp" nil nil)
  (lkb-load-lisp (this-directory) "checkpaths.lsp" t nil)
  (lkb-load-lisp (this-directory) "patches.lsp" t nil)
  #+:speech
  (lkb-load-lisp (this-directory) "convert-wcn.lsp" t nil)
  (load-irregular-spellings 
   (list
    (lkb-pathname (parent-directory) "irregs.tab")
    ))
  (read-tdl-type-files-aux
   (list (lkb-pathname (parent-directory) "fundamentals.tdl")
         (lkb-pathname (parent-directory) "tmt.tdl")
         (lkb-pathname (parent-directory) "lextypes.tdl")
         (lkb-pathname (parent-directory) "syntax.tdl")
         #+:speech
         (lkb-pathname (parent-directory) "speech/syntax-speech.tdl")
         (lkb-pathname (parent-directory) "lexrules.tdl")
         (lkb-pathname (parent-directory) "auxverbs.tdl")
         #+:educ
         (lkb-pathname (parent-directory) "educ/lextypes-epgy.tdl")
         #+:mt
         (lkb-pathname (parent-directory) "mtr.tdl")
         )
   (lkb-pathname (this-directory) "settings.lsp"))
  (read-cached-leaf-types-if-available
   (list (lkb-pathname (parent-directory) "letypes.tdl")))
  #+:educ
  (read-cached-leaf-types-if-available
   (list (lkb-pathname (parent-directory) "educ/letypes-epgy.tdl")))

  #+(or :speech :educ)
  (setf *lexdb-params* nil)

  (if (and (boundp '*lexdb-params*) *lexdb-params*)
      (progn
	(load-lexdb-from-script)
	(read-cached-sublex-if-available 
	"gle" (lkb-pathname (parent-directory) "gle.tdl"))
	)
    (read-cached-lex-if-available 
     (list
      (lkb-pathname (parent-directory) "lexicon.tdl")
      #+:educ
      (lkb-pathname (parent-directory) "educ/lexicon-epgy.tdl")
      (lkb-pathname (parent-directory) "gle.tdl")
      #+:speech
      (lkb-pathname (parent-directory) "speech/lex-speech.tdl")
      #+:robot
      (lkb-pathname (parent-directory) "speech/lex-robot.tdl")
      )))

  #-:educ
  (read-tdl-grammar-file-aux 
   (lkb-pathname (parent-directory) "constructions.tdl"))
  #+:educ
  (read-tdl-grammar-file-aux 
   (lkb-pathname (parent-directory) "educ/constr-epgy.tdl"))

  ;; Include to accommodate missing dets for sg-count-nouns, or subj-verb agr. 
  ;;#+:speech
  ;;(read-tdl-grammar-file-aux 
  ;; (lkb-pathname (parent-directory) "speech/constr-speech.tdl"))

  (read-morph-file-aux (lkb-pathname (parent-directory) "inflr.tdl"))
  (read-morph-file-aux (lkb-pathname (parent-directory) "inflr-pnct.tdl"))
  ; For ecommerce and vm, where e.g. period is used to end questions
  #+:speech
  (read-morph-file-aux 
   (lkb-pathname (parent-directory) "speech/inflr-pnct-rbst.tdl"))
  (read-tdl-psort-file-aux 
   (lkb-pathname (parent-directory) "roots.tdl"))
  #-:educ
  (read-tdl-lex-rule-file-aux 
   (lkb-pathname (parent-directory) "lexrinst.tdl"))
  #+:educ
  (read-tdl-lex-rule-file-aux 
   (lkb-pathname (parent-directory) "educ/lexrinst-epgy.tdl"))
  (read-tdl-parse-node-file-aux 
   (lkb-pathname (parent-directory) "parse-nodes.tdl"))

  (lkb-load-lisp (this-directory) "mrsglobals.lsp" t nil)
  #+:tsdb
  (lkb-load-lisp (parent-directory "www") "setup.lsp" t nil)
  ))

;;;
;;; the Regular Expression Pre-Processor (REPP) is now broken up into quite a
;;; number of files.  to ease re-use across the LKB and PET, keep the REPP 
;;; configuration in a separate file.
;;;
(lkb-load-lisp (parent-directory "rpp") "setup.lsp" t nil)

;;;
;;; when loaded into an environment including [incr tsdb()] and the Redwoods
;;; tools, also load a Maximum Entropy realization ranking model and activate
;;; selective unpacking (for generation).
;;;
#+(and :tsdb :logon)
(tsdb::read-model (lkb-pathname (parent-directory) "tc.g.mem"))
#+(and :tsdb :logon)
(setf *unpacking-scoring-hook* #'tsdb::mem-score-configuration)

;;;
;;; the semantic interface (SEM-I), used only in batch testing (for now)
;;;
#+:logon
(mt:read-semi (lkb-pathname (parent-directory) "erg.smi"))

;;;
;;; the mapping from grammar-internal to SEM-I compliant variable properties
;;;
(mt:read-vpm (lkb-pathname (parent-directory) "semi.vpm") :semi)
;;;
;;; another variable property mapping, used only when constructing the SEM-I
;;;
(mt:read-vpm (lkb-pathname (parent-directory) "abstract.vpm") :abstract)


;;;
;;; finally, use the new transfer component: load any number of rule sets, each
;;; in a separate file, associated with its identifier.  
;;;
(lkb-load-lisp (this-directory) "mt.lsp" t nil)

;;;
;;; the following two rule sets could possibly be merged at some point, but
;;; for right now dan prefers keeping them separate.            (3-may-05; oe)
;;;
(mt:read-transfer-rules 
 (list
  (lkb-pathname (parent-directory) "paraphraser.mtr")
  #+:educ
  (lkb-pathname (parent-directory) "educ.mtr")
  )
 "MWE Paraphraser"
 :before "mrs::normalize-mrs" :filter nil :task :paraphrase)

(mt:read-transfer-rules 
 (list
  (lkb-pathname (parent-directory) "idioms.mtr"))
 "Idiom Tests"
 :filter nil :task :idiom)

(mt:read-transfer-rules 
 (list
  (lkb-pathname (parent-directory) "trigger.mtr"))
 "Generation Trigger Rules"
 :filter nil :task :trigger :recurse nil :edges 200 :subsume nil)

(mt:read-transfer-rules 
 (list
  (lkb-pathname (parent-directory) "generation.mtr"))
 "Generation Input Fix-Up Rules"
 :before "mrs::normalize-mrs" :filter nil :task :fixup :edges 200)

#+:null
(mt:read-transfer-rules 
 (list
  (lkb-pathname (parent-directory) "cluster.mtr"))
 "Cluster Normalizer"
 :filter nil :task :paraphrase)

#+:null
(mt:read-transfer-rules 
 (list
  (lkb-pathname (parent-directory) "semiout.mtr"))
 "SEMI example generator"
 :filter nil :task :paraphrase :recurse nil :subsume t)


;;;
;;; finally, this time really, compute a head table for non-unary rules.  this
;;; is such a cheap operation that it seems best to let it run automatically
;;; each time the grammar is (re-)loaded.
;;;
#+:null
(with-open-file (stream (lkb-pathname (parent-directory) "erg.hds")
                 :direction :output :if-exists :supersede)
  (let ((rules (loop for rule being each hash-value in *rules* collect rule)))
    (loop
        for rule in (sort rules #'string< :key #'rule-id)
        do
          (format
           stream "~(~a~) ~a ~a~%"
           (rule-id rule) (length (rule-rhs rule)) (rule-head rule)))))

#+:tsdb
(progn
  (in-package "TSDB")
  (setf *tsdb-maximal-number-of-analyses* 1)
  (setf *tsdb-maximal-number-of-results* 1)
  (in-package "LKB"))