Author Topic: Sentence to Words  (Read 5341 times)

0 Members and 1 Guest are viewing this topic.

HofCAD

  • Guest
Sentence to Words
« on: March 07, 2011, 06:55:20 AM »
I would like to convert a mtext or a text sentence to single words.
I have some beauty problems with it.

Regards HofCAD CSI.
« Last Edit: March 10, 2011, 09:41:33 AM by HofCAD »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Sentence to Words
« Reply #1 on: March 07, 2011, 08:41:34 AM »
As you have discovered the task is relatively simple if the text does not contain formatting changes & special characters.
the explode should produce manageable plain text. Once you have that you must parse the remaining strings using space as
the delimiter. Then use a pointer to hold the location of the text initially. This also can be troublesome if not LEFT justified.
Then use TextBox to get the the end of that piece of text. You may need to strip the special character before using textbox.
Then increment your pointer to place the next word. Note that Tim Willey had lisp that dealt with text positions, I'll see
if I can find it. Also I would use the style & size from the original (plain text) to Entitymake the new words.

A quick look at your code & you have most of this done. 8-)
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.

HofCAD

  • Guest
Re: Sentence to Words
« Reply #2 on: March 08, 2011, 06:57:15 AM »
When I explode an Mtext, than the new Text entity's have their lineweights
and linetypes always ByLayer.
This is not happening with the entity's color.
Therefore I can't use:
Code: [Select]
   (if (assoc 6 ed)
     (cons 6 (cdr (assoc 6 ed)))
     (cons 1 (nth n TxtLst))
   )
   (if (assoc 370 ed)
     (cons 370 (cdr (assoc 370 ed)))
     (cons 1 (nth n TxtLst))
   )
Regards HofCAD CSI.
« Last Edit: March 10, 2011, 09:40:30 AM by HofCAD »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Sentence to Words
« Reply #3 on: March 08, 2011, 07:09:20 AM »
HofCAD,

On the side, note that:

Code: [Select]
(cons x (cdr (assoc x elist)))

is the same as:

Code: [Select]
(assoc x elist)

pBe

  • Bull Frog
  • Posts: 402
Re: Sentence to Words
« Reply #4 on: March 08, 2011, 07:30:24 AM »
HofCAD,

On the side, note that:

Code: [Select]
(cons x (cdr (assoc x elist)))

is the same as:

Code: [Select]
(assoc x elist)

uhuh.... I remember that so well  :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Sentence to Words
« Reply #5 on: March 08, 2011, 07:49:05 AM »
Thought I'd take a stab at it, so here is my attempt for Single-Line Left-Justified Text:

Code: [Select]
(defun c:S2W ( / _str->lst _textwidth ang e elist i l spc ss str txt ) (vl-load-com)

  (defun _str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
      (cons (substr str 1 pos) (_str->lst (substr str (+ pos 1 (strlen del))) del))
      (list str)
    )
  )

  (defun _textwidth ( el )
    ( (lambda ( box ) (- (caadr box) (caar box))) (textbox el))
  )
 
  (if (setq ss (ssget "_:L" '((0 . "TEXT") (72 . 0) (73 . 0))))
    (repeat (setq i (sslength ss))
      (setq elist (entget (ssname ss (setq i (1- i)))) txt nil)
     
      (foreach x (_str->lst (cdr (assoc 1 elist)) " ")
        (setq txt
          (cons
            (cons
              (setq e (entmakex (subst (cons 1 x) (assoc 1 elist) elist)))
              (_textwidth (entget e))
            )
            txt
          )
        )
      )
      (setq spc (/ (- (_textwidth elist) (apply '+ (mapcar 'cdr txt))) (float (if (zerop (setq l (1- (length txt)))) 1. l)))
            txt (reverse txt)
            ang (cdr (assoc 50 elist))
      )
      (mapcar
        (function
          (lambda ( a b / e1 e2 ) (setq e1 (entget (car a)) e2 (entget (car b)))
            (entmod
              (subst (cons 10 (polar (cdr (assoc 10 e1)) ang (+ (cdr a) spc))) (assoc 10 e2) e2)
            )
          )
        )
        txt (cdr txt)
      )
      (entdel (cdr (assoc -1 elist)))
    )
  )

  (princ)
)

Multiple Lines and/or other Justifications will take more thought...

EDIT: Fixed dodgy function naming...
« Last Edit: March 08, 2011, 09:21:13 AM by Lee Mac »

HofCAD

  • Guest
Re: Sentence to Words
« Reply #6 on: March 08, 2011, 08:59:30 AM »
Thanks Lee Mac.

But that doesn't help with the real problem.
Your program is more sophisticated, but has less functionality.
Because, when there are only Text entity's, I can put
Code: [Select]
    (if (assoc 6 ed)
      (assoc 6 ed)
      (cons 1 (nth n TxtLst))
    )
    (if (assoc 370 ed)
      (assoc 370 ed)
      (cons 1 (nth n TxtLst))
    )
in my code.

By the way if you run your program and in the selection
there is a sentence with only one word, you get:
; error: divide by zero

Regards HofCAD CSI.
« Last Edit: March 08, 2011, 09:16:57 AM by HofCAD »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Sentence to Words
« Reply #7 on: March 08, 2011, 09:20:06 AM »
But that doesn't help with the real problem.
Your program is more sophisticated, but has less functionality.

True - it was only for single-line left justified text, but I wanted to offer an alternative for how to approach the text creation and placement.

By the way if you run your program and in the selection
there is a sentence with only one word, you get:
; error: divide by zero

Good catch - updated.

HofCAD

  • Guest
Re: Sentence to Words
« Reply #8 on: March 08, 2011, 03:29:35 PM »
Thanks.

I think, that I solve the problem of the lineweights
and the linetypes at my own way.

Regards HofCAD CSI.

Code updated.


« Last Edit: March 08, 2011, 05:09:26 PM by HofCAD »

HofCAD

  • Guest
Re: Sentence to Words
« Reply #9 on: March 10, 2011, 09:29:51 AM »
There were still problems when the Single-Line Text was not Left-Justified (left-aligned).
I think you can solve these problems by converting first the Text entities
to Mtext entities.
I use TXT2MTXT of DotSoft.
http://www.theswamp.org/index.php?topic=5909.msg73232#msg73232
After using S2W all modified Text and Mtext entities are Left-Justified.

Regards HofCAD CSI.

Code S2W.lsp at the start of this topic updated.
« Last Edit: March 10, 2011, 03:47:17 PM by HofCAD »

fervour

  • Guest
Re: Sentence to Words
« Reply #10 on: January 06, 2015, 05:14:39 PM »
I have a sentence in mtext and all the words are separated with TAB spacer. What is the modification to make so that it only breaks the tabs between words and not regular spaces.

Thank you.