all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Shortening words with multiple rules
@ 2022-08-16  0:49 uzibalqa via Users list for the GNU Emacs text editor
  2022-08-16  8:55 ` Arash Esbati
  0 siblings, 1 reply; 13+ messages in thread
From: uzibalqa via Users list for the GNU Emacs text editor @ 2022-08-16  0:49 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org

The following function shortens words according to specific rules. The specific
rule for the function is to replace a word beginning with the character sequence
`cog', `col', `com', `con', `cor', `coun', or `cum', and replacing the match by
the letter `k'​.

How can I have a function that is able to perform a number of rules rather than
just a single one.

(defun shorten-word ()
"Shortens a word according to specific rules."

(interactive)

(let* ( (bounds (bounds-of-thing-at-point 'word))
(word (buffer-substring (car bounds) (cdr bounds)))
(point (point))
(impl "[RX]") )

(goto-char (car bounds)) (delete-char (length word))

(cond

(equal translate-regexps "[RX]")
(insert (replace-regexp-in-string
(rx (seq word-start
(or (seq "co" (any "glmnr")) "coun" "cum")))
"k" word))
(t

(insert (replace-regexp-in-string "\\<\\(co[glmnr]\\|coun\\|cum\\)" "k" word))))

(goto-char point)))

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-16  0:49 Shortening words with multiple rules uzibalqa via Users list for the GNU Emacs text editor
@ 2022-08-16  8:55 ` Arash Esbati
  2022-08-16 10:08   ` uzibalqa
  0 siblings, 1 reply; 13+ messages in thread
From: Arash Esbati @ 2022-08-16  8:55 UTC (permalink / raw)
  To: uzibalqa via Users list for the GNU Emacs text editor; +Cc: uzibalqa

uzibalqa via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> writes:

> The following function shortens words according to specific rules. The specific
> rule for the function is to replace a word beginning with the character sequence
> `cog', `col', `com', `con', `cor', `coun', or `cum', and replacing the match by
> the letter `k'​.
>
> How can I have a function that is able to perform a number of rules rather than
> just a single one.

Does this template help?

(defun shorten-word ()
  "Shortens a word according to specific rules."
  (interactive)
  (let* ((bounds (bounds-of-thing-at-point 'word))
         (s (car bounds))
         (case-fold-search nil)
         (p (point-marker)))
    (when s
      (goto-char s)
      (cond ((looking-at (regexp-opt '("cog" "col" "com" "con"
                                       "cor" "cum" "coun")
                                     "\\<\\("))
             (replace-match "k"))
            ;; Other rules
            (t nil))
      (goto-char p))
    (set-marker p nil)))

Best, Arash



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-16  8:55 ` Arash Esbati
@ 2022-08-16 10:08   ` uzibalqa
  2022-08-16 10:45     ` Arash Esbati
  0 siblings, 1 reply; 13+ messages in thread
From: uzibalqa @ 2022-08-16 10:08 UTC (permalink / raw)
  To: Arash Esbati; +Cc: uzibalqa via Users list for the GNU Emacs text editor

------- Original Message -------
On Tuesday, August 16th, 2022 at 8:55 AM, Arash Esbati <arash@gnu.org> wrote:


> uzibalqa via Users list for the GNU Emacs text editor help-gnu-emacs@gnu.org writes:
>
> > The following function shortens words according to specific rules. The specific
> > rule for the function is to replace a word beginning with the character sequence
> > `cog',` col', `com',` con', `cor',` coun', or `cum', and replacing the match by the letter` k'.
> >
> > How can I have a function that is able to perform a number of rules rather than
> > just a single one.
>
>
> Does this template help?

It does help a lot.  How would a match at the end of word (matching "ley" "ily" "ly") look like, with your scheme?

