lisp从另一个列表中删除一个列表的内容

lisp从另一个列表中删除一个列表的内容,lisp,common-lisp,Lisp,Common Lisp,我有一个类似这样的字符串列表,称为F: (“你好,我是瓦利德”“再见,夫人”)=>此列表包含两个字符串元素 我还有另一个类似这样的列表调用(“单词”“夫人”)=>这包含两个单词 现在我想从列表F的每个字符串中删除列表S的元素,输出应该是这样的(“hello I'am walid”“再见”) 我已经找到了这个函数: 例如: (删除字符串“walid”“hello i'am walid”)=>输出“hello i'am” 但有一个问题 例如: (删除字符串“wa”“hello i'am walid

我有一个类似这样的字符串列表,称为F:

(“你好,我是瓦利德”“再见,夫人”)
=>此列表包含两个字符串元素

我还有另一个类似这样的列表调用
(“单词”“夫人”)
=>这包含两个单词

现在我想从列表F的每个字符串中删除列表S的元素,输出应该是这样的
(“hello I'am walid”“再见”)

我已经找到了这个函数:
例如:
(删除字符串“walid”“hello i'am walid”)
=>输出
“hello i'am”

但有一个问题

例如:
(删除字符串“wa”“hello i'am walid”)
=>输出
“hello i'am lid”

但是输出应该是这样的“hello i'am walid”,换句话说,我不会从字符串中删除确切的单词

请帮助我并感谢的

提供此功能:

(defun replace-all (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part 
is replaced with replacement."
    (with-output-to-string (out)
      (loop with part-length = (length part)
            for old-pos = 0 then (+ pos part-length)
            for pos = (search part string
                              :start2 old-pos
                              :test test)
            do (write-string string out
                             :start old-pos
                             :end (or pos (length string)))
            when pos do (write-string replacement out)
            while pos))) 
(loop for raw-string in '("hello word i'am walid" "goodbye madame")
        collect (reduce (lambda (source-string bad-word)
                          (replace-all source-string bad-word ""))
                        '("word" "madame")
                     :initial-value raw-string))
使用该功能:

(defun replace-all (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part 
is replaced with replacement."
    (with-output-to-string (out)
      (loop with part-length = (length part)
            for old-pos = 0 then (+ pos part-length)
            for pos = (search part string
                              :start2 old-pos
                              :test test)
            do (write-string string out
                             :start old-pos
                             :end (or pos (length string)))
            when pos do (write-string replacement out)
            while pos))) 
(loop for raw-string in '("hello word i'am walid" "goodbye madame")
        collect (reduce (lambda (source-string bad-word)
                          (replace-all source-string bad-word ""))
                        '("word" "madame")
                     :initial-value raw-string))
提供以下功能:

(defun replace-all (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part 
is replaced with replacement."
    (with-output-to-string (out)
      (loop with part-length = (length part)
            for old-pos = 0 then (+ pos part-length)
            for pos = (search part string
                              :start2 old-pos
                              :test test)
            do (write-string string out
                             :start old-pos
                             :end (or pos (length string)))
            when pos do (write-string replacement out)
            while pos))) 
(loop for raw-string in '("hello word i'am walid" "goodbye madame")
        collect (reduce (lambda (source-string bad-word)
                          (replace-all source-string bad-word ""))
                        '("word" "madame")
                     :initial-value raw-string))
使用该功能:

(defun replace-all (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part 
is replaced with replacement."
    (with-output-to-string (out)
      (loop with part-length = (length part)
            for old-pos = 0 then (+ pos part-length)
            for pos = (search part string
                              :start2 old-pos
                              :test test)
            do (write-string string out
                             :start old-pos
                             :end (or pos (length string)))
            when pos do (write-string replacement out)
            while pos))) 
(loop for raw-string in '("hello word i'am walid" "goodbye madame")
        collect (reduce (lambda (source-string bad-word)
                          (replace-all source-string bad-word ""))
                        '("word" "madame")
                     :initial-value raw-string))
您可以使用正则表达式库。它的正则表达式风格理解单词边界
\b

替代品的工作原理如下:

(cl-ppcre:regex-replace-all "\\bwa\\b" "ba wa walid" "")

=> "ba  walid"
我猜您希望将删除的单词周围的所有空格合并为一个:

(cl-ppcre:regex-replace-all "\\s*\\bwa\\b\\s*" "ba wa walid" " ")

=> "ba walid"
请参阅上面链接的文档

更新:您将问题扩展到标点符号。这实际上有点复杂,因为现在有三种字符:字母数字、标点符号和空格

这里我不能给出一个完整的解决方案,但我设想的大纲是在这三种类型之间创建边界定义。为此,你需要正面/负面的表情。然后查看被替换的字符串,无论它是以标点符号开头还是以标点符号结尾,并将相应的边界附加或前置到有效表达式

为了以可读的方式定义边界,cl-ppcre的解析树语法可能很有用。

您可以使用正则表达式库。它的正则表达式风格理解单词边界
\b

替代品的工作原理如下:

(cl-ppcre:regex-replace-all "\\bwa\\b" "ba wa walid" "")

=> "ba  walid"
我猜您希望将删除的单词周围的所有空格合并为一个:

(cl-ppcre:regex-replace-all "\\s*\\bwa\\b\\s*" "ba wa walid" " ")

=> "ba walid"
请参阅上面链接的文档

更新:您将问题扩展到标点符号。这实际上有点复杂,因为现在有三种字符:字母数字、标点符号和空格

这里我不能给出一个完整的解决方案,但我设想的大纲是在这三种类型之间创建边界定义。为此,你需要正面/负面的表情。然后查看被替换的字符串,无论它是以标点符号开头还是以标点符号结尾,并将相应的边界附加或前置到有效表达式


为了以可读的方式定义边界,cl ppcre的解析树语法可能会很有用。

好的,我现在同意你的看法,我有一个类似这样的停止词列表(“am”“或”“没有”!”“?”)和一个注释列表(“am walid”“我现在没有!”)我不会删除第二个列表中每个评论中的所有停止词,因为它工作得很好i'am example cl ppcre:regex存在一个问题,替换所有“\\s*\\bam\\b\\s*”“i'am wa walid”“”)=>“am wa walid”它不应该这样做that@WALIDBELRHALMIA:我从中得到了“I'wa walid”。顺便说一下,单词边界不会像你现在想要的那样得到标点。不,“我是”不是一个完整的单词。这是一个错误。它可以是(如果它是英语的话)“我是”或缩写“我是”。好的,我同意你的观点。我还有最后一个问题是删除Pontuation假设我有“你好,我是瓦利德,我很高兴”,我不会删除。而且,因为当我这样做时(cl-ppcre:regex替换所有“\\s*\\b\.\\b\\s*”hello word.i'm walid,i feel happy”“”),它不会删除。最后谢谢你好的,我现在和你在一起了,我有一个这样的停止词列表(“am”“或”“没有”!”?)和一个评论列表(“am walid”“我现在没有!”)我不会删除第二个列表中每个评论中的所有停止词,因为它工作得很好i'am example cl ppcre:regex存在一个问题,替换所有“\\s*\\bam\\b\\s*”“i'am wa walid”“”)=>“am wa walid”它不应该这样做that@WALIDBELRHALMIA:我从中得到了“I'wa walid”。顺便说一下,单词边界不会像你现在想要的那样得到标点。不,“我是”不是一个完整的单词。这是一个错误。它可以是(如果它是英语的话)“我是”或缩写“我是”。好的,我同意你的观点。我还有最后一个问题是删除Pontuation假设我有“你好,我是瓦利德,我很高兴”,我不会删除。而且,因为当我这样做时(cl-ppcre:regex替换所有“\\s*\\b\.\\b\\s*”hello word.i'm walid,i feel happy”“”),它不会删除。最后谢谢你