Created
June 3, 2011 14:33
-
-
Save apg/1006422 to your computer and use it in GitHub Desktop.
Test if something is a haiku in elisp -- did I post this before?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'cl) | |
(defconst count-syllables-negative | |
'("cial" "tia" "cius" "cious" "giu" "ion" "iou" "sia$" ".ely$")) | |
(defconst count-syllables-positive | |
'("ia" "riet" "dien" "iu" "io" "li" | |
"[aeiouym]bl$" | |
"[aeiou]\\{3\\}" | |
"^mc" | |
"ism$" ; isms | |
"\\([^aeiouy]\\)\\1l$" ; middle twiddle battle bottle, etc | |
"[^l]lien" ; alien, salient, but not lien, or ebbuillient | |
"^coa[dglx]" ; exception for words coadjutor coagulable coagulate | |
; coalesce coalescent coalition coaxial | |
"[^gq]ua[^aeiou]" | |
"dnt$")) ; couldn't | |
(defun count-syllables (word) | |
(let ((word (downcase word)) | |
(word (replace-regexp-in-string "'" "" word)) | |
(word (replace-regexp-in-string "e$" "" word)) | |
(vowgrouplen (length (remove-if-not '(lambda (x) (> (length x) 0)) | |
(split-string word "[^aeiouy]+"))))) | |
(if (= (length word) 1) | |
1 | |
(progn | |
(let ((pluses (reduce '(lambda (count thing) | |
(if (string-match-p thing word) | |
(1+ count) | |
count)) | |
count-syllables-positive :initial-value 0)) | |
(minuses (reduce '(lambda (count thing) | |
(if (string-match-p thing word) | |
(1- count) | |
count)) | |
count-syllables-negative :initial-value 0))) | |
(or (+ pluses minuses vowgrouplen) 1)))))) | |
(defun haiku-p (s &optional seperator) | |
"Tests if s is a haiku" | |
(interactive) | |
(let ((seperator (or seperator "\n")) | |
(sentences (split-string s seperator))) | |
(equalp (mapcar '(lambda (sent) | |
(reduce '(lambda (count word) | |
(+ count (count-syllables word))) | |
(remove-if-not '(lambda (x) (> (length x) 0)) | |
(split-string sent "[ ,.;:?#-]+")) | |
:initial-value 0)) | |
sentences) | |
'(5 7 5)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment