Advertisement
Guest User

Untitled

a guest
Oct 30th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 11.03 KB | None | 0 0
  1. ;;; useful functions for LISP natural language parsing program
  2.  
  3. ;;; Set up the parse-obj and word-dict
  4. (setf parse-obj (MAKE-HASH-TABLE))
  5. (setf word-dict (MAKE-HASH-TABLE))
  6. (setf table-ht  (MAKE-HASH-TABLE))
  7.  
  8.  
  9. ;;; processRequest
  10. ;;;    Parameters:
  11. ;;;       request - a list of words making a request
  12. ;;;    Purpose:
  13. ;;;       - Sets up necessary information (see below) and invokes the student's checkRequest
  14. ;;;         function.  
  15. ;;;       - Since the student's checkRequest sets the parts of speech, processRequest prints
  16. ;;;         those parts of speech.
  17. ;;;       - Returns T if the request is a valid request.  Otherwise, NIL is returned.
  18. ;;;    Notes:
  19. ;;;       - Sets the parse-obj hash table to contain the request
  20. ;;;       - Sets the cursor position to zero.
  21. ;;;       - Uses resetPartsOfSpeech to reset the values of each part of speech.
  22. ;;;       - Prints the request
  23. ;;;       - Invokes checkRequest to check for valid syntax and prints
  24. ;;;         the result.
  25. ;;;       - Prints the value for each part of speech
  26. ;;;       - Invokes makeDBRequest to generate a database request from the parts of speech
  27. ;;;    Notes:
  28. ;;;       - Requests will not contain punctuation since
  29. ;;;             -- commas have a meaning in Common LISP associated with backquote
  30. ;;;             -- periods are used for dotted pairs
  31. ;;;    
  32. (defun processRequest(request)
  33.     (PROG (result)
  34.         ;;; Save the request in parse-obj.
  35.         (putp 'request parse-obj request) ;; KEY=request; VAL=whatever the request was
  36.        
  37.         ;;; Set the cursor position to 0.
  38.         (putp 'cursor parse-obj 0)
  39.  
  40.         ;;; reset the parts of speech to NIL
  41.         (resetPartsOfSpeech parse-obj 'verb 'directObj 'prep 'indirectObj 'QualIndirectPrep 'QualIndirectObj) ;; CODE THIS - RESET THESE WORDS TO NIL in parse-obj HT
  42.         (resetPartsOfSpeech parse-obj 'Comparator 'NotQual 'QualPrep 'QualValue) ;; CODE THIS - RESET THESE WORDS TO NILL in parse-obj HT
  43.        
  44.         (format T "~% ******************************************************************")
  45.         (format T "~%  ~a" request) ;; our request. EX: COUNT THE NUMBER OF RENTALS
  46.         (setf result (checkRequest parse-Obj)) ;; CODE THIS - sets result equal to whatever checkRequest returned
  47.         (format T "~%    checkRequest returned ~a" result) ;; T if request was valid; NIL if it was not
  48.         (format T "~%    verb= ~a" (getp 'verb parse-obj)) ;; returns the property value for 'verb from parse-obj ht
  49.         (format T "~%    directObj= ~a" (getp 'directObj parse-obj)) ;; returns the property value for 'directObj from parse-obj ht
  50.         (format T "~%    prep= ~a" (getp 'prep parse-obj)) ;; returns the property value for 'prep from parse-obj ht
  51.         (format T "~%    indirectObj= ~a" (getp 'indirectObj parse-obj)) ;; returns the property value for 'indirectObj from parse-obj ht
  52.        
  53.         ;;; if we have a qual indirect, show it (SECOND FORM)
  54.         (cond ((or  (getp 'QualIndirectPrep parse-obj) ;; QualIndirectPrep EX: WITH
  55.                     (getp 'QualIndirectObj parse-obj)) ;; QualIndirectObj EX: RATING, DATE
  56.                 (format T "~%    QualIndirectPrep= ~a" (getp 'QualIndirectPrep parse-obj)) ;; get val associated with symbol QualIndirectPrep from parse-obj ht
  57.                 (format T "~%    QualIndirectObj= ~a" (getp 'QualIndirectObj parse-obj))   ;; get val associated with symbol QualIndirectObj from parse-obj ht
  58.         ))
  59.         ;;; if we have qual, show it
  60.         (cond ((or  (getp 'Comparator parse-obj) ;; return value associated with key 'Comparator from parse-obj ht
  61.                     (getp 'NotQual parse-obj)    ;; return value associated with key 'NotQual from parse-obj ht
  62.                     (getp 'QualPrep parse-obj)   ;; return value associated with key 'QualPrep from parse-obj ht
  63.                     (getp 'QualValue parse-obj)) ;; return value associated with key 'QualValue from parse-obj ht
  64.                    
  65.                 (if (getp 'NotQual parse-obj) ;; if NotQual does not return NIL, display it
  66.                      (format T "~%    NotQual = ~a" (getp 'NotQual parse-obj)))
  67.                      
  68.                 (format T "~%    Comparator= ~a" (getp 'Comparator parse-obj)) ;; return val assoc with key 'Comparator from parse-obj ht
  69.                 (format T "~%    QualPrep= ~a" (getp 'QualPrep parse-obj))     ;; return val assoc with key 'QualPrep from parse-obj ht
  70.                 (format T "~%    QualValue= ~a" (getp 'QualValue parse-obj))   ;; return val assoc with key 'QualValue from parse-obj ht
  71.         ))
  72.         (if (not result) (return result)) ;; If request is not NIL; return it
  73.        
  74.         ;;; see if doing extra credit, return if not
  75.         (if (NOT doingExtra)
  76.             (return result))
  77.        
  78.         ;;; generate the SQL and show that SQL query
  79.         (setf query (genSQL parse-Obj))
  80.         (format T "~%    DBRequest= ~a" query)
  81.        
  82.         (return result) ) )
  83.  
  84. ;;; putp
  85. ;;;    Parameters:
  86. ;;;       symbol   - symbol to be given the property value
  87. ;;;       ht       - hash table to store the symbol and its property value
  88. ;;;       value    - the property value
  89. ;;;    Purpose:
  90. ;;;       stores the property value for the symbol in the specified hash table
  91. ;;;    Notes:
  92. ;;;       If the symbol isn't an ATOM, putp breaks execution with an ERROR.
  93. ;;;    Example Usage:
  94. ;;;       (putp 'prep parse preposition)
  95. ;;;       (putp word word-dict partOfSpeech)
  96. ;;;          KEY       VALUE
  97. (defun putp (symbol ht value)
  98.     (if (ATOM symbol)
  99.         (setf (gethash symbol ht) value)
  100.         (ERROR "~s is not a valid symbol for putp" symbol)
  101.     )
  102. )
  103.    
  104. ;;; getp
  105. ;;;    Parameters:
  106. ;;;       symbol   - symbol about which we want its property value
  107. ;;;       ht       - hash table which stores the symbol and its property value
  108. ;;;    Purpose:
  109. ;;;       Returns the property value for the symbol in the specified hash table
  110. ;;;       If not found, it returns NIL.
  111. ;;;    Example Usage:
  112. ;;;       (getp word word-dict)  would return the partOfSpeech for that word
  113. ;;;       (getp 'request parse-obj)  would return the original request from the parse-obj.
  114. (defun getp (symbol ht)
  115.      (gethash symbol ht) )
  116.    
  117.      
  118. ;;; getCursor
  119. ;;;    Parameters:
  120. ;;;       parse - the parse object containing a request, cursor position, and
  121. ;;;               value for each part of speech
  122. ;;;    Purpose:
  123. ;;;       returns the current cursor position (relative to zero)
  124. (defun getCursor (parse)
  125.     (getp 'cursor parse) ) ;; should be a number
  126.  
  127. ;;; setCursor
  128. ;;;    Parameters:
  129. ;;;       parse - the parse object containing a request, cursor position, and
  130. ;;;               value for each part of speech
  131. ;;;       cursorPosition - new cursor position
  132. ;;;    Purpose:
  133. ;;;       Sets the value of the cursor position (relative to zero) in the
  134. ;;;       parse object
  135. ;;;    Notes:
  136. ;;;       If the cursorPosition isn't a numeric, setCursor breaks execution
  137. ;;;       with an ERROR.
  138. (defun setCursor (parse cursorPosition)
  139.     (if (numberp cursorPosition)
  140.         (putp 'cursor parse cursorPosition)
  141.         (ERROR "~s is not a numeric argument for setCursor" cursorPosition)
  142.     )
  143. )
  144.  
  145. ;;; getToken
  146. ;;;    Parameters:
  147. ;;;       parse - the parse object containing a request, cursor position, and
  148. ;;;               value for each part of speech
  149. ;;;    Purpose:
  150. ;;;       returns the next token from the request.  If there are no more
  151. ;;;       tokens, it returns NIL.
  152. ;;;    Notes:
  153. ;;;       This modifies the cursor position after getting the current token
  154. ;;;       at the old position.
  155. (defun getToken (parse)
  156.     (prog (temp)
  157.         (setf temp (nth (getp 'cursor parse) (getp 'request parse)))
  158.         (setCursor parse (1+ (getp 'cursor parse)))
  159.         (return temp) ) )
  160.  
  161.  
  162. ;;; checkRequest
  163. ;;; This function checks for a valid request according to the grammar above.
  164. ;;; Assume it is passed a parse-obj (which has already been populated with
  165. ;;; a request) and returns T if the request is valid; otherwise it returns NIL.
  166. ;;; Name the parameter parse to avoid conflicts. Additionally, checkRequest
  167. ;;; saves the identified parts of speech. checkRequest is invoked by Larry's
  168. ;;; processRequest(see the sample output).
  169. (defun checkRequest (parse)
  170.     (PROG (result)
  171.         ;;VERB
  172.         ;(setf saveCursor (getCursor parse))
  173.         (setf gram (getToken parse))
  174.         (do ()
  175.             ((NULL gram))
  176.             (print gram)
  177.            
  178.             (if (isa gram 'verb) (putp 'verb parse gram))
  179.            
  180.             (if (NOT(isa gram 'article)) (setCursor parse saveCursor) (putp 'article parse gram))
  181.            
  182.             (if (AND (isa gram 'noun) (EQL count 2))
  183.                 (prog ()
  184.                     (putp 'indirectObj parse gram)
  185.                     (setf count (+ 1 count)) ) )
  186.  
  187.             (if (AND (isa gram 'noun) (EQL count 1))
  188.                 (prog ()
  189.                     (putp 'directObj parse gram)
  190.                     (setf count (+ 1 count)) ) )
  191.            
  192.             (if (isa gram 'prep) (putp 'prep parse gram))
  193.            
  194.             (setf saveCursor (getCursor parse))
  195.             (setf gram (getToken parse))
  196.         )
  197.        
  198.     )
  199.    
  200. )
  201.        
  202.    
  203.  
  204. ;;; ***************************** DONE ***************************************
  205. ;;; resetPartsOfSpeech
  206. ;;; This function resets the value for each of the specified parts of speech
  207. ;;; to NIL using putp. The first argument is a parse-obj. There are a variable
  208. ;;; number of parts of speech passed to resetPartsOfSpeech.
  209. (defun resetPartsOfSpeech (parse &rest speech)
  210.     (dolist (part speech) (putp part parse NIL))
  211. )
  212.  
  213. ;;; ***************************** DONE ***************************************
  214. ;;; partOfSpeech
  215. ;;; This macro defines each word in the list of words to be specified
  216. ;;; partOfSpeech in the dictionary (hard coded word-dict). Use (putp word
  217. ;;; word-dict partOfSpeech) to put each word in word-dict with the partOfSpeech.
  218. ;;; KEYS=a an the; VALUE=ARTICLE
  219. (defmacro set_isa (word &rest partOfSpeech)
  220.     (dolist (part partOfSpeech) (putp part word-dict word))
  221. )
  222.  
  223. ;;; ***************************** DONE ***************************************
  224. ;;; isa
  225. ;;; returns T if the specified word is that specified partOfSpeech; otherwise,
  226. ;;; NIL is returned.
  227. (defun isa (word partOfSpeech)
  228.     (COND ((EQL (getp word word-dict) partOfSpeech)) )
  229. )
  230.  
  231.  
  232. ;(print (isa 'the (getp 'the word-dict) ))
  233. ;(print (getp 'elect word-dict))
  234. ;(setf x  '123)
  235. ;(print (NULL x))
  236. ;(print (NULL(isa 'select (getp 'select word-dict))))
  237. ;(print parse-obj)
  238.  
  239.  
  240.  
  241.  ;;; defaulting doingExtra to NIL
  242.  (setf doingExtra NIL)
  243.  
  244. (set_isa article a n the)
  245. (set_isa noun movies movie rental rentals customer customers)
  246. (set_isa noun ID title genre rating custNr name state date number gender)
  247. (set_isa verb count select show print)
  248. (set_isa prep of with to in for)
  249. (set_isa comparator  equal after before)
  250. (set_isa adjective horror scifi romance comedy action male female g pg13 r x)
  251. (processRequest '(count the number of rentals))
  252. ;(processRequest '(show id for movies with rating equal to sex))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement