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!

Drew
  • 75,699
  • 9
  • 109
  • 225
myotis
  • 1,099
  • 1
  • 13
  • 27
  • 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

1 Answers1

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).

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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
  • Drew, your code is very interesting if it can exhcange paragraph numbering with sentence numbering (sentence in the sense used in natural language :). But it would be nice if it did not nubmer the whole buffer but only the region I chose to mark. Is that possible ? Thans in advance ! – myotis Aug 12 '15 at 22:00
  • @myotis perhaps you may consider to `(setq sentence-end-double-space nil)` but this causes confusion with the things like `i.e.` or `P.S.A.`. – Name Aug 12 '15 at 22:24
  • Name: why should I set it to `nil` ? – myotis Aug 12 '15 at 22:31
  • @myotis because if you set `(setq sentence-end-double-space nil)`, Emacs considers a period followed by a single space as the end of a *sentence*. As I understand the sentences in your text don't end with period followed by double space. If your sentences end with a period followed by a double space, you get what you want with the solution of Drew. – Name Aug 12 '15 at 22:36
  • Drew: thanks for your code, I can use it as is if I manually insert double space after each sentence ending (to avoid numbers inseted after abbreviations). And thanks to you Name for helping me understand the `sentence-end-double-space` variable – myotis Aug 12 '15 at 22:44
  • @myotis The code given by Drew can do what you want on regions, just a small modification: `(defun foo2 () "Number sentences" (interactive) (let ((ii 0) ins) (save-excursion (goto-char (region-end)) (while (> (point) (region-beginning)) (backward-sentence) (insert (setq ins (format "[%d] " (setq ii (1+ ii))))) (search-backward ins nil t)))))` – Name Aug 12 '15 at 23:01
  • Sorry, I was out for a while. I've updated the example, so you can use it with an active region or whole buffer. And yes, you need to read the Emacs manual, node Sentences, as I said in item #1. It tells you how Emacs recognizes a sentence -- in particular, it describes `sentence-end-double-space`. – Drew Aug 13 '15 at 04:19
  • Drew & Name :Thanks so much to both of you! Your code works perfect, except the backward way of the numbering. It had been more practical for me if it had numbered the senteces in a forward manner. Is it possible to run another function first, one that only counts the number of sentences n, and stores this as N=n+1. And then your code start the backward numbering by inserting the numbers : N-1, N-2 and so on? – myotis Aug 13 '15 at 10:59
  • Sure. Or you can start at the beginning and use `forward-sentence` followed by `backward-sentence`, insert, followed by `forward-sentence`, etc. Something like the second example. – Drew Aug 13 '15 at 13:27
  • Drew: Thanks again! Now it works perfect : ) hihaaaa! – myotis Aug 13 '15 at 15:29