3

I am analysing text with respect to its argument structure (conclusion supported by reasons, each supported by evidence). During this analysis it would have been very usefull to have each sentence numbered, for easy reference. Hence, I need a function that EITHER inserts an number at the start of each sentence, with the number surrounded by brackets or symbols clearly distinguishing it from the rest of the text, OR insert the sentence number(s) for each text line in the margin of the text.

Any idea of how I could number the sentences in a text?

Thanks for any suggestions or tips!

  • Are the sentences in paragraph form -- with multiple sentences separated by the common English endings? Or, is it one sentence per line? Are there any blank lines in between sentences. Is it every single sentence in the buffer that needs to be numbered, or are there exceptions that should not be numbered (if so, what are the exceptions)? – lawlist Aug 12 '15 at 20:46
  • Are there periods in the middle of sentences (something like e.g. in the middle of a phrase)? – Name Aug 12 '15 at 20:47
  • Hi, thanks for thinking and asking clearly : ) Yes, sentences are separated by common English (or Norwegian) endings, and the sentences are organized in one or several paragraphs. The typical texts could be taken from book, magazines or newspapers. Ideally, I wold like to number only those paragraphs in the buffer that I want to analyze, and use the rest of the buffer to write my own comments to the numbered text. – myotis Aug 12 '15 at 20:53
  • Yes, I see your point. Periods not representing end of a sentence are typical like in " e.g.", "i.e". Maybe I should use two spaces after each period representing end of sentence. – myotis Aug 12 '15 at 20:55
  • myotis: That is indeed the standard solution. See C-h i g (emacs) Sentences for details. – phils Aug 12 '15 at 21:59
3
  1. Know that you can move forward and backward over sentences using M-e and M-a. And there are other sentence operations. See the Emacs manual, node Sentences.

  2. You can write a command that inserts increasing numbers before sentences. Iterate over the text using function backward-sentence, starting at the end of the buffer. For example:

    (defun foo-backward (beg end)
      "Number sentences in buffer or active region, from end, starting with 1."
      (interactive (if (use-region-p)
                       (list (region-beginning) (region-end))
                     (list (point-min) (point-max))))
      (let ((ii  0)
            ins)
        (save-excursion
          (goto-char end)
          (while (> (point) beg)
            (backward-sentence)
            (insert (setq ins  (format "[%d] " (setq ii  (1+ ii)))))
            (search-backward ins nil t)))))

    (defun foo-forward (beg end)
      "Number sentences in buffer or active region, starting with 1."
      (interactive (if (use-region-p)
                       (list (region-beginning) (region-end))
                     (list (point-min) (point-max))))
      (let ((ii  0)
            ins)
        (save-excursion
          (goto-char beg)
          (while (< (point) end)
            (forward-sentence)
            (backward-sentence)
            (insert (setq ins  (format "[%d] " (setq ii  (1+ ii)))))
            (forward-sentence)))))
  1. Not sure whether this will help with what you really need for your sentence analysis, but if you use Icicles then you can use multi-command icicle-sentence (bound to M-s M-s s) to search the text within sentences.

    IOW, search contexts are individual sentences -- other text is ignored. You can easily navigate among sentences, narrow the set of sentences you look at by typing a pattern to match, etc. Use C-down to cycle among the matching sentences (or all sentences, if your minibuffer input is empty).

  • Thanks, but it does not help me in this situation. I want the numbers to be inserted and visible, so that I can give the text to others that I cooperate with (export from org mode to HTML or Latex for instance) – myotis Aug 12 '15 at 21:39
  • Drew: Sorry, I was a little quick here. Could you elaborate your point 2? How can i use backward-sentence to insert numbers? – myotis Aug 12 '15 at 21:41
  • I added a quick example. – Drew Aug 12 '15 at 21:46
  • Thanks Drew, I have tested it. Your code adds numbers to the beginning of each paragraph, not numbers to the start of each sentences. (I am using org mode). – myotis Aug 12 '15 at 21:54
  • Your sentences must match the definition of "sentence" that Emacs is using. Typically that means separating them by two spaces after the period (as you suggested yourself in a different comment, for the same reasoning that Emacs uses). See Drew's link to the documentation. – phils Aug 12 '15 at 21:55

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.