Author Topic: How to Get First Letter of Words in A Sentence  (Read 1373 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to Get First Letter of Words in A Sentence
« Reply #15 on: January 16, 2024, 11:56:15 AM »
Code - Auto/Visual Lisp: [Select]
  1. _$ (acronym_ALE "What  you  see is  what you get.")
  2. "W Y SI WYG"
Yes  :) (acronym_ALE2 "What  you  see is  what you get.") => "WYSIWYG"

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: How to Get First Letter of Words in A Sentence
« Reply #16 on: January 16, 2024, 12:12:43 PM »
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun acronym ( s / e r )
  2.     (if (setq r (vlax-create-object "vbscript.regexp"))
  3.         (progn
  4.             (setq e
  5.                 (vl-catch-all-apply
  6.                     (function
  7.                         (lambda ( )
  8.                             (vlax-put-property  r 'pattern "([^A-Z]|\\s)*([A-Z])\\S*")
  9.                             (vlax-put-property  r 'global :vlax-true)
  10.                             (vlax-invoke-method r 'replace (strcase (vl-string-trim " " s)) "$2")
  11.                         )
  12.                     )
  13.                 )
  14.             )
  15.             (vlax-release-object r)
  16.             (if (not (vl-catch-all-error-p e)) e)
  17.         )
  18.     )
  19. )
« Last Edit: January 16, 2024, 12:16:48 PM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: How to Get First Letter of Words in A Sentence
« Reply #17 on: January 16, 2024, 01:16:00 PM »
I didn't see the "straight forward" method, so I thought I'd add that (I only glanced at a few of the other offerings so if this method has already been offered, I apologize). The following will just search through a string looking for spaces (this simple method will not work in all cases--like: " 1 @ What you see..." or recognize only chars--but for simple strings/situations, will work fine).

This is a simple loop method that uses only the base autolisp functions (should work in other platforms like OSX and in scripting) but as I stated above is not fool proof like others shown.

Code - Auto/Visual Lisp: [Select]
  1. (defun find-acronym (aString / maxlength counter out)
  2.   ;; This function will iterate a string and assemble an acronym of the first
  3.   ;; letter from each word.
  4.   ;;
  5.   ;; This function assumes that each word is separated by spaces.
  6.   ;;
  7.   ;; PARAMS:
  8.   ;;   aString - A String to search.
  9.   ;;
  10.   ;; Returns:
  11.   ;;   a string
  12.   (setq maxlength  (strlen aString)                    ; -establish length of input string
  13.         counter 1                                      ; -start a counter
  14.         out (substr aString counter 1)                 ; -establish a output string (assume the first char
  15.                                                        ;  is correct).
  16.                                                        ;  NOTE: This is a potential problem (here be dragons);
  17.                                                        ;        We are not "searching/finding" the first char.
  18.         ) ; setq
  19.   (while (< counter maxlength)                         ; -process string one chr at a time
  20.          (if (= " " (substr aString counter 1))        ; -look for the spaces
  21.            (setq out                                   ; -if we have a space, concat the output
  22.                                                        ;  with the next char in the string.
  23.                  (strcat out
  24.                          (substr aString (1+ counter) 1)))
  25.            ) ; if
  26.          (setq counter (1+ counter))                   ; -increment the counterer
  27.          ) ; while
  28.   out                                                  ; -return the output string.
  29.   )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: How to Get First Letter of Words in A Sentence
« Reply #18 on: January 16, 2024, 04:22:01 PM »
Code - C#: [Select]
  1. var str = "Will this fail?";
  2.  
  3. String initials = String.Join("", str.Split(' ').Select(x => x[0].ToString()).ToArray()).ToUpper();
  4.  
  5. Console.WriteLine(initials);
  6.  

added: synchronised code and image  :embarrassed:
« Last Edit: January 16, 2024, 04:53:14 PM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: How to Get First Letter of Words in A Sentence
« Reply #19 on: January 16, 2024, 05:20:50 PM »
I think I have a string split in some of my C stuff already but not an acronym one. And I guess to do it right in C you should create an array to store the items instead of just printing them out as we fly through the string.

Not as concise as C# but still fun.
Code - C++: [Select]
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define LINE_SIZE 500
  5. #define MAX_WORDS 50
  6. #define WORD_SIZE 2
  7.  
  8. int main(int argc, const char * argv[]) {
  9.  
  10.     // Read a line of input from the user (ie stdin)
  11.     char line[LINE_SIZE];
  12.     printf("Please type in a list of words delimted with spaces then hit ENTER:\n");
  13.     while ( fgets(line, LINE_SIZE, stdin) == NULL )
  14.         fprintf(stderr, "You did not enter anything.");
  15.  
  16.     // The list of words -i.e. the array to store the letters.
  17.     char words[MAX_WORDS][WORD_SIZE];                   // -an array of upto 50 words
  18.                                                         //  upto 2 characters each
  19.     int wordCount = 0;                                  // -the number of words in the array.
  20.  
  21.     strncpy(words[wordCount++], line, 1);               // -Pull the first char into the array.
  22.  
  23.     // Loop through the string
  24.     for ( char *c=line; *c; ++c ) {                     // -foreach char in line.
  25.         char ch = *c;                                   // -"ch" is the character
  26.                                                         //  value-at the-char-pointer "c".
  27.         if ( ch==' '                                    // -if this char is a space,
  28.           || ch=='\n'                                   //  or we've reached the EOL char
  29.           ) {
  30.           strncpy(words[wordCount++], (c + 1), 1);      // -Copy the char (+1) to the array.
  31.         }
  32.     }
  33.  
  34.     // Print out the words
  35.     for ( int w=0; w<(wordCount - 1); ++w ) {
  36.         printf("%s", words[w]);
  37.     }
  38.     printf("\n");
  39.     return 0;
  40. }
  41.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to Get First Letter of Words in A Sentence
« Reply #20 on: January 16, 2024, 05:31:30 PM »
F#
Code - F#: [Select]
  1. let acronym (str: string) =
  2.     Regex.Matches(str.ToUpper(), @"^[A-Z]|\W[A-Z]")
  3.     |> Seq.cast<Match>
  4.     |> Seq.fold (fun s m -> s + m.Value.Trim()) ""

Code: [Select]
> acronym "What you see is what you get";;
val it: string = "WYSIWYG"

Edit: use Seq.fold instead of Seq.map Seq.reduce
« Last Edit: January 16, 2024, 05:54:16 PM by gile »
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: How to Get First Letter of Words in A Sentence
« Reply #21 on: January 16, 2024, 05:51:37 PM »
F#
Code - F#: [Select]
  1. let acronym (str: string) =
  2.     Regex.Matches(str.ToUpper(), @"^[A-Z]|\W[A-Z]")
  3.     |> Seq.cast<Match>
  4.     |> Seq.map (fun m -> m.Value.Trim())
  5.     |> Seq.reduce (+)

Code: [Select]
> acronym "What you see is what you get";;
val it: string = "WYSIWYG"

I considered a similar regex, but abandoned the use of \W as an underscore is considered a word and so bruno's earlier example fails.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to Get First Letter of Words in A Sentence
« Reply #22 on: January 16, 2024, 06:20:06 PM »
F#
Code - F#: [Select]
  1. let acronym (str: string) =
  2.     Regex.Matches(str.ToUpper(), @"^[A-Z]|\W[A-Z]")
  3.     |> Seq.cast<Match>
  4.     |> Seq.map (fun m -> m.Value.Trim())
  5.     |> Seq.reduce (+)

Code: [Select]
> acronym "What you see is what you get";;
val it: string = "WYSIWYG"

I considered a similar regex, but abandoned the use of \W as an underscore is considered a word and so bruno's earlier example fails.

Using the same pattern as you:
Code - F#: [Select]
  1. let acronym (str: string) =
  2.     Regex.Replace(str.ToUpper(), @"([^A-Z]|\s)*([A-Z])\S*", "$2")

Code: [Select]
> acronym "_What you see is what you get";;                         
val it: string = "WYSIWYG"
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to Get First Letter of Words in A Sentence
« Reply #23 on: January 16, 2024, 06:23:55 PM »
C#
Code - C#: [Select]
  1. public static string Acronym(string str) =>
  2.     Regex.Replace(str.ToUpper(), @"([^A-Z]|\s)*([A-Z])\S*", "$2");
Speaking English as a French Frog

MeasureUp

  • Bull Frog
  • Posts: 462
Re: How to Get First Letter of Words in A Sentence
« Reply #24 on: January 17, 2024, 01:48:29 AM »
Wow...
Appreciated for all your big guys' inputs.
Your codes definitely help.
I will take some time to learn from the codes.
Thanks again.
:-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: How to Get First Letter of Words in A Sentence
« Reply #25 on: January 17, 2024, 09:14:03 AM »
So last night I got to thinking about how I would actually do this task in a C program (you don't normally just create arrays 50 big this way and 50 big that way; you have to manage memory). And I thought the most efficient method I would employ would be to do is to just bust up the string by the delimiter--create a list of strings--and then loop over the list and grab the specific character. This would allow me to build the acronym from the second, or last, or first character with almost no overhead (other than the initial parsing).

So, here is another method (in lisp).
Code - Auto/Visual Lisp: [Select]
  1. (defun strparse (aStr delim / strList pos)
  2.  ;; This function will take a string and parse it out into a list of
  3.  ;; of strings.
  4.  ;;
  5.  ;; PARAMS:    aStr - A string to parse
  6.  ;;            delim - A string of the delimiter
  7.  ;;
  8.  ;; Ex: (StrParse "This is a test string" " ")
  9.  ;;   > ("This" "is" "a" "test" "string")
  10.  ;;
  11.  ;; History: Unknown
  12.  ;;
  13.   (while
  14.     (setq pos (vl-string-search delim aStr 0))
  15.     (setq strList (cons (substr aStr 1 POS) strList)
  16.           aStr (substr aStr (+ pos 2))))
  17.   (reverse (cons aStr strList)) )
  18.  
  19. ;; Second-letter cypher.
  20.   'strcat
  21.   (mapcar
  22.     '(lambda (x) (strcase (substr x 2 1)))
  23.     (strparse "What you are is see is what you get" " ")
  24.     )
  25.   )
  26.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to Get First Letter of Words in A Sentence
« Reply #26 on: January 18, 2024, 04:01:48 AM »
So, here is another method (in lisp).
(defun strparse (aStr delim / strList pos) ... )

If you do not setq aStr every delim but use a position, this function is faster in long strings with many delimiters
Code: [Select]
; Version 1.20 - 2001 11 08
; Description:  convert a string into a list

; Arguments:
;   InpStr = string [STR]
;   CarDlm = delimiter > 1 or more characters [STR]
;
; Examples:
;   (ALE_String_ToListStrDlm "\"Abc\"][1.0][1][Xyz" "][")
;   => ("\"Abc\"" "1.0" "1" "Xyz")
;   (ALE_String_ToListStrDlm "\"Abc\"][1.0][1][Xyz][(][99" "][")
;   => ("\"Abc\"" "1.0" "1" "Xyz" "(" "99")
;   (ALE_String_ToListStrDlm "\"Abc\"][1.0][1][Xyz][)][99" "][")
;   => ("\"Abc\"" "1.0" "1" "Xyz" ")" "99")
;   (ALE_String_ToListStrDlm "\"Abc\"][1.0][1][Xyz][\"][99" "][")
;   => ("\"Abc\"" "1.0" "1" "Xyz" "\"" "99")
;   (ALE_String_ToListStrDlm "\"Abc\"][1.0][1][Xyz][,][99" "][")
;   => ("\"Abc\"" "1.0" "1" "Xyz" "," "99")
;   (ALE_String_ToListStrDlm "\"Abc\"][1.0][1][Xyz][.][99" "][")
;   => ("\"Abc\"" "1.0" "1" "Xyz" "." "99")
;   (ALE_String_ToListStrDlm "\"Abc\"][1.0][1][Xyz][.99" "][")
;   => ("\"Abc\"" "1.0" "1" "Xyz" ".99")
; Return Values: [LIST]
;
(defun ALE_String_ToListStrDlm (InpStr StrDlm / SttPos TmpPos DlmLng TmpLst)
  (cond
    ( (/= (type InpStr) (type StrDlm) 'STR) )
    ( (= InpStr StrDlm) '("") )
    ( (setq
        DlmLng (strlen StrDlm)   SttPos 0
        TmpPos (vl-string-search StrDlm InpStr SttPos)
      )
      (while TmpPos
        (setq
          TmpLst (cons (substr InpStr (1+ SttPos) (- TmpPos SttPos)) TmpLst)
          SttPos (+ TmpPos DlmLng)
          TmpPos (vl-string-search StrDlm InpStr SttPos)
        )
      )
      (reverse (cons (substr InpStr (1+ SttPos)) TmpLst))
    )
  )
)