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

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 462
How to Get First Letter of Words in A Sentence
« on: January 15, 2024, 02:38:38 AM »
I can't figure it out.

Here is an example:
What you see is what you get.
I want to get something like this:
WYSIWYG

Thanks in advance.

ribarm

  • Gator
  • Posts: 3282
  • Marko Ribar, architect
Re: How to Get First Letter of Words in A Sentence
« Reply #1 on: January 15, 2024, 03:12:50 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:firstletters ( / txt lett pos )
  2.   (setq txt (getstring t "\nInput sentence : "))
  3.   (setq lett (cons (substr txt 1 1) lett))
  4.   (while (setq pos (vl-string-position 32 txt))
  5.     (setq lett (cons (substr txt (+ 2 pos) 1) lett))
  6.     (setq txt (substr txt (+ 2 pos)))
  7.   )
  8. )
  9.  

Code: [Select]
: FIRSTLETTERS
Input sentence : What you see is what you get.
"WYSIWYG"
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: How to Get First Letter of Words in A Sentence
« Reply #2 on: January 15, 2024, 03:20:13 AM »
Code: [Select]
(defun ALE_String_ToList (InpStr CarDlm TrueFl / SttPos EndPos TmpLst TmpStr)
  (setq
    CarDlm (ascii CarDlm)   SttPos 0
    EndPos (vl-string-position CarDlm InpStr)
  )
  (while EndPos
    (setq
      TmpStr (substr InpStr (1+ SttPos) (- EndPos SttPos))
      SttPos (1+ EndPos) EndPos (vl-string-position CarDlm InpStr SttPos)
    )
    (and
      (or (/= TmpStr "") TrueFl)
      (setq TmpLst (cons TmpStr TmpLst))
    )
  )
  (if (or (/= (setq TmpStr (substr InpStr (1+ SttPos))) "") TrueFl)
    (reverse (cons TmpStr TmpLst))
    (reverse TmpLst)
  )
)
(defun Foo (InpStr / OutStr)
  (setq OutStr "")
  (foreach ForElm (ALE_String_ToList InpStr " " nil)
    (setq OutStr (strcat OutStr (strcase (substr ForElm 1 1))))
  )
)
(Foo "What you see is what you get.") => "WYSIWYG"

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: How to Get First Letter of Words in A Sentence
« Reply #3 on: January 15, 2024, 03:53:29 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun acronym ( s )
  2.     (   (lambda ( l )
  3.             (vl-list->string
  4.                 (vl-remove nil
  5.                     (mapcar '(lambda ( a b ) (if (and (= 32 a) (< 64 (setq b (logand (~ 32) b)) 91)) b))
  6.                         (cons 32 l) l
  7.                     )
  8.                 )
  9.             )
  10.         )
  11.         (vl-string->list s)
  12.     )
  13. )

bruno_vdh

  • Newt
  • Posts: 107
Re: How to Get First Letter of Words in A Sentence
« Reply #4 on: January 15, 2024, 09:34:59 AM »
Hello,
Another...
Code - Auto/Visual Lisp: [Select]
  1. (defun acronym (s / foo)
  2.  
  3.   (defun foo (l)
  4.     (cond ((null l) nil)
  5.           ((< 64 (car l) 91) (cons (car l) (foo (cdr (member 32 (cdr l))))))
  6.           (T (foo (cdr l)))
  7.     )
  8.   )
  9.  
  10.   (vl-list->string (foo (vl-string->list (strcase s))))
  11. )

Code: [Select]
_$ (acronym "  _What you see is   what you get.")
"WYSIWYG"   
   
« Last Edit: January 15, 2024, 09:51:22 AM by bruno_vdh »

dexus

  • Bull Frog
  • Posts: 209
