LenguajeNatural.lsp



; _____________________________________________________________
; Programa que descompone una frase en sus elementos atOmicos
;
; Iniciar la ejecuciOn con la funciOn:		(frase)
;
;
; _____________________________________________________________
; Lista de datos iniciales
;______________________________________________________________
(setq L_verbos '(corre corta brinca camina))
(setq L_articulos '(el la))
(setq L_adverbios '(rapidamente))
(setq L_preposiciones '(hacia sobre))
(setq L_adjetivos '(alta rojo))
(setq L_sustantivos '(casa jardin joven perro))

;______________________________________________________________

; Frase de prueba
; (setq Texto '(el perro corre rapidamente sobre el jardin))
; (setq Texto '(el joven corre rapidamente hacia la alta casa))


; Objetivo: Indicar si Elemento es un Objeto definido
;	Si no es regresa nil
;	Si sI es regresa una lista -funciOn member-
;_____________________________________________________________
(defun Es_Verbo(Elemento)	(member Elemento L_verbos) )
(defun Es_Articulo(Elemento)	(member Elemento L_articulos) )
(defun Es_Adverbio(Elemento)	(member Elemento L_adverbios) )
(defun Es_Preposicion(Elemento)	(member Elemento L_preposiciones) )
(defun Es_Adjetivo(Elemento)	(member Elemento L_adjetivos) )
(defun Es_Sustantivo(Elemento)	(member Elemento L_sustantivos) )

(defun frase ()
	(print "Introduce una frase: ")
	(setq Texto (Lee-frase))
	(setq Resto (PN Texto))
	(PV Resto)
)

; ============================================================================================
; ============================================================================================
; PREDICADO NOMINAL
;	(1) PN     --->    articulo + sustantivo
;	(2) PN     --->    articulo + adjetivo + sustantivo
;	(3) PN     --->    preposicion + PN
;_____________________________________________________________________________________________
(defun PN (Frase)
;       (1) PN     --->    articulo + sustantivo		<=====
;           ----------------------------------------------------
        (setq Palabra_1 (nth 0 Frase))
	(setq Palabra_2 (nth 1 Frase))

	(if
		(and
			(eql (not (Es_Articulo Palabra_1)) nil)
			(eql (not (Es_Sustantivo Palabra_2)) nil)
		)
		(when t
			(print (format nil "Articulo   ==> ~a" Palabra_1))
			(print (format nil "Sustantivo ==> ~a" Palabra_2))
			(cdr (cdr Frase))
		)
;       (2) PN     --->    articulo + adjetivo + sustantivo	<=====
;           ----------------------------------------------------
		(when t
			(setq Palabra_3 (nth 2 Frase))
			(if
				(and (and
					(eql (not (Es_Articulo Palabra_1)) nil)
					(eql (not (Es_Adjetivo Palabra_2)) nil)
					)
					(eql (not (Es_Sustantivo Palabra_3)) nil)
				)
				(when t
					(print (format nil "Articulo   ==> ~a" Palabra_1))
					(print (format nil "Adjetivo   ==> ~a" Palabra_2))
					(print (format nil "Sustantivo ==> ~a" Palabra_3))
					(cdr (cdr (cdr Frase)))
				)
;       (3) PN     --->    preposicion + PN			<=====
;           ----------------------------------------------------
				(when t
					(if (eql (not (Es_Preposicion Palabra_1)) nil)
						(when t
							(print (format nil "Preposicion ==> ~a" Palabra_1))
							(setq Resto (cdr Frase))
							(setq Resto (PN Resto))
						)
						(print 'frase_no_reconocida)
					)
				)
			)
		)
	)
)

; ============================================================================================
; ============================================================================================
; PREDICADO VERBAL
;	(1) PV      --->    verbo + PN
;	(2) PV      --->    verbo + adverbio + PN
;	(3) PV      --->    verbo + adverbio
;	(4) PV      --->    verbo
;_____________________________________________________________________________________________
(defun PV (Frase)
;       (1) PV     --->    verbo + PN                     <=====
;           ----------------------------------------------------
        (setq Palabra_1 (nth 0 Frase))
	(setq Palabra_2 (nth 1 Frase))
        (setq Resto (cdr Frase))		; DESPUES DE LA PRIMERA PALABRA.
						; SE USA PARA EL CASO (1)

        (if
        	(and
			(eql (not (Es_Verbo Palabra_1)) nil)
                        (ES_PN Resto)
                )
                (when t
                        (print (format nil "Verbo      ==> ~a" Palabra_1))
                        (PN Resto)
                )
;       (2) PV     --->    verbo + adverbio + PN          <=====
;           ----------------------------------------------------
                (when t
			(if
				(and
					(eql (not (Es_Verbo Palabra_1)) nil)
					(and
						(eql (not (Es_Adverbio Palabra_2)) nil)
						(ES_PN (cdr Resto))
					)
				)
				(when t
					(print (format nil "Verbo      ==> ~a" Palabra_1))
					(print (format nil "Adverbio   ==> ~a" Palabra_2))
					(PN (cdr Resto))
				)
;       (3) PV     --->    verbo + adverbio               <=====
;           ----------------------------------------------------
				(when t
					(if
						(and
							(eql (not (Es_Verbo Palabra_1)) nil)
							(eql (not (Es_Adverbio Palabra_2)) nil)
						)
						(when t
							(print (format nil "Verbo      ==> ~a" Palabra_1))
							(print (format nil "Adverbio   ==> ~a" Palabra_2))
						)
;       (4) PV     --->    verbo                          <=====
;           ----------------------------------------------------
						(when t
							(if
								(eql (not (Es_Verbo Palabra_1)) nil)
								(print (format nil "Verbo      ==> ~a" Palabra_1))
								(print 'frase_no_reconocida)
							)
						)
                                        )
				)
			)
		)
	)
)

;________________________________________________________________________
;          Solo se identifica si es un articulo o preposiciOn
;          lo cual indica que inicia un predicado nominal
;------------------------------------------------------------------------
(defun ES_PN (Frase)
	(if
		(or
			(eql (not (Es_Articulo (car Frase))) nil)
			(eql (not (Es_Preposicion (car Frase))) nil)
		) t nil
	)
)

;
;________________________________________________________________________
; Se introduce una cadena de texto y la convierte a una lista de palabras
;------------------------------------------------------------------------
;
; FUNCIONES QUE SE UTILIZAN
; string-trim			Recorta los caracteres extremos indicados entre comillas
; with-input-from-string 	Convierte una cadena de texto en una lista de palabras
;				construyendo una variable flujo entre la cadena y READ
(defun Lee-frase ()
	(with-input-from-string (Cadena (string-trim " ." (read-line)))
		(do ((Palabra (read Cadena nil) (read Cadena nil)) (oracion nil))
			((not Palabra) (return (reverse oracion)))
			(push Palabra oracion)
		)
	)
)