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

0 Members and 1 Guest are viewing this topic.

gile

  • Water Moccasin
  • Posts: 2261
  • Marseille, France
Re: -={ Challenge }=- Titlecase
« Reply #30 on: October 09, 2010, 12:15:42 PM »
The VBScript Regex.Replace method only allows to replace a specified string (or pattern) by another specified string.

The .NET Regex.Replace method is overloaded (means many definitions with different arguments) and some allows to use a MatchEvaluator delegate as argument (a method defined with requiered argument(s) and return value).
Since NET 3.0 version delegates can be specified as 'lambda expressions' which allows more concise (and cryptic ?) code:
x => x.Value.ToUpper() (where x is a Match object: the MatchEvaluator argument).

A 'more traditionnal' way should have been to define the delegate as an external function:
Code: [Select]
public string TitleCase(string s)
{
    return Regex.Replace(s.ToLower(), "(^[a-z]|\\s[a-z])", MatchToUpper); }));
}

private string MatchToUpper(Match m)
{
    return m.Value.ToUpper();
}

Or using an 'anonymous method' (more like the lambda LISP function):
Code: [Select]
private static string TitleCase(string s)
{
    return Regex.Replace(s.ToLower(), "(^[a-z]|\\s[a-z])", delegate(Match m) { return m.Value.ToUpper(); });
}

Maybe it's not the right forum for this (Please CAB, don't cry...)
« Last Edit: October 09, 2010, 12:46:17 PM by gile »
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #31 on: October 09, 2010, 02:02:54 PM »
Thanks for the information Gile, I had read up about the MatchEvaluator argument, and its a shame this cannot be supplied to the VBScript Regex Object - the closest I can get to that behaviour is to use the "$n" symbols (n integer) in the replace string, which reference the matches 'remembered' when using parentheses in the pattern string.

Example, changing the order of matched strings:

Code: [Select]
(RegRep "$3$2$1" "(\\S)(\\S)(\\S)" "abc abc abc")
"cba cba cba"



roy_043

  • Water Moccasin
  • Posts: 1802
  • BricsCAD 18
Re: -={ Challenge }=- Titlecase
« Reply #32 on: October 09, 2010, 04:19:41 PM »
Improved version of kg:TitleCase2:

Code: [Select]
(defun kg:TitleCase3 (s / wordBeginMarkersLst prefixesWithFollowingCapLst wordsNoCapsMatchString tmp lst)
  ;; lists and wordsNoCapsMatchString are language dependent and can of course be extended...
  (setq
    s (strcase s 'T)
    wordBeginMarkersLst ; the order of this list is important
      '(
        " '"
        " "
        "-"
        "("
      )
    prefixesWithFollowingCapLst
      '(
        "Mc"
        "Mac"
      )
    wordsNoCapsMatchString "a,an,and,and/or,as,at,in,is,it,of,on,or,the,to"
    tmp ""
  )
  (while (/= s "")
    (if
      (not
        (vl-some
          '(lambda (a)
            (if (wcmatch s (strcat a "*"))
              (setq
                lst (vl-list* (cons 0 a) (cons 1 tmp) lst)
                tmp ""
                s (substr s (1+ (strlen a)))
              )
            )
          )
          wordBeginMarkersLst
        )
      )
      (setq
        tmp (strcat tmp (substr s 1 1))
        s (substr s 2)
      )
    )
  )
  (setq tmp
    (apply
      'strcat
      (mapcar
        '(lambda (a)
          (cond
            ((or (zerop (car a)) (wcmatch (cdr a) wordsNoCapsMatchString))
              (cdr a)
            )
            (
              (vl-some
                '(lambda (b)
                  (if (wcmatch (cdr a) (strcase (strcat b "*") 'T))
                    (setq tmp (strcat b (strcase (substr (cdr a) (1+ (strlen b)) 1)) (substr (cdr a) (+ (strlen b) 2))))
                  )
                )
                prefixesWithFollowingCapLst
              )
              tmp
            )
            ('T
              (strcat (strcase (substr (cdr a) 1 1)) (substr (cdr a) 2))
            )
          )
        )
        (reverse (cons (cons 1 tmp) lst))
      )
    )
  )
  (strcat (strcase (substr tmp 1 1)) (substr tmp 2))
)

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

@ Se7en: Pretty quirky I would say :evil:. But undoubtedly very slow...

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #33 on: October 09, 2010, 05:51:38 PM »
One more

Code: [Select]
(defun LM:Titlecase5 ( s / a s l n c )
  (setq a (vl-string->list (strcase s t)) s (vl-string-subst (strcase (chr (car a))) (chr (car a)) s 0) l (length a))
 
  (while (setq x (vl-position 32 a))
    (setq n (length a) a (cdr (member 32 a)) c (chr (car a)) s (vl-string-subst (strcase c) c s (+ 1 (- l n) x)))
  )
)

roy_043

  • Water Moccasin
  • Posts: 1802
  • BricsCAD 18
