Author Topic: -={ Challenge }=- Titlecase  (Read 11864 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- Titlecase
« Reply #15 on: October 09, 2010, 05:59:53 AM »
J7:TitleCase    


sharp eyes John  :)

Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

roy_043

  • Water Moccasin
  • Posts: 1747
  • BricsCAD 18
Re: -={ Challenge }=- Titlecase
« Reply #16 on: October 09, 2010, 06:47:20 AM »
Code: [Select]
(defun kg:TitleCase (s / result)
  (setq
    result (list (ascii (strcase (substr s 1 1))))
    s (cdr (vl-string->list (strcase s 'T)))
  )
  (while s
    (if (= (car s) 32)
      (setq
        result (vl-list* (ascii (strcase (chr (cadr s)))) (car s) result)
        s (cddr s)
      )
      (setq
        result (cons (car s) result)
        s (cdr s)
      )
    )
  )
  (vl-list->string (reverse result))
)

123-test: pass.
Code: [Select]
(KG:TITLECASE "it's easy as 123") => "It's Easy As 123"
McMurphy-test: fail. :cry:
Code: [Select]
(KG:TITLECASE "McMurphy is an Irish-American brawler") => "Mcmurphy Is An Irish-american Brawler"

Lee Mac

  • Seagull
  • Posts: 12318
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #17 on: October 09, 2010, 08:22:05 AM »
Code: [Select]
(J7:TITLECASE "it's easy as 123")

Lee Mac

  • Seagull
  • Posts: 12318
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #18 on: October 09, 2010, 08:23:42 AM »
Tidying up my other one:

Code: [Select]
(defun LM:TitleCase3 ( s / n )
  (vl-list->string
    (mapcar
      (function
        (lambda ( x )
          (setq n (if (and (or (not n) (= 32 n)) (< 96 x 123)) (boole 2 x 32) x))
        )
      )
      (vl-string->list (strcase s t))
    )
  )
)

Code: [Select]
(LM:TitleCase3 "it's easy as 123")
"It's Easy As 123"

gile

  • Water Moccasin
  • Posts: 2234
  • Marseille, France
Re: -={ Challenge }=- Titlecase
« Reply #19 on: October 09, 2010, 08:32:55 AM »
Hi,

A very quicky using the most used (by me) string manipulation routines (str2lst and lst2str)
Certainly not the faster one (converting string to an integer list with vl-string->list, manipulating the list and convert it back to a string seems to be the faster way to manipulate string with LISP).

Code: [Select]
(defun gc:TitleCase (str)
  (gc:lst2str
    (mapcar '(lambda (x)
       (strcat (strcase (substr x 1 1)) (strcase (substr x 2) T))
     )
    (gc:str2lst str " ")
    )
    " "
  )
)

(defun gc:str2lst (str sep / pos)
  (if (setq pos (vl-string-search sep str))
    (cons (substr str 1 pos)
  (gc:str2lst (substr str (+ (strlen sep) pos 1)) sep)
    )
    (list str)
  )
)

(defun gc:lst2str (lst sep)
  (if (cdr lst)
    (strcat (car lst) sep (gc:lst2str (cdr lst) sep))
    (car lst)
  )
)
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10376
Re: -={ Challenge }=- Titlecase
« Reply #20 on: October 09, 2010, 08:42:01 AM »
Here is the Title case stripped from my old routine.
Code: [Select]
(defun tcase (txt / spc letter nlst)
  (setq spc  t)   ; space char flag
  (foreach letter (vl-string->list txt)
    (cond
      ((= letter 32) (setq spc t))
      ((and spc (< 96 letter 123))
       (setq letter (- letter 32) spc nil)) ; to upper
      ((< 64 letter 91) (setq letter (+ letter 32))) ; to lower
    )
    (setq nlst (cons letter nlst))
  )
  (vl-list->string (reverse nlst))
)
« Last Edit: October 09, 2010, 09:40:39 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

gile

  • Water Moccasin
  • Posts: 2234
  • Marseille, France
Re: -={ Challenge }=- Titlecase
« Reply #21 on: October 09, 2010, 09:19:23 AM »
Another one

Code: [Select]
(defun gc:titleCase2 (s / f)
  (defun f (l)
    (if l
      (if (and (= 32 (car l)) (< 96 (cadr l) 123))
(cons 32 (cons (- (cadr l) 32) (f (cddr l))))
(cons (car l) (f (cdr l)))
      )
    )
  )
  (vl-list->string
    (f
      (vl-string->list
(strcat (strcase (substr s 1 1)) (strcase (substr s 2) T))
      )
    )
  )
)
Speaking English as a French Frog

roy_043

  • Water Moccasin
  • Posts: 1747
  • BricsCAD 18
Re: -={ Challenge }=- Titlecase
« Reply #22 on: October 09, 2010, 10:03:53 AM »
This one tries to be a little (English) language sensitive:

Code: [Select]
(defun kg:TitleCase2 (s / markers result)
  (setq
    markers
      '(
        " a "
        " an "
        " and "
        " and/or "
        " as "
        " at "
        " in "
        " is "
        " of "
        " on "
        " or "
        " the "
        " to "
        " Mc"
        " Mac"
        " '"
        " ("
        ")("
        " -"
        " "
        "-"
        ; list is language dependent and can of course be extended...
      )
    result ""
    s (strcat " " (strcase s 'T)) ; temporarily add a space
  )
  (while (/= s "")
    (if
      (not
        (vl-some
          '(lambda (a)
            (if (wcmatch s (strcat (strcase a 'T) "*"))
              (if (or (= a " ") (= a (setq a (vl-string-right-trim " " a))))
                (setq
                  result (strcat result a (strcase (substr s (1+ (strlen a)) 1)))
                  s (substr s (+ (strlen a) 2))
                )
                (setq
                  result (strcat result a)
                  s (substr s (1+ (strlen a)))
                )
              )
            )
          )
          markers
        )
      )
      (setq
        result (strcat result (substr s 1 1))
        s (substr s 2)
      )
    )
  )
  (strcat (strcase (substr result 2 1)) (substr result 3))
)

Code: [Select]
(kg:TitleCase2 "the taming of the shrew") => "The Taming of the Shrew"
(kg:TitleCase2 "it's easy as 123") => "It's Easy as 123"
(kg:TitleCase2 "mcmurphy is an irish-american brawler") => "McMurphy is an Irish-American Brawler"
(kg:TitleCase2 "old macdonald had a farm, e-i-e-i-o") => "Old MacDonald Had a Farm, E-I-E-I-O"
(kg:TitleCase2 "he said (and i quote): 'i hate school. it's terrible!'") => "He Said (And I Quote): 'I Hate School. It's Terrible!'"

Edit: this one fails actually:
Code: [Select]
(kg:TitleCase2 "he said (and i quote): 'i hate school. it's terrible!'") => "He Said (And I Quote): 'I Hate School. It's Terrible!'""(And " should be "(and "
« Last Edit: October 09, 2010, 10:12:16 AM by roy_043 »

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9300
Re: -={ Challenge }=- Titlecase
« Reply #23 on: October 09, 2010, 10:04:44 AM »
J7:TitleCase    
sharp eyes John  :)

Thank you Kerry.


So far (I'm keeping tally): I have sharp eyes and a snide fingers.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

Lee Mac

  • Seagull
  • Posts: 12318
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #24 on: October 09, 2010, 10:05:24 AM »
Another

Code: [Select]
(defun LM:Titlecase4 ( s / regex v )
  (setq s (strcase s t) regex (vlax-create-object "VBScript.RegExp"))

  (vlax-put-property regex 'global  actrue)
  (vlax-put-property regex 'pattern "(^|\\s)\\S")

  (vlax-for x (vlax-invoke regex 'execute s)
    (setq s (vl-string-subst (strcase (setq v (vlax-get x 'value))) v s (vlax-get x 'firstindex)))
  )
  (vlax-release-object regex)
  s
)

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9300
Re: -={ Challenge }=- Titlecase
« Reply #25 on: October 09, 2010, 10:10:02 AM »
Code: [Select]
(J7:TITLECASE "it's easy as 123")

Oh no, mine was not built for that. I had very specific criteria I wanted to meet when i built mine. I spend an *hour* designing it and you had already said that we didn't need to design for that.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9300
Re: -={ Challenge }=- Titlecase
« Reply #26 on: October 09, 2010, 10:24:35 AM »
I have my regular tee time with my father in a few so here is a test for someone to fill in and run.

Code: [Select]
( (lambda (/ s)


   (setq s "test string with several spaces"
      s (repeat 7 (setq s (strcat s s))))
   ;; dont operate on a string much larger because the memory gets used up.
   ;;(strlen s)

   (benchmark
     '(

      )
    )
  )
)
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9300
Re: -={ Challenge }=- Titlecase
« Reply #27 on: October 09, 2010, 10:26:51 AM »
roy_043,

I agree with your method but this thread is what I would call "stacked".

Hint: build for raw speed or a quirky algorithm.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

gile

  • Water Moccasin
  • Posts: 2234
  • Marseille, France
Re: -={ Challenge }=- Titlecase
« Reply #28 on: October 09, 2010, 11:31:08 AM »
I was thinking about Regex too,

Here's a C# example:
Code: [Select]
public string TitleCase(string s)
{
    return Regex.Replace(s.ToLower(), "(^[a-z]|\\s[a-z])", x => x.Value.ToUpper());
}
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12318
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #29 on: October 09, 2010, 11:41:30 AM »
Nice Gile -

I tried to be more concise with mine, but struggled to perform the replacement using RegEx  :|