Autocomplete and company completes the word . But is there any package which helps predict the next word expected in the sentence.

  • Good question. Maybe clarify whether you mean (1) editing at some position in an existing sentence (e.g. replacing a word or otherwise having Emacs look at the text that follows the cursor to figure out what word you might want to insert) or (2) inserting a word based only on the text that comes before the cursor. – Drew Dec 17 '18 at 18:30
  • I tried to predict the next words in this question, but failed. – phils Dec 17 '18 at 21:01
  • 1
    Most of the time you are writing the same sentences again and again. Based on the context of what you are writing, the artificial intelligence should predict what the person’s next word would be. As a doctor, I keep writing about patient’s symptoms and signs. In a day I had to repeat myself many times. E.g. If I write lower, it is many times followed by abdominal pain. The software should be able to predict abdominal pain. – Vaibhav Dec 18 '18 at 3:50

You can use company-ngram for this.

showing off company-ngram

Do mind that you'll need to (setq-local company-auto-complete t) and (setq-local company-auto-complete-chars '(? )) to trigger completion after (inserting) a space, not only in the middle of a word and specially after a whitespace.

NOTE: the image is from the repo and is actually licensed under GPLv3.

EDIT: sample code to get it working with markdown-mode

(with-eval-after-load 'company-ngram                                                                                       
  ;; ~/data/ngram/*.txt are used as data                                                                                   
  (setq company-ngram-data-dir "~/data/ngram")                                                                             
  ;; company-ngram supports python 3 or newer                                                                              
  (setq company-ngram-python "pypy3")                                                                                      
  ;; or use `M-x turn-on-company-ngram' and                                                                                
  ;; `M-x turn-off-company-ngram' on individual buffers                                                                    
  ;; save the cache of candidates                                                                                          
  (run-with-idle-timer 7200 t                                                                                              
                       (lambda ()                                                                                          
                         (company-ngram-command "save_cache")                                                              

(require 'company-ngram nil t)                                                                                             

;; to make it work with markdown-mode                                                                                      
(require 'markdown-mode)                                                                                                   
(add-hook 'markdown-mode-hook 'company-mode)                                                                               
(add-hook 'markdown-mode-hook (lambda ()                                                                                   
                (add-to-list 'company-backends 'company-ngram-backend)                                                     
                (setq-local company-auto-complete t)                                                                       
                (setq-local company-auto-complete-chars '(? )))) 
  • Does it require python installation – Vaibhav Dec 20 '18 at 6:28
  • Yes. See the configuration pointer: (setq company-ngram-python "python3") – Felipe Lema Dec 20 '18 at 12:54
  • I successfully installed. I have 5 mb txt file which I created by copying all my .org files to one text file. I put it in a ~/data/ngram directory. But now emacs shows an ongoing process even after keeping it running for 5 hours ! – Vaibhav Dec 20 '18 at 16:26
  • This process is a listening server. It's supposed to be running for company-ngram to work: github.com/kshramt/company-ngram/blob/… – Felipe Lema Dec 20 '18 at 16:34
  • Thanks for the suggestion. But I couldn’t get any predictions . Is it working for you? – Vaibhav Dec 22 '18 at 2:17

dabbrev solves this and is part of the standard Emacs distribution. Suppose that you have already typed "lower abdominal pain" either in the file you are editing or else in an open buffer. If you now type "lower " and then press "M-/", Emacs will insert "abdominal", if you now add a space and press "M-/" again it will insert "pain". If you find that Emacs inserts a thing you do not want (for example you may have typed "lower the boom" and Emacs finds this first so inserts "the"), you simply press "M-/" again and it replaces the thing you do not want by the next possibility it finds.

It is possible to set up dabbrev so that it looks in files that are not open but at this moment I do not remember how this goes; it is just a question of looking this up in the info file for Emacs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.