Re: -={ Challenge }=- Titlecase
« Reply #34 on: October 10, 2010, 06:56:15 AM »
Inspired by Lee's use of the member function:

Code: [Select]
(defun kg:TitleCaseQuirk (s / result tmp)
  (setq s (vl-string->list (strcase s 'T)))
  (substr
    (apply
      'strcat
      (reverse
        (while s
          (setq tmp (reverse s))
          (while (member 32 tmp) (setq tmp (cdr (member 32 tmp))))
          (setq s (cdr (member 32 s)))
          (setq result (cons (strcat " " (strcase (chr (car (setq tmp (reverse tmp))))) (vl-list->string (cdr tmp))) result))
        )
      )
    )
    2
  )
)

roy_043

  • Water Moccasin
  • Posts: 1802
  • BricsCAD 18
Re: -={ Challenge }=- Titlecase
« Reply #35 on: October 10, 2010, 09:12:54 AM »
And another:

Code: [Select]
(defun kg:TitleCase4 (s / result)
  (setq s (reverse (vl-string->list (strcase s 'T))))
  (while s
    (setq result (cons (if (and (member (cadr s) '(32 nil)) (< 96 (car s) 123)) (- (car s) 32) (car s)) result))
    (setq s (cdr s))
  )
  (vl-list->string result)
)

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #36 on: October 10, 2010, 09:38:08 AM »
Nice ideas Roy  :-)

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #37 on: October 10, 2010, 09:57:34 AM »
A quick test:

Code: [Select]
String Length: 992
Benchmarking .............Elapsed milliseconds / relative speed for 1024 iteration(s):

    (LM:TITLECASE3 S)..........1076 / 19.95 <fastest>
    (LM:TITLECASE2 S)..........1233 / 17.41
    (LM:TITLECASE5 S)..........1342 / 15.99
    (GC:TITLECASE2 S)..........1435 / 14.96
    (KG:TITLECASE S)...........1436 / 14.95
    (CAB:TCASE S)..............1684 / 12.75
    (KG:TITLECASE4 S)..........1857 / 11.56
    (GC:TITLECASE S)...........1950 / 11.01
    (LM:TITLECASE4 S)..........7768 / 2.76
    (DB:PROPER S)..............8393 / 2.56
    (KG:TITLECASEQUIRK S).....21465 / 1.00 <slowest>

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9484
Re: -={ Challenge }=- Titlecase
« Reply #38 on: October 10, 2010, 11:35:48 AM »
Mine wasn't good enough to test (or am i not to partake in any reindeer games)?
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #39 on: October 10, 2010, 11:38:36 AM »
Mine wasn't good enough to test (or am i not to partake in any reindeer games)?

Yours wasn't comparing apples with apples (see #17), same reason my first one doesn't appear in the list either.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9484
Re: -={ Challenge }=- Titlecase
« Reply #40 on: October 10, 2010, 01:25:06 PM »
So i wasted an hour deriving a clever function to your criteria and its all for not...all i have to say is: WTF!

roy_043 made this point right away and you dismissed it.


TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #41 on: October 10, 2010, 01:34:08 PM »
So i wasted an hour deriving a clever function to your criteria and its all for not...all i have to say is: WTF!

It hardly met the criteria if it can handle some strings but not others, how much time you spend on it and what you do with your time is up to you.

roy_043 made this point right away and you dismissed it.

What are you talking about? Roy made that point, and so I changed my code to account for it.
« Last Edit: October 10, 2010, 01:38:52 PM by Lee Mac »

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9484
Re: -={ Challenge }=- Titlecase
« Reply #42 on: October 10, 2010, 02:34:57 PM »
So here is my understanding (recap);

you state a challenge.
someone (roy_043) asks for clarification.
you state that we are to be literal ("remove ambiguity").
I state that being literal would be easier to program.
 (BTW, I added a "literal" meaning to your statement about being ambiguous thus creating a joke)
you insult me.
 (and ignore me)
you determine that one item in the sea of `ambiguity' is worth addressing.
 (part of the humor in my joke)

...again: WTF! and I'll even add: screw-u.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #43 on: October 10, 2010, 02:52:04 PM »
The clarification that Roy asked for is beside the point - your function (and my first one) cannot handle strings containing numbers (amongst other characters), hence they cannot be compared with those which do handle such. Roy pointed this out straight away (see post #5), and I corrected my code to account for it. You seem outraged that the same bug be pointed out in your "clever" code.

you insult me.

Quote
Quote
A little crabby are we?
I've got better things to do than rise to your snide comments.

I would hardly call that an 'insult', this however:

...again: WTF! and I'll even add: screw-u.

is quite insulting, at which point I shall depart from this conversation.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9484
Re: -={ Challenge }=- Titlecase
« Reply #44 on: October 10, 2010, 03:10:11 PM »
So you get to determine what is insulting to me?!

Go ahead and bail on the conversation (I didn't bail on your challenge even though i felt insulted).
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org