> (defun shorten-word ()
> "Shortens a word according to specific rules."
> (interactive)
> (let* ((bounds (bounds-of-thing-at-point 'word))
> (s (car bounds))
> (case-fold-search nil)
> (p (point-marker)))
> (when s
> (goto-char s)
> (cond ((looking-at (regexp-opt '("cog" "col" "com" "con"
> "cor" "cum" "coun")
> "\\<\\("))
> (replace-match "k"))
> ;; Other rules
> (t nil))
> (goto-char p))
> (set-marker p nil)))
>
> Best, Arash



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-16 10:08   ` uzibalqa
@ 2022-08-16 10:45     ` Arash Esbati
  2022-08-16 19:06       ` uzibalqa
  0 siblings, 1 reply; 13+ messages in thread
From: Arash Esbati @ 2022-08-16 10:45 UTC (permalink / raw)
  To: uzibalqa; +Cc: uzibalqa via Users list for the GNU Emacs text editor

uzibalqa <uzibalqa@proton.me> writes:

> It does help a lot.  How would a match at the end of word (matching
> "ley" "ily" "ly") look like, with your scheme?

Maybe something like this:

(defun shorten-word ()
  "Shortens a word according to specific rules."
  (interactive)
  (let* ((bounds (bounds-of-thing-at-point 'word))
         (s (car bounds))
         (case-fold-search nil)
         (p (point-marker)))
    (when s
      (goto-char s)
      (cond ((looking-at (regexp-opt '("cog" "col" "com" "con"
                                       "cor" "cum" "coun")
                                     "\\<\\("))
             (replace-match "k"))
            ((looking-at (concat "[[:alpha:]]*?"
                                 "\\("
                                 (regexp-opt '("ley" "ily" "ly"))
                                 "\\)\\>"))
             (replace-match "X" nil nil nil 1))
            (t nil))
      (goto-char p))
    (set-marker p nil)))

Best, Arash



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-16 10:45     ` Arash Esbati
@ 2022-08-16 19:06       ` uzibalqa
  2022-08-17  0:09         ` uzibalqa
  0 siblings, 1 reply; 13+ messages in thread
From: uzibalqa @ 2022-08-16 19:06 UTC (permalink / raw)
  To: Arash Esbati; +Cc: uzibalqa via Users list for the GNU Emacs text editor


------- Original Message -------
On Tuesday, August 16th, 2022 at 10:45 AM, Arash Esbati <arash@gnu.org> wrote:


> uzibalqa uzibalqa@proton.me writes:
>
> > It does help a lot. How would a match at the end of word (matching
> > "ley" "ily" "ly") look like, with your scheme?
>
>
> Maybe something like this:
>
> (defun shorten-word ()
> "Shortens a word according to specific rules."
> (interactive)
> (let* ((bounds (bounds-of-thing-at-point 'word))
> (s (car bounds))
> (case-fold-search nil)
> (p (point-marker)))
> (when s
> (goto-char s)
> (cond ((looking-at (regexp-opt '("cog" "col" "com" "con"
> "cor" "cum" "coun")
> "\\<\\("))
> (replace-match "k"))
> ((looking-at (concat "[[:alpha:]]*?"
> "\\("
> (regexp-opt '("ley" "ily" "ly"))
> "\\)\\>"))
>
> (replace-match "X" nil nil nil 1))
> (t nil))
> (goto-char p))
> (set-marker p nil)))
>
> Best, Arash

Works very well Arash.

Another task would be to match character sequences in the middle of a word.

I have been using the following regex to match "ple" inside a word and replacing
it with the letter "p", allowing punctuation.

(replace-regexp-in-string
   "\[[:alpha:]]+[-]?\\)ple\\([[[:alpha:]]+[[:punct:]]?\\)" "\\1p\\2" word)

How would this translate to your function?




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-16 19:06       ` uzibalqa
@ 2022-08-17  0:09         ` uzibalqa
  2022-08-17  4:18           ` uzibalqa
  0 siblings, 1 reply; 13+ messages in thread
From: uzibalqa @ 2022-08-17  0:09 UTC (permalink / raw)
  To: uzibalqa
  Cc: Arash Esbati,
	uzibalqa via Users list for the GNU Emacs text editor

------- Original Message -------
On Tuesday, August 16th, 2022 at 7:06 PM, uzibalqa <uzibalqa@proton.me> wrote:


> ------- Original Message -------
> On Tuesday, August 16th, 2022 at 10:45 AM, Arash Esbati arash@gnu.org wrote:
>
>
>
> > uzibalqa uzibalqa@proton.me writes:
> >
> > > It does help a lot. How would a match at the end of word (matching
> > > "ley" "ily" "ly") look like, with your scheme?
> >
> > Maybe something like this:
> >
> > (defun shorten-word ()
> > "Shortens a word according to specific rules."
> > (interactive)
> > (let* ((bounds (bounds-of-thing-at-point 'word))
> > (s (car bounds))
> > (case-fold-search nil)
> > (p (point-marker)))
> > (when s
> > (goto-char s)
> > (cond ((looking-at (regexp-opt '("cog" "col" "com" "con"
> > "cor" "cum" "coun")
> > "\\<\\("))
> > (replace-match "k"))
> > ((looking-at (concat "[[:alpha:]]*?"
> > "\\("
> > (regexp-opt '("ley" "ily" "ly"))
> > "\\)\\>"))
> >
> > (replace-match "X" nil nil nil 1))
> > (t nil))
> > (goto-char p))
> > (set-marker p nil)))
> >
> > Best, Arash

Would like to allow hyphen in words and punctuation

Have started with

 ((looking-at (concat "[[:alpha:]]+[-]?"
           "\\(" (regexp-opt '("ley" "ily" "ly")) "\\)\\>"))
        (replace-match "l" nil nil nil 1))


But this replaced "family" with just the letter "l".



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-17  0:09         ` uzibalqa
@ 2022-08-17  4:18           ` uzibalqa
  2022-08-17  6:16             ` Arash Esbati
  0 siblings, 1 reply; 13+ messages in thread
From: uzibalqa @ 2022-08-17  4:18 UTC (permalink / raw)
  To: uzibalqa
  Cc: Arash Esbati,
	uzibalqa via Users list for the GNU Emacs text editor


Arash, I have come up with a variation of what you wrote

(defun shorten-word-b ()
  "Shortens a word according to specific rules."

  (interactive)

  (let* ((bounds (bounds-of-thing-at-point 'word))
	 (s (car bounds))
	 (case-fold-search nil)
	 (p (point-marker)))

    (when s
      (goto-char s)
      (cond

       ;;-----------------------------------------------
       ;; Insert `k' for words with initial
       ;; `cog', `col', `com', `con', `cor', `coun', `cum'.
       ((looking-at (concat  "\\<"
	  (regexp-opt '("cog" "col" "com" "con" "cor" "cum" "coun"))))

	 (replace-match "k"))

       ;;-----------------------------------------------
       ;; Insert `l' for words with final
       ;; `ley', `ily', and `ly'.
       ((search-forward-regexp (concat
           (regexp-opt '("ley" "ily" "ly")) "\\>"))

	(replace-match "l"))

       (t nil))
      (goto-char p))
    (set-marker p nil)))

What is left is to do is the ability to insert the letter `p' for medial `ple' in a word.
Medially means that the character sequence appears inside the word with at least one character
on each side op the character sequence `ple`.  An example would be the word complementary, where
"ple" occurs from character 4 to character 7.






^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-17  4:18           ` uzibalqa
@ 2022-08-17  6:16             ` Arash Esbati
  2022-08-17  9:16               ` uzibalqa
  0 siblings, 1 reply; 13+ messages in thread
From: Arash Esbati @ 2022-08-17  6:16 UTC (permalink / raw)
  To: uzibalqa; +Cc: uzibalqa via Users list for the GNU Emacs text editor

uzibalqa <uzibalqa@proton.me> writes:

> What is left is to do is the ability to insert the letter `p' for
> medial `ple' in a word.

Maybe something like this:

(defun shorten-word-b ()
  "Shortens a word according to specific rules."
  (interactive)
  (let* ((bounds (bounds-of-thing-at-point 'word))
         (s (car bounds))
         (case-fold-search nil)
         (e (make-marker))
         (p (point-marker)))
    (when s
      (set-marker e (cdr bounds))
      (goto-char s)
      (cond
       ;;-----------------------------------------------
       ;; Insert `k' for words with initial
       ;; `cog', `col', `com', `con', `cor', `coun', `cum'.
       ((looking-at (concat  "\\<"
                             (regexp-opt '("cog" "col" "com" "con" "cor" "cum" "coun"))))

        (replace-match "k"))

       ;;-----------------------------------------------
       ;; Insert `l' for words with final
       ;; `ley', `ily', and `ly'.
       ((save-excursion
          (re-search-forward (concat
                              (regexp-opt '("ley" "ily" "ly")) "\\>")
                             e t))
        (replace-match "l"))

       ((save-excursion
          (re-search-forward "ple" e t))
        (replace-match "l"))

       (t nil))
      (goto-char p))
    (set-marker e nil)
    (set-marker p nil)))

Best, Arash



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-17  6:16             ` Arash Esbati
@ 2022-08-17  9:16               ` uzibalqa
  2022-08-17 10:07                 ` Arash Esbati
  0 siblings, 1 reply; 13+ messages in thread
From: uzibalqa @ 2022-08-17  9:16 UTC (permalink / raw)
  To: Arash Esbati; +Cc: uzibalqa via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

------- Original Message -------
On Wednesday, August 17th, 2022 at 6:16 AM, Arash Esbati <arash@gnu.org> wrote:


> uzibalqa uzibalqa@proton.me writes:
>
> > What is left is to do is the ability to insert the letter `p' for medial` ple' in a word.
>
>
> Maybe something like this:
>
> (defun shorten-word-b ()
> "Shortens a word according to specific rules."
> (interactive)
> (let* ((bounds (bounds-of-thing-at-point 'word))
> (s (car bounds))
> (case-fold-search nil)
> (e (make-marker))
> (p (point-marker)))
> (when s
> (set-marker e (cdr bounds))
> (goto-char s)
> (cond
> ;;-----------------------------------------------
> ;; Insert `k' for words with initial ;;` cog', `col',` com', `con',` cor', `coun',` cum'.
> ((looking-at (concat "\\<"
> (regexp-opt '("cog" "col" "com" "con" "cor" "cum" "coun"))))
>
> (replace-match "k"))
>
> ;;-----------------------------------------------
> ;; Insert `l' for words with final ;;` ley', `ily', and` ly'.
> ((save-excursion
> (re-search-forward (concat
> (regexp-opt '("ley" "ily" "ly")) "\\>")
>
> e t))
> (replace-match "l"))
>
> ((save-excursion
> (re-search-forward "ple" e t))
> (replace-match "l"))
>
> (t nil))
> (goto-char p))
> (set-marker e nil)
> (set-marker p nil)))
>
> Best, Arash

Trying the code upon the word "please" results in "pase" because you have
not checked for mid-word in the final search.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-17  9:16               ` uzibalqa
@ 2022-08-17 10:07                 ` Arash Esbati
  2022-08-17 11:00                   ` uzibalqa
  0 siblings, 1 reply; 13+ messages in thread
From: Arash Esbati @ 2022-08-17 10:07 UTC (permalink / raw)
  To: uzibalqa; +Cc: uzibalqa via Users list for the GNU Emacs text editor

uzibalqa <uzibalqa@proton.me> writes:

> Trying the code upon the word "please" results in "pase" because you
> have not checked for mid-word in the final search.

For me, it turns "please" into "lase".  Maybe I didn't understand this
correctly, but do you want to apply all the rules to a word?  In that
case, you have adjust the code to do so like this:

(defun shorten-word-b ()
  "Shortens a word according to specific rules."
  (interactive)
  (let* ((bounds (bounds-of-thing-at-point 'word))
         (s (car bounds))
         (case-fold-search nil)
         (e (make-marker))
         (p (point-marker)))
    (when s
      (set-marker e (cdr bounds))
      (goto-char s)
      
      ;;-----------------------------------------------
      ;; Insert `k' for words with initial
      ;; `cog', `col', `com', `con', `cor', `coun', `cum'.
      (when (looking-at
             (concat  "\\<"
                      (regexp-opt '("cog" "col" "com" "con" "cor" "cum" 
                                    "coun"))))

        (replace-match "k")
        (goto-char s))
      

      ;;-----------------------------------------------
      ;; Insert `l' for words with final
      ;; `ley', `ily', and `ly'.
      (when (save-excursion
              (re-search-forward (concat
                                  (regexp-opt '("ley" "ily" "ly")) "\\>")
                                 e t))
        (replace-match "l")
        (goto-char s))

      ;; Make sure we don't match anything at the beginning of the
      ;; string by going one char forward:
      (when (save-excursion
              (forward-char)
              (re-search-forward "ple" e t))
        (replace-match "l")
        (goto-char s))
      
      (goto-char p))
    (set-marker e nil)
    (set-marker p nil)))

This turns "completely" to "kltel".  If you want only one rule to apply,
adjust the last version with `cond' to your needs.

Best, Arash



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-17 10:07                 ` Arash Esbati
@ 2022-08-17 11:00                   ` uzibalqa
  2022-08-17 12:15                     ` Arash Esbati
  0 siblings, 1 reply; 13+ messages in thread
From: uzibalqa @ 2022-08-17 11:00 UTC (permalink / raw)
  To: Arash Esbati; +Cc: uzibalqa via Users list for the GNU Emacs text editor

------- Original Message -------
On Wednesday, August 17th, 2022 at 10:07 AM, Arash Esbati <arash@gnu.org> wrote:


> uzibalqa uzibalqa@proton.me writes:
>
> > Trying the code upon the word "please" results in "pase" because you
> > have not checked for mid-word in the final search.
>
>
> For me, it turns "please" into "lase". Maybe I didn't understand this
> correctly, but do you want to apply all the rules to a word? In that
> case, you have adjust the code to do so like this:
>
> (defun shorten-word-b ()
> "Shortens a word according to specific rules."
> (interactive)
> (let* ((bounds (bounds-of-thing-at-point 'word))
> (s (car bounds))
> (case-fold-search nil)
> (e (make-marker))
> (p (point-marker)))
> (when s
> (set-marker e (cdr bounds))
> (goto-char s)
>
> ;;-----------------------------------------------
> ;; Insert `k' for words with initial ;;` cog', `col',` com', `con',` cor', `coun',` cum'.
> (when (looking-at
> (concat "\\<"
> (regexp-opt '("cog" "col" "com" "con" "cor" "cum"
> "coun"))))
>
> (replace-match "k")
> (goto-char s))
>
>
> ;;-----------------------------------------------
> ;; Insert `l' for words with final ;;` ley', `ily', and` ly'.
> (when (save-excursion
> (re-search-forward (concat
> (regexp-opt '("ley" "ily" "ly")) "\\>")
>
> e t))
> (replace-match "l")
> (goto-char s))
>
> ;; Make sure we don't match anything at the beginning of the
> ;; string by going one char forward:
> (when (save-excursion
> (forward-char)
> (re-search-forward "ple" e t))
> (replace-match "l")
> (goto-char s))
>
> (goto-char p))
> (set-marker e nil)
> (set-marker p nil)))
>
> This turns "completely" to "kltel". If you want only one rule to apply,
> adjust the last version with `cond' to your needs.
>
> Best, Arash

Works by going one char forward.  Would you have a solution that uses `search-forward-regexp' ?
I think one can have all three possibilities function with `search-forward-regexp'.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-17 11:00                   ` uzibalqa
@ 2022-08-17 12:15                     ` Arash Esbati
  2022-08-17 21:29                       ` uzibalqa
  0 siblings, 1 reply; 13+ messages in thread
From: Arash Esbati @ 2022-08-17 12:15 UTC (permalink / raw)
  To: uzibalqa; +Cc: uzibalqa via Users list for the GNU Emacs text editor

uzibalqa <uzibalqa@proton.me> writes:

> Would you have a solution that uses `search-forward-regexp' ?  I think
> one can have all three possibilities function with
> `search-forward-regexp'.

This one is left as an exercise for the reader :-)

Best, Arash



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Shortening words with multiple rules
  2022-08-17 12:15                     ` Arash Esbati
@ 2022-08-17 21:29                       ` uzibalqa
  0 siblings, 0 replies; 13+ messages in thread
From: uzibalqa @ 2022-08-17 21:29 UTC (permalink / raw)
  To: Arash Esbati; +Cc: uzibalqa via Users list for the GNU Emacs text editor

------- Original Message -------
On Wednesday, August 17th, 2022 at 12:15 PM, Arash Esbati <arash@gnu.org> wrote:


> uzibalqa uzibalqa@proton.me writes:
>
> > Would you have a solution that uses `search-forward-regexp' ? I think one can have all three possibilities function with` search-forward-regexp'.
>
>
> This one is left as an exercise for the reader :-)
>
> Best, Arash

Have realised it is an alias.




^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-08-17 21:29 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-16  0:49 Shortening words with multiple rules uzibalqa via Users list for the GNU Emacs text editor
2022-08-16  8:55 ` Arash Esbati
2022-08-16 10:08   ` uzibalqa
2022-08-16 10:45     ` Arash Esbati
2022-08-16 19:06       ` uzibalqa
2022-08-17  0:09         ` uzibalqa
2022-08-17  4:18           ` uzibalqa
2022-08-17  6:16             ` Arash Esbati
2022-08-17  9:16               ` uzibalqa
2022-08-17 10:07                 ` Arash Esbati
2022-08-17 11:00                   ` uzibalqa
2022-08-17 12:15                     ` Arash Esbati
2022-08-17 21:29                       ` uzibalqa

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.