Re: How to Get First Letter of Words in A Sentence
« Reply #5 on: January 15, 2024, 10:41:04 AM »
And anotherone  :-D
Code - Auto/Visual Lisp: [Select]
  1. (defun acronym (str / char rtn skipNext)
  2.   (foreach char (vl-string->list (strcase str))
  3.     (cond
  4.       ((= 32 char)
  5.         (setq skipNext nil)
  6.       )
  7.       ((and (not skipNext) (< 64 char 91))
  8.         (setq rtn (cons char rtn)
  9.               skipNext t)
  10.       )
  11.     )
  12.   )
  13.   (vl-list->string (reverse rtn))
  14. )

CodeDing

  • Newt
  • Posts: 51
Re: How to Get First Letter of Words in A Sentence
« Reply #6 on: January 15, 2024, 03:05:01 PM »
Code - Auto/Visual Lisp: [Select]
  1. ;; First Letter of Word(s)
  2. ;; str - string of word(s)
  3. ;; returns - string, an acronym [capitalized] of all of the first letters of each 'word'
  4. (defun FLOW (str / prevSpace)
  5.   (vl-list->string
  6.     (vl-remove nil
  7.       (mapcar
  8.         '(lambda (x)
  9.           (cond
  10.             ((and (null prevSpace) (= 32 x)) (not (setq prevSpace t)))
  11.             ((and prevSpace (<= 65 x 90))) (setq prevSpace nil) x)
  12.           )
  13.         )
  14.         (cons 32 (vl-string->list (strcase str)))
  15.       )
  16.     )
  17.   )
  18. )

Code: [Select]
Command: (FLOW "  What you $see is !@#$%what you get... .")
"WYSIWYG"
~DD
Senior CAD Tech & AI Specialist
Need AutoLisp help?
Try my custom GPT 'AutoLISP Ace'

CodeDing

  • Newt
  • Posts: 51
Re: How to Get First Letter of Words in A Sentence
« Reply #7 on: January 15, 2024, 09:20:41 PM »
I also got my GPT (AutoLISP ACE) to get an answer that works (but it does NOT cover all exceptions like the other codes above). Could be useful in the future if you pay for ChatGPT.

AutoLISP Ace:
https://chat.openai.com/g/g-Zt0xFNpOH-autolisp-ace

Chat return:
Quote
The issue you are encountering is due to the way the function processes spaces and extracts words. Let's modify the function to correctly handle spaces and ensure it generates the full acronym from the phrase:

(defun C:CreateAcronym ( / phrase acronym word startPos endPos)
    (setq phrase (getstring T "\nEnter phrase: ")) ; Enable spaces in input
    (setq phrase (strcat phrase " ")) ; Append a space to handle the last word
    (setq acronym "") ; Initialize acronym as an empty string
    (setq startPos 0) ; Initialize starting position for substring

    (while (setq endPos (vl-string-search " " phrase startPos)) ; Find next space
        (setq word (substr phrase (+ startPos 1) (- endPos startPos))) ; Extract word
        (setq acronym (strcat acronym (strcase (substr word 1 1)))) ; Add the first letter to the acronym
        (setq startPos (+ endPos 1)) ; Update startPos for next word
    )

    (prompt (strcat "\nAcronym: " acronym)) ; Display the acronym
    (princ)
)

In this revised function, the while loop searches for the next space in the phrase starting from the current startPos. Each word is extracted using substr based on the positions of startPos and endPos, and the first character of each word is added to the acronym. The startPos is then updated to the position after the found space for the next iteration.

Load this updated function into AutoCAD and run it by typing CreateAcronym. It should now correctly generate the full acronym for the input phrase "what you see is what you get" as "WYSIWYG".
~DD
Senior CAD Tech & AI Specialist
Need AutoLisp help?
Try my custom GPT 'AutoLISP Ace'

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: How to Get First Letter of Words in A Sentence
« Reply #8 on: January 16, 2024, 05:01:30 AM »
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun acronym ( s / l r )
  2.     (setq l (cons 32 (vl-string->list (strcase s))))
  3.     (while (setq l (vl-member-if (function (lambda ( x ) (< 64 x 91))) (cdr (member 32 l))))
  4.         (setq r (cons (car l) r))
  5.     )
  6.     (vl-list->string (reverse r))
  7. )

