;;; -*- 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
  #-(or :educ :openproof :mal :edgen)
  (lkb-load-lisp (parent-directory) "Version.lsp" t nil)
  #+(or :educ :edgen)
  (lkb-load-lisp (parent-directory) "educ/Version-educ.lsp" t nil)
  #+:mal
  (lkb-load-lisp (parent-directory) "educ/Version-mal.lsp" t nil)
  #+:openproof
  (lkb-load-lisp (parent-directory) "openproof/Version-op.lsp" t nil)
  #+:dict
  (lkb-load-lisp (parent-directory) "dict/Version-dict.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)
  (load-irregular-spellings 
   (list
    (lkb-pathname (parent-directory) "irregs.tab")
    ))
  (read-tdl-type-files-aux
   (list (lkb-pathname (parent-directory) "fundamentals.tdl")
      	 #+:singlish
   	 (lkb-pathname (parent-directory) "singlish/fundamentals_sg.tdl")
         (lkb-pathname (parent-directory) "tmt.tdl")
         (lkb-pathname (parent-directory) "lextypes.tdl")
         #+:singlish
         (lkb-pathname (parent-directory) "singlish/lextypes_sg.tdl")
         (lkb-pathname (parent-directory) "syntax.tdl")
         #+:singlish
         (lkb-pathname (parent-directory) "singlish/syntax_sg.tdl")
	 (lkb-pathname (parent-directory) "ctype.tdl")
         #+:singlish
	 (lkb-pathname (parent-directory) "singlish/ctype_sg.tdl")
	 #+(or :educ :mal :edgen)
	 (lkb-pathname (parent-directory) "educ/ctype-educ.tdl")
         (lkb-pathname (parent-directory) "lexrules.tdl")
         #+:singlish
         (lkb-pathname (parent-directory) "singlish/lexrules_sg.tdl")
	 (lkb-pathname (parent-directory) "delims.tdl")
	 #+(or :singlish)
	 (lkb-pathname (parent-directory) "singlish/delims_sg.tdl")
         (lkb-pathname (parent-directory) "auxverbs.tdl")
	 #+(or :educ :mal :edgen)
         (lkb-pathname (parent-directory) "educ/lextypes-educ.tdl")
	 #+:openproof
         (lkb-pathname (parent-directory) "openproof/types-op.tdl")
	 #+:openproof
         (lkb-pathname (parent-directory) "openproof/prorder.tdl")
         #+:mt
         (lkb-pathname (parent-directory) "mtr.tdl")
	 #+:openproof
         (lkb-pathname (parent-directory) "openproof/mtr-op.tdl")
         )
   (lkb-pathname (this-directory) "settings.lsp"))
  (read-cached-leaf-types-if-available
   (list (lkb-pathname (parent-directory) "letypes.tdl")
   	 #+:singlish
   	 (lkb-pathname (parent-directory) "singlish/letypes_sg.tdl")
	 #+(or :educ :edgen)
	 (lkb-pathname (parent-directory) "educ/letypes-mask.tdl")
	 #+(or :educ :mal)
	 (lkb-pathname (parent-directory) "educ/letypes-mal.tdl")))

  #+(or :educ :mal :openproof :edgen :lkb-fos)
  (setf *lexdb-params* nil)

  (if (and (boundp '*lexdb-params*) *lexdb-params*)
      (progn
	(load-lexdb-from-script)
	(read-cached-sublex-if-available
	"le" (list (lkb-pathname (parent-directory) "gle.tdl")
		   (lkb-pathname (parent-directory) "ple.tdl"))))
    (read-cached-lex-if-available 
     (list
      (lkb-pathname (parent-directory) "lexicon.tdl")
      (lkb-pathname (parent-directory) "ple.tdl")
      #+:singlish
      (lkb-pathname (parent-directory) "singlish/lexicon_sg.tdl")
      #+(or :educ :edgen)
      (lkb-pathname (parent-directory) "educ/lexicon-mask.tdl")
      #+(or :educ :mal)
      (lkb-pathname (parent-directory) "educ/lexicon-mal.tdl")
      #-(or :educ :mal :edgen)
      (lkb-pathname (parent-directory) "gle-gen.tdl")
      #+:openproof
      (lkb-pathname (parent-directory) "openproof/lex-openproof.tdl")
      )))

  (read-cached-sublex-if-available 
     "gle"
     (list
      (lkb-pathname (parent-directory) "gle.tdl")))

  (read-tdl-grammar-file-aux 
   (lkb-pathname (parent-directory) "constructions.tdl"))
  #+:singlish
  (read-tdl-grammar-file-aux 
   (lkb-pathname (parent-directory) "singlish/constructions_sg.tdl"))
  #+(or :educ :edgen)
  (read-tdl-grammar-file-aux 
   (lkb-pathname (parent-directory) "educ/constr-mask.tdl"))
  #+(or :educ :mal)
  (read-tdl-grammar-file-aux 
   (lkb-pathname (parent-directory) "educ/constr-mal.tdl"))
  #+:openproof
  (read-tdl-grammar-file-aux 
   (lkb-pathname (parent-directory) "openproof/constr-op.tdl"))
  #+:dict
  (read-tdl-grammar-file-aux 
   (lkb-pathname (parent-directory) "dict/constr-dict.tdl"))
  (read-morph-file-aux (lkb-pathname (parent-directory) "inflr.tdl"))
  #+:singlish
  (read-morph-file-aux (lkb-pathname (parent-directory) "singlish/inflr_sg.tdl"))
  ; For ecommerce and vm, where e.g. period is used to end questions
  (read-tdl-psort-file-aux 
   (lkb-pathname (parent-directory) "roots.tdl"))
  #+:singlish
  (read-tdl-psort-file-aux
   (lkb-pathname (parent-directory) "singlish/roots_sg.tdl"))
  #+:openproof
  (read-tdl-psort-file-aux 
   (lkb-pathname (parent-directory) "openproof/roots-op.tdl"))
  #-(or :educ :mal :openproof :edgen)
  (read-tdl-lex-rule-file-aux 
   (lkb-pathname (parent-directory) "lexrinst.tdl"))
  #+:singlish
  (read-tdl-lex-rule-file-aux 
   (lkb-pathname (parent-directory) "singlish/lexrinst_sg.tdl"))
  #+(or :educ :edgen)
  (read-tdl-lex-rule-file-aux 
   (lkb-pathname (parent-directory) "educ/lexrinst-mask.tdl"))
  #+(or :educ :mal)
  (read-tdl-lex-rule-file-aux 
   (lkb-pathname (parent-directory) "educ/lexrinst-mal.tdl"))
  ;; DPF 2020-06-14 - Until we get token-mapping support in the LKB, exclude
  ;; loading these mal-rules which depend on information from token-mapping:
  ;#+(or :educ :mal)
  ;(read-tdl-lex-rule-file-aux 
  ; (lkb-pathname (parent-directory) "educ/lexrinst-tok.tdl"))
  #+:openproof
  (read-tdl-lex-rule-file-aux 
   (lkb-pathname (parent-directory) "openproof/lexrinst.tdl"))
  ;; DPF 2020-06-06 - Comment out until token-mapping is supported, since the
  ;; following rules refer to token properties such as weak brackets that are
  ;; constrained in token mapping:
  ;  (read-tdl-lex-rule-file-aux 
  ;   (lkb-pathname (parent-directory) "lexrinst-tok.tdl"))
  #-:singlish
  (read-tdl-parse-node-file-aux 
   (lkb-pathname (parent-directory) "parse-nodes.tdl"))
  #+:singlish
  (read-tdl-parse-node-file-aux 
   (lkb-pathname (parent-directory) "singlish/parse-nodes_sg.tdl"))

  (lkb-load-lisp (this-directory) "mrsglobals.lsp" t nil)
  (lkb-load-lisp (this-directory) "eds.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)

;;;
;;; Load the chart-mapping rules
;;;
#+:chart-mapping
(progn
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/gml.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/ptb.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/spelling.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/ne1.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/split.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/ne2.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/class.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/ne3.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/punctuation.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/pos.tdl"))
(read-token-mapping-file-aux 
 (lkb-pathname (parent-directory) "tmr/finis.tdl"))

(read-lexical-filtering-file-aux 
 (lkb-pathname (parent-directory) "lfr.tdl"))
)
;;;
;;; 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) "jhpstg.g.mem"))
#+(and :tsdb :logon)
(setf *unpacking-scoring-hook* #'tsdb::mem-score-configuration)

;;;
;;; the semantic interface (SEM-I), used in MRS comparison and generation
;;;
(mt:read-semi (lkb-pathname (parent-directory "etc") "erg.smi"))

;;;
;;; the mapping from grammar-internal to SEM-I compliant variable properties
;;;
#-:openproof
(mt:read-vpm (lkb-pathname (parent-directory) "semi.vpm") :semi)
#+:openproof
(mt:read-vpm (lkb-pathname (parent-directory) "openproof/semi.vpm") :semi)
;;;
;;; upon conversion to EDS, drop 'default' variable properties, e.g. [ PERF - ]
;;;
(mt:read-vpm (lkb-pathname (parent-directory) "eds.vpm") :eds)
;;;
;;; 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)
;;;

;#-:openproof
(mt:read-transfer-rules 
 (list
  (lkb-pathname (parent-directory) "paraphraser.mtr")
  #+(or :educ :mal :edgen)
  (lkb-pathname (parent-directory) "educ/educ.mtr")
  )
 "MWE Paraphraser"
 :before "mrs::normalize-mrs" :filter nil :task :paraphrase)

;#+:openproof
;(mt:read-transfer-rules 
; (list
;  (lkb-pathname (parent-directory) "openproof/openproof.mtr"))
; "Generation Input Fix-Up Rules for OpenProof"
; :before "mrs::normalize-mrs" :filter nil :task :paraphrase :edges 200)

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

(mt:read-transfer-rules 
 (list
#-:openproof  
  (lkb-pathname (parent-directory) "trigger.mtr")
#+:openproof  
(lkb-pathname (parent-directory) "openproof/trig-op.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.
;;;
(with-open-file (stream 
#-(or :educ :mal :dict :singlish)
  (lkb-pathname (parent-directory) "erg.hds")
#+(or :educ :mal)
  (lkb-pathname (parent-directory) "educ/erg.hds")
#+:singlish
  (lkb-pathname (parent-directory) "singlish/erg.hds")
#+:dict
  (lkb-pathname (parent-directory) "dict/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)))))

(setf *start-symbol* '(root_strict root_informal))
#+(or :educ :mal)
(setf *start-symbol* '(root_decl root_question root_command root_robust_ques root_robust_s root_inffrag root_robust_frag root_lex root_informal))
#+:singlish
(setf *start-symbol* '(root_lepak root_gen_lepak))
#+:openproof
(setf mrs::*icons-p* nil)