Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; useful functions for LISP natural language parsing program
- ;;; Set up the parse-obj and word-dict
- (setf parse-obj (MAKE-HASH-TABLE))
- (setf word-dict (MAKE-HASH-TABLE))
- (setf table-ht (MAKE-HASH-TABLE))
- ;;; processRequest
- ;;; Parameters:
- ;;; request - a list of words making a request
- ;;; Purpose:
- ;;; - Sets up necessary information (see below) and invokes the student's checkRequest
- ;;; function.
- ;;; - Since the student's checkRequest sets the parts of speech, processRequest prints
- ;;; those parts of speech.
- ;;; - Returns T if the request is a valid request. Otherwise, NIL is returned.
- ;;; Notes:
- ;;; - Sets the parse-obj hash table to contain the request
- ;;; - Sets the cursor position to zero.
- ;;; - Uses resetPartsOfSpeech to reset the values of each part of speech.
- ;;; - Prints the request
- ;;; - Invokes checkRequest to check for valid syntax and prints
- ;;; the result.
- ;;; - Prints the value for each part of speech
- ;;; - Invokes makeDBRequest to generate a database request from the parts of speech
- ;;; Notes:
- ;;; - Requests will not contain punctuation since
- ;;; -- commas have a meaning in Common LISP associated with backquote
- ;;; -- periods are used for dotted pairs
- ;;;
- (defun processRequest(request)
- (PROG (result)
- ;;; Save the request in parse-obj.
- (putp 'request parse-obj request) ;; KEY=request; VAL=whatever the request was
- ;;; Set the cursor position to 0.
- (putp 'cursor parse-obj 0)
- ;;; reset the parts of speech to NIL
- (resetPartsOfSpeech parse-obj 'verb 'directObj 'prep 'indirectObj 'QualIndirectPrep 'QualIndirectObj) ;; CODE THIS - RESET THESE WORDS TO NIL in parse-obj HT
- (resetPartsOfSpeech parse-obj 'Comparator 'NotQual 'QualPrep 'QualValue) ;; CODE THIS - RESET THESE WORDS TO NILL in parse-obj HT
- (format T "~% ******************************************************************")
- (format T "~% ~a" request) ;; our request. EX: COUNT THE NUMBER OF RENTALS
- (setf result (checkRequest parse-Obj)) ;; CODE THIS - sets result equal to whatever checkRequest returned
- (format T "~% checkRequest returned ~a" result) ;; T if request was valid; NIL if it was not
- (format T "~% verb= ~a" (getp 'verb parse-obj)) ;; returns the property value for 'verb from parse-obj ht
- (format T "~% directObj= ~a" (getp 'directObj parse-obj)) ;; returns the property value for 'directObj from parse-obj ht
- (format T "~% prep= ~a" (getp 'prep parse-obj)) ;; returns the property value for 'prep from parse-obj ht
- (format T "~% indirectObj= ~a" (getp 'indirectObj parse-obj)) ;; returns the property value for 'indirectObj from parse-obj ht
- ;;; if we have a qual indirect, show it (SECOND FORM)
- (cond ((or (getp 'QualIndirectPrep parse-obj) ;; QualIndirectPrep EX: WITH
- (getp 'QualIndirectObj parse-obj)) ;; QualIndirectObj EX: RATING, DATE
- (format T "~% QualIndirectPrep= ~a" (getp 'QualIndirectPrep parse-obj)) ;; get val associated with symbol QualIndirectPrep from parse-obj ht
- (format T "~% QualIndirectObj= ~a" (getp 'QualIndirectObj parse-obj)) ;; get val associated with symbol QualIndirectObj from parse-obj ht
- ))
- ;;; if we have qual, show it
- (cond ((or (getp 'Comparator parse-obj) ;; return value associated with key 'Comparator from parse-obj ht
- (getp 'NotQual parse-obj) ;; return value associated with key 'NotQual from parse-obj ht
- (getp 'QualPrep parse-obj) ;; return value associated with key 'QualPrep from parse-obj ht
- (getp 'QualValue parse-obj)) ;; return value associated with key 'QualValue from parse-obj ht
- (if (getp 'NotQual parse-obj) ;; if NotQual does not return NIL, display it
- (format T "~% NotQual = ~a" (getp 'NotQual parse-obj)))
- (format T "~% Comparator= ~a" (getp 'Comparator parse-obj)) ;; return val assoc with key 'Comparator from parse-obj ht
- (format T "~% QualPrep= ~a" (getp 'QualPrep parse-obj)) ;; return val assoc with key 'QualPrep from parse-obj ht
- (format T "~% QualValue= ~a" (getp 'QualValue parse-obj)) ;; return val assoc with key 'QualValue from parse-obj ht
- ))
- (if (not result) (return result)) ;; If request is not NIL; return it
- ;;; see if doing extra credit, return if not
- (if (NOT doingExtra)
- (return result))
- ;;; generate the SQL and show that SQL query
- (setf query (genSQL parse-Obj))
- (format T "~% DBRequest= ~a" query)
- (return result) ) )
- ;;; putp
- ;;; Parameters:
- ;;; symbol - symbol to be given the property value
- ;;; ht - hash table to store the symbol and its property value
- ;;; value - the property value
- ;;; Purpose:
- ;;; stores the property value for the symbol in the specified hash table
- ;;; Notes:
- ;;; If the symbol isn't an ATOM, putp breaks execution with an ERROR.
- ;;; Example Usage:
- ;;; (putp 'prep parse preposition)
- ;;; (putp word word-dict partOfSpeech)
- ;;; KEY VALUE
- (defun putp (symbol ht value)
- (if (ATOM symbol)
- (setf (gethash symbol ht) value)
- (ERROR "~s is not a valid symbol for putp" symbol)
- )
- )
- ;;; getp
- ;;; Parameters:
- ;;; symbol - symbol about which we want its property value
- ;;; ht - hash table which stores the symbol and its property value
- ;;; Purpose:
- ;;; Returns the property value for the symbol in the specified hash table
- ;;; If not found, it returns NIL.
- ;;; Example Usage:
- ;;; (getp word word-dict) would return the partOfSpeech for that word
- ;;; (getp 'request parse-obj) would return the original request from the parse-obj.
- (defun getp (symbol ht)
- (gethash symbol ht) )
- ;;; getCursor
- ;;; Parameters:
- ;;; parse - the parse object containing a request, cursor position, and
- ;;; value for each part of speech
- ;;; Purpose:
- ;;; returns the current cursor position (relative to zero)
- (defun getCursor (parse)
- (getp 'cursor parse) ) ;; should be a number
- ;;; setCursor
- ;;; Parameters:
- ;;; parse - the parse object containing a request, cursor position, and
- ;;; value for each part of speech
- ;;; cursorPosition - new cursor position
- ;;; Purpose:
- ;;; Sets the value of the cursor position (relative to zero) in the
- ;;; parse object
- ;;; Notes:
- ;;; If the cursorPosition isn't a numeric, setCursor breaks execution
- ;;; with an ERROR.
- (defun setCursor (parse cursorPosition)
- (if (numberp cursorPosition)
- (putp 'cursor parse cursorPosition)
- (ERROR "~s is not a numeric argument for setCursor" cursorPosition)
- )
- )
- ;;; getToken
- ;;; Parameters:
- ;;; parse - the parse object containing a request, cursor position, and
- ;;; value for each part of speech
- ;;; Purpose:
- ;;; returns the next token from the request. If there are no more
- ;;; tokens, it returns NIL.
- ;;; Notes:
- ;;; This modifies the cursor position after getting the current token
- ;;; at the old position.
- (defun getToken (parse)
- (prog (temp)
- (setf temp (nth (getp 'cursor parse) (getp 'request parse)))
- (setCursor parse (1+ (getp 'cursor parse)))
- (return temp) ) )
- ;;; checkRequest
- ;;; This function checks for a valid request according to the grammar above.
- ;;; Assume it is passed a parse-obj (which has already been populated with
- ;;; a request) and returns T if the request is valid; otherwise it returns NIL.
- ;;; Name the parameter parse to avoid conflicts. Additionally, checkRequest
- ;;; saves the identified parts of speech. checkRequest is invoked by Larry's
- ;;; processRequest(see the sample output).
- (defun checkRequest (parse)
- (PROG (gram)
- (setf gram (getToken parse))
- ;(print (getp gram word-dict))
- (do ( )
- ( (NULL gram) ) ;until nil do
- (if (not (isa gram (getp gram word-dict)))
- (return NIL)
- (putp (getp gram word-dict) parse gram))
- (setf gram (getToken parse)) ;get next token
- )
- )
- )
- ;;; ***************************** DONE ***************************************
- ;;; resetPartsOfSpeech
- ;;; This function resets the value for each of the specified parts of speech
- ;;; to NIL using putp. The first argument is a parse-obj. There are a variable
- ;;; number of parts of speech passed to resetPartsOfSpeech.
- (defun resetPartsOfSpeech (parse &rest speech)
- (dolist (part speech) (putp part parse NIL))
- )
- ;;; ************************** DONE... MAYBE *********************************
- ;;; partOfSpeech
- ;;; This macro defines each word in the list of words to be specified
- ;;; partOfSpeech in the dictionary (hard coded word-dict). Use (putp word
- ;;; word-dict partOfSpeech) to put each word in word-dict with the partOfSpeech.
- ;;; KEYS=a an the; VALUE=ARTICLE
- (defmacro set_isa (word &rest partOfSpeech)
- (dolist (part partOfSpeech) (putp part word-dict word))
- )
- ;;; ***************************** DONE ***************************************
- ;;; isa
- ;;; returns T if the specified word is that specified partOfSpeech; otherwise,
- ;;; NIL is returned.
- (defun isa (word partOfSpeech)
- (COND ( (EQL (getp word word-dict) partOfSpeech) T) )
- )
- (set_isa article a n the)
- (set_isa noun movies movie rental rentals customer customers)
- (set_isa noun ID title genre rating custNr name state date number gender)
- (set_isa verb count select show print)
- (set_isa prep of with to in for)
- (set_isa comparator equal after before)
- (set_isa adjective horror scifi romance comedy action male female g pg13 r x)
- (processRequest '(count the number of rentals))
- ;;; defaulting doingExtra to NIL
- (setf doingExtra NIL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement