Advertisement
Guest User

PGM4 UPDATE 1

a guest
Oct 30th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 10.23 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 (gram)
  171.     (setf gram (getToken parse))
  172.    
  173.     ;(print (getp gram word-dict))
  174.         (do ( )
  175.             ( (NULL gram) ) ;until nil do
  176.             (if (not (isa gram (getp gram word-dict)))
  177.                 (return NIL)
  178.             (putp (getp gram word-dict) parse gram))
  179.            
  180.             (setf gram (getToken parse)) ;get next token
  181.            
  182.         )
  183.        
  184.        
  185.     )
  186. )
  187.        
  188.  
  189. ;;; ***************************** DONE ***************************************
  190. ;;; resetPartsOfSpeech
  191. ;;; This function resets the value for each of the specified parts of speech
  192. ;;; to NIL using putp. The first argument is a parse-obj. There are a variable
  193. ;;; number of parts of speech passed to resetPartsOfSpeech.
  194. (defun resetPartsOfSpeech (parse &rest speech)
  195.     (dolist (part speech) (putp part parse NIL))
  196. )
  197.  
  198. ;;; ************************** DONE... MAYBE *********************************
  199. ;;; partOfSpeech
  200. ;;; This macro defines each word in the list of words to be specified
  201. ;;; partOfSpeech in the dictionary (hard coded word-dict). Use (putp word
  202. ;;; word-dict partOfSpeech) to put each word in word-dict with the partOfSpeech.
  203. ;;; KEYS=a an the; VALUE=ARTICLE
  204. (defmacro set_isa (word &rest partOfSpeech)
  205.     (dolist (part partOfSpeech) (putp part word-dict word))
  206. )
  207.  
  208. ;;; ***************************** DONE ***************************************
  209. ;;; isa
  210. ;;; returns T if the specified word is that specified partOfSpeech; otherwise,
  211. ;;; NIL is returned.
  212. (defun isa (word partOfSpeech)
  213.     (COND ( (EQL (getp word word-dict) partOfSpeech) T) )
  214. )
  215.  
  216. (set_isa article a n the)
  217. (set_isa noun movies movie rental rentals customer customers)
  218. (set_isa noun ID title genre rating custNr name state date number gender)
  219. (set_isa verb count select show print)
  220. (set_isa prep of with to in for)
  221. (set_isa comparator  equal after before)
  222. (set_isa adjective horror scifi romance comedy action male female g pg13 r x)
  223. (processRequest '(count the number of rentals))
  224.  
  225.  
  226.  
  227.  
  228.  ;;; defaulting doingExtra to NIL
  229.  (setf doingExtra NIL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement