2016-12-21 43 views
0

我有一個詞表Elisp:如何搜索單詞列表並將結果複製到另一個緩衝區?

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

我有一個文本XML的一種-的

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

我想搜索的單詞表「dempron」,並複製有從詞表詞的句子稱爲的緩衝區結果爲

+0

您自己對此問題的任何嘗試? Stackoverflow是針對特定的編程問題,而不是一個網站,人們免費寫代碼... –

+0

我沒有濫用Stackoverflow的意圖。我希望 其他人將受益於charliegreen的代碼。 –

回答

0

我同意Simon Fromme,但希望這會讓你開始。如果您有任何問題,請告訴我!

(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

非常感謝您的寶貴代碼。它工作很好 ,我絕對開始。 –

相關問題