-3

I want to add replacements to matching words to a list called nscrip.

I already have a large number of lists that I use for dabbrev, written in the following form. Perhaps I can use these lists, or else have two lists (one for matching and one for replacement strings) if the latter would work better.

(defconst ptc-tablet-16
  '( ("almost" "lmo")    ("beautiful" "btf")  ("big" "bg")
     ("bird" "b/")       ("brother" "Bro")    ("child" "ci")
     ("children" "cil")  ("dear" "de")        ("feel" "fl") ))

Without a list I am using the following expression

(add-to-list 'nscrip (s-replace-regexp "almost" "lmo" word))
Dilna
  • 1,173
  • 3
  • 10
  • This question is seriously about the kind of list? Or you are trying to ask about something else? Please read https://stackoverflow.com/help/how-to-ask – dalanicolai Aug 21 '22 at 07:58
  • I could have the structure as shown, in the form used by `dabbrev`. Or perhaps using two lists would work better, but I am not sure about what approach would work best. I then need a way to use the chosen list to loop through and add the replacements to another list. – Dilna Aug 21 '22 at 08:03
  • Personally, I would use conses for the 'pairs', because I would not need to type an extra `car` to get the latter word. But you might prefer to type one extra `car` instead of typing multiple dots. Otherwise, I would say that an alist is a perfect candidate here. Please read about `alist-get`, and its docstring to see how to make it match on strings, before you ask a new question here. – dalanicolai Aug 21 '22 at 08:18
  • I have done `(setq mylist '(("pine" . "cones") ("oak" . "acorns") ("maple" . "seeds")))`. Then I could do `(assoc "pine" mylist)`. And in general `(assoc word mylist)`. – Dilna Aug 21 '22 at 08:29
  • Fine, `alist-get` would add the `cdr` for you. On the other hand, `assoc` already matches on strings... – dalanicolai Aug 21 '22 at 08:37
  • The replacement would then be `(cdr (assoc word mylist))`. Correct? – Dilna Aug 21 '22 at 09:05
  • How would I loop through the `alist` when using the command `(add-to-list 'protocuneus-nscrip (s-replace-regexp car-elem (cdr (assoc car-elem mylist)) word))`? – Dilna Aug 21 '22 at 09:13
  • I am thinking that instead of looping I can do `(add-to-list 'nscrip (s-replace-regexp word (cdr (assoc word mylist) word))`. – Dilna Aug 21 '22 at 09:36
  • Think about what you need the loop for? Trying to get an element from the alist? Is this functionality already provided by some other function you already know/are using? Then, indeed, use that. In general, I would suggest that when writing functions, you first write down only comments describing the steps that are required... then fill in the steps to implement the function. If then you have a question, post the comments/steps, with the code you have... – dalanicolai Aug 21 '22 at 11:08
  • That's good advice. Thank you so very much for that. I have some code now. – Dilna Aug 21 '22 at 11:20

1 Answers1

0

I have come up this solution. I am not quite sure how efficient this is.

    (defconst alist-tablet-1
      '( ("all" . "l")     ("as" . "as")      ("can" . "k")
         ("do" . "do")     ("for" . "f")      ("in" . "n")
         ("is" . "s")      ("it" . "t")       ("know" . "no")
         ("like" . "lk")   ("little" . "ll")  ("more" . "mo")
         ("other" . "O")   ("some" . "so")    ("than" . "n")
         ("that" . "ta")   ("there" . "tr")   ("this" . "th")
         ("time" . "ti")   ("to" . "to")      ("we" . "w")
         ("well" . "l")    ("will" . "l")     ("work" . "wk")
         ("you" . "u") ))
    
    (defun shorten-word ()
      "Shorten word at point using alist-tablet-1."
    
      (interactive)
    
      (let* ( (bounds  (bounds-of-thing-at-point 'word))
              (word    (buffer-substring (car bounds) (cdr bounds))) )
    
        (goto-char (car bounds))
    
        (add-to-list 'nscrip
           (s-replace-regexp word (cdr (assoc word alist-tablet-1)) word))))

Dilna
  • 1,173
  • 3
  • 10
  • Do you have the original format lists already? – ocodo Aug 21 '22 at 13:24
  • Original lists were of the form used by abbrev, one shown in the question. – Dilna Aug 21 '22 at 13:27
  • So there's no point in converting those lists by hand, you can use them as is. Just do `(cadr x)` instead of `(cdr x)` that would be: `(cadr (assoc word alist-tablet-1))` – ocodo Aug 21 '22 at 13:29
  • @dalanicolai was of the opinion that alist is a perfect candidate here. You can put another answer, and we can all discuss it. – Dilna Aug 21 '22 at 13:31
  • The lists were not really suitable for `abbrev` because customarily `abbrev` is used to convert an abbreviation to a longer phrase. But I am doing the other way round, changing a word to an abbreviation. Furthermore, abbrev cannot handle punctuation in the strings. – Dilna Aug 21 '22 at 13:35
  • .... use alists. – ocodo Aug 21 '22 at 14:08
  • We all approve of alists then. – Dilna Aug 21 '22 at 14:09
  • You could use just `backward-word`. And although using external libraries is fine, I would prefer to use the standard function `replace-regexp-in-string`. But anyway... looks good! – dalanicolai Aug 21 '22 at 14:29
  • External libraries because I am using `s.el`? Perhaps I do not need to replace anything. All I need is to check if word matches `car` of `alist` element, then put `cdr` in `nscrip` list. – Dilna Aug 21 '22 at 14:42
  • I have actually encountered a problem when word does not exist in `alist-tablet-1`. I get `Debugger entered--Lisp error: (void-function nil) nil(#("owell" 0 5 (fontified t))) replace-regexp-in-string(#("owell" 0 5 (fontified t)) nil #("owell" 0 5 (fontified t))) (add-to-list 'nscrip` – Dilna Aug 21 '22 at 15:18