bruno_vdh

  • Newt
  • Posts: 107
Re: How to Get First Letter of Words in A Sentence
« Reply #9 on: January 16, 2024, 06:06:35 AM »
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun acronym ( s / l r )
  2.     (setq l (cons 32 (vl-string->list (strcase s))))
  3.     (while (setq l (vl-member-if (function (lambda ( x ) (< 64 x 91))) (cdr (member 32 l))))
  4.         (setq r (cons (car l) r))
  5.     )
  6.     (vl-list->string (reverse r))
  7. )


Hello,
I think a simple if is better :grinwink:
Code - Auto/Visual Lisp: [Select]
  1. (defun acronym (s / l r)
  2.   (setq l (cons 32 (vl-string->list (strcase s))))
  3.   (while (setq l (cdr l))
  4.     (if (< 64 (car l) 91)
  5.       (setq r (cons (car l) r) l (member 32 l))
  6.     )
  7.   )
  8.   (vl-list->string (reverse r))
  9. )
  10.  

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: How to Get First Letter of Words in A Sentence
« Reply #10 on: January 16, 2024, 09:35:48 AM »


I don't know if it's a coherent version...
Code: [Select]

(defun acronym_ALE (s / l r)
  (setq
    l (vl-string->list (strcase (vl-string-trim " " s)))
    r (list (car l))
  )
  (while (setq l (cdr l))
    (if (= 32 (car l))
      (setq r (cons (cadr l) r) l (member 32 l))
    )
  )
  (vl-list->string (reverse r))
)
Code: [Select]
(setq Astring "@LeeVdh What you see is what you get.")
(acronym_ALE Astring) => "@WYSIWYG"
(acronym_Lee Astring) => "LWYSIWYG"

bruno_vdh

  • Newt
  • Posts: 107
Re: How to Get First Letter of Words in A Sentence
« Reply #11 on: January 16, 2024, 10:07:51 AM »

I don't know if it's a coherent version...
Code: [Select]
(setq Astring "@LeeVdh What you see is what you get.")
(acronym_ALE Astring) => "@WYSIWYG"
(acronym_Lee Astring) => "LWYSIWYG"

I thought the request was about alphabetic characters...

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: How to Get First Letter of Words in A Sentence
« Reply #12 on: January 16, 2024, 11:31:44 AM »

I don't know if it's a coherent version...
Code: [Select]
(setq Astring "@LeeVdh What you see is what you get.")
(acronym_ALE Astring) => "@WYSIWYG"
(acronym_Lee Astring) => "LWYSIWYG"

I thought the request was about alphabetic characters...
Yes but... 

Code: [Select]
; for multi spaced strings:


(defun acronym_ALE2 (s / l r)
  (setq
    l (vl-string->list (strcase (vl-string-trim " " s)))
    r (list (car l))
  )
  (while (setq l (cdr l))
    (and
      (= 32 (car l)) (/= 32 (cadr l))
      (setq r (cons (cadr l) r) l (member 32 l))
    )
  )
  (vl-list->string (reverse r))
)

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: How to Get First Letter of Words in A Sentence
« Reply #13 on: January 16, 2024, 11:40:43 AM »
Code - Auto/Visual Lisp: [Select]
  1. _$ (acronym_ALE "What  you  see is  what you get.")
  2. "W Y SI WYG"

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: How to Get First Letter of Words in A Sentence
« Reply #14 on: January 16, 2024, 11:45:53 AM »
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun acronym ( s / l r )
  2.     (setq l (vl-string->list (strcase s)))
  3.     (while
  4.         (cond
  5.             (   (< 64 (car l) 91) (setq r (cons (car l) r) l (member 32 l)))
  6.             (   (setq l (cdr l)))
  7.         )
  8.     )
  9.     (vl-list->string (reverse r))
  10. )