2016-12-21 18 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

非常感谢您的宝贵代码。它工作很好 ,我绝对开始。 –

相关问题