2016-12-21 1 views
0

Ich habe eine WortlisteElisp: Wie eine Wortliste suchen und die Ergebnisse in einen anderen Puffer kopieren?

dempron {hic, haec, hoc, huius, huic, hunc, hanc, hac, hi, hae, horum, harum, his, hos, has} 

Ich habe eine xml-Art-Text

<p>Hoc templum magnum est.</p> 
<p>Templa Romanorum magna sunt.</p> 
<p>Claudia haec templa in foro videt.</p> 

Ich möchte die Wortliste „dempron“ suchen, und die Sätze kopieren, die Wörter aus der Wortliste haben Ein Puffer namens Ergebnisse.

+0

Alle eigenen Versuche zu diesem Problem? Stackoverflow ist für bestimmte Programmierprobleme und nicht für eine Seite, wo Leute Code kostenlos schreiben ... –

+0

Ich hatte nicht die Absicht, Stackoverflow zu missbrauchen. Ich hoffe, dass andere von charliegreen's Code profitieren werden. –

Antwort

0

Ich stimme Simon Fromme zu, aber hoffentlich wird dir das helfen. Lass es mich wissen, wenn du Fragen hast!

(defconst dempron 
    '("hic" "haec" "hoc" "huius" "huic" "hunc" "hanc" "hac" "hi" "hae" "horum" 
    "harum" "his" "hos" "has")) 

(defun dempron-search() 
    "A function to naively search for sentences in XML <p> tags 
containing words from `dempron'. Run this in the buffer you want 
to search, and it will search from POINT onwards, writing results 
to a buffer called 'results'." 
    (interactive) 
    (beginning-of-line) 

    (while (not (eobp)) ;; while we're not at the end of the buffer 
    (let ((cur-line ;; get the current line as a string 
      (buffer-substring-no-properties 
      (progn (beginning-of-line) (point)) 
      (progn (end-of-line) (point))))) 

     ;; See if our current line is in a <p> tag (and run `string-match' so we 
     ;; can extract the sentence with `match-string') 
     (if (string-match "^<p>\\(.*\\)</p>$" cur-line) 
     (progn 
     ;; then extract the actual sentence with `match-string' 
     (setq cur-line (match-string 1 cur-line)) 

     ;; For each word in our sentence... (split on whitespace and 
     ;; anything the sentence is likely to end with) 
     (dolist (word (split-string cur-line "[[:space:].?!\"]+")) 
      ;; `downcase' to make our search case-insensitive 
      (if (member (downcase word) dempron) 
       ;; We have a match! Temporarily switch to the 
       ;; results buffer and write the sentence 
       (with-current-buffer (get-buffer-create "results") 
       (insert cur-line "\n"))))))) 
    (forward-line 1))) ;; Move to the next line 
+0

Vielen Dank für Ihren Code-Schatz. Es funktioniert super und ich fange definitiv an. –

Verwandte Themen