基于正则表达式模式的词干生成不起作用(Python)

基于正则表达式模式的词干生成不起作用(Python),python,regex,Python,Regex,我想写一个函数,用正则表达式替换字符串。然而,它并没有做必要的事情。不知道怎么了 我正在Windows10上使用Python 3.4.3 这是nltk代码本中的代码 import re replacement_patterns = [ (r'won\'t', 'will not'), (r'can\'t', 'cannot'), (r'i\'m', 'i am'), (r'ain\'t', 'is not'), (r'(\w+)\'ll', '\g<

我想写一个函数,用正则表达式替换字符串。然而,它并没有做必要的事情。不知道怎么了

我正在Windows10上使用Python 3.4.3

这是nltk代码本中的代码

import re

replacement_patterns = [
    (r'won\'t', 'will not'),
    (r'can\'t', 'cannot'),
    (r'i\'m', 'i am'),
    (r'ain\'t', 'is not'),
    (r'(\w+)\'ll', '\g<1> will'),
    (r'(\w+)n\'t', '\g<1> not'),
    (r'(\w+)\'ve', '\g<1> have'),
    (r'(\w+)\'s', '\g<1> is'),
    (r'(\w+)\'re', '\g<1> are'),
    (r'(\w+)\'d', '\g<1> would')
]

class RegexpReplacer(object):
    def __init__(self, patterns=replacement_patterns):
        self.patterns = [(re.compile(regex), repl) for (regex, repl) in patterns]
        print("init")
        print(self.patterns)

    def replace(self, text):
        print("In replace")
        s = text
        print(self.patterns)
        for (pattern, repl) in self.patterns:
            s = re.sub(pattern, repl, s)
            print(s)
            return s


if __name__ == "__main__":
    print("RegEx replacers")
    replacer = RegexpReplacer()
    result = replacer.replace("can't is a contraction")
    print(result)
    result = replacer.replace("I should've done that thing I didn't do")
    print(result)
重新导入
替换模式=[
(r‘不会’、‘不会’),
(r'can't','cannot'),
(r'i'm'、'i'm'),
(r‘不是’、‘不是’),
(r'(\w+)'ll','g will'),
(r'(\w+)n't','\g not'),
(r'(\w+)'ve','g have'),
(r'(\w+)'s',“\g is'),
(r'(\w+)'re','g are'),
(r'(\w+)'d','\g'd')
]
类RegexpReplacer(对象):
定义初始(自,模式=替换模式):
self.patterns=[(re.compile(regex),repl)for(regex,repl)in patterns]
打印(“初始化”)
打印(自我模式)
def更换(自我,文本):
打印(“替换中”)
s=文本
打印(自我模式)
对于self.patterns中的(pattern,repl):
s=re.sub(模式、回复、s)
印刷品
返回s
如果名称=“\uuuuu main\uuuuuuuu”:
打印(“正则表达式替换程序”)
replacer=RegexpReplacer()
结果=replace.replace(“不能是收缩”)
打印(结果)
result=replace.replace(“我应该做我没有做的事”)
打印(结果)

您的
替换功能中存在
缩进问题

class RegexpReplacer(object):

    def replace(self, text):
        print("In replace")
        s = text
        print(self.patterns)
        for (pattern, repl) in self.patterns:
            s = re.sub(pattern, repl, s)
            print(s)
        return s  #here is the problem

关于您的功能的一些建议,请删除
打印
行,使其更干净、更简单

class RegexpReplacer(object):

    def replace(self, text):
        for (pattern, repl) in self.patterns:
            text = re.sub(pattern, repl, text)
        return s

您的
replace
函数中存在
缩进问题

class RegexpReplacer(object):

    def replace(self, text):
        print("In replace")
        s = text
        print(self.patterns)
        for (pattern, repl) in self.patterns:
            s = re.sub(pattern, repl, s)
            print(s)
        return s  #here is the problem

关于您的功能的一些建议,请删除
打印
行,使其更干净、更简单

class RegexpReplacer(object):

    def replace(self, text):
        for (pattern, repl) in self.patterns:
            text = re.sub(pattern, repl, text)
        return s

您的
replace
函数中存在
缩进问题

class RegexpReplacer(object):

    def replace(self, text):
        print("In replace")
        s = text
        print(self.patterns)
        for (pattern, repl) in self.patterns:
            s = re.sub(pattern, repl, s)
            print(s)
        return s  #here is the problem

关于您的功能的一些建议,请删除
打印
行,使其更干净、更简单

class RegexpReplacer(object):

    def replace(self, text):
        for (pattern, repl) in self.patterns:
            text = re.sub(pattern, repl, text)
        return s

您的
replace
函数中存在
缩进问题

class RegexpReplacer(object):

    def replace(self, text):
        print("In replace")
        s = text
        print(self.patterns)
        for (pattern, repl) in self.patterns:
            s = re.sub(pattern, repl, s)
            print(s)
        return s  #here is the problem

关于您的功能的一些建议,请删除
打印
行,使其更干净、更简单

class RegexpReplacer(object):

    def replace(self, text):
        for (pattern, repl) in self.patterns:
            text = re.sub(pattern, repl, text)
        return s

除了公认的答案之外,您的代码还有一个问题:在原始字符串中使用excape序列。比如说

r'won\'t'
r'\\'
是一个原始字符串(r前缀),它不会扩展转义序列,因此您的字符串实际上是

won\'t
改为使用混合引号:

r"won't"
这个错误现在不会咬你,因为
\'
没有特殊意义,所以它被转换为
'
,但它会在其他时间出现,例如

r'won\'t'
r'\\'

是一个长度为2的字符串。

除了可接受的答案之外,您的代码还有一个问题:在原始字符串中使用excape序列。比如说

r'won\'t'
r'\\'
是一个原始字符串(r前缀),它不会扩展转义序列,因此您的字符串实际上是

won\'t
改为使用混合引号:

r"won't"
这个错误现在不会咬你,因为
\'
没有特殊意义,所以它被转换为
'
,但它会在其他时间出现,例如

r'won\'t'
r'\\'

是一个长度为2的字符串。

除了可接受的答案之外,您的代码还有一个问题:在原始字符串中使用excape序列。比如说

r'won\'t'
r'\\'
是一个原始字符串(r前缀),它不会扩展转义序列,因此您的字符串实际上是

won\'t
改为使用混合引号:

r"won't"
这个错误现在不会咬你,因为
\'
没有特殊意义,所以它被转换为
'
,但它会在其他时间出现,例如

r'won\'t'
r'\\'

是一个长度为2的字符串。

除了可接受的答案之外,您的代码还有一个问题:在原始字符串中使用excape序列。比如说

r'won\'t'
r'\\'
是一个原始字符串(r前缀),它不会扩展转义序列,因此您的字符串实际上是

won\'t
改为使用混合引号:

r"won't"
这个错误现在不会咬你,因为
\'
没有特殊意义,所以它被转换为
'
,但它会在其他时间出现,例如

r'won\'t'
r'\\'

是一个长度为2的字符串。

你好,罗洛,缩进是个问题。我把指纹放进去只是为了调试。错过了缩进问题。谢谢邦森你好罗洛,缩进是个问题。我把指纹放进去只是为了调试。错过了缩进问题。谢谢邦森你好罗洛,缩进是个问题。我把指纹放进去只是为了调试。错过了缩进问题。谢谢邦森你好罗洛,缩进是个问题。我把指纹放进去只是为了调试。错过了缩进问题。谢谢谢谢你,托马斯。。我已经在代码中做了更改。还有一个问题-我需要(r“.”?还是我应该删除它并使用(“不会”、“不会”)。不,你不需要它(r字符串本身与正则表达式无关),但是更复杂的正则表达式往往有很多例外:
x\dx
(x-一些数字-x)。在这种情况下,使用原始字符串更容易,而且不必担心
\d
是在字符串级别还是在正则表达式级别上被解释。谢谢托马斯。我已经在代码中做了更改。另外一个问题-我需要(r.)?还是删除它并使用(“不会”、“不会”)。不,你不需要它(r字符串本身与正则表达式没有任何关系),但是更复杂的正则表达式往往会有很多例外:
x\dx
(x-一些数字-x)。在这种情况下,使用原始字符串更容易,而且不必担心
\d
是在字符串级别还是在正则表达式级别上被解释。谢谢托马斯。我已经在代码中做了更改。另外一个问题-我需要(r.)?还是删除它并使用(“不会”、“不会”)。不,你不需要它(r字符串本身与正则表达式没有任何关系),但是更复杂的正则表达式往往会有很多例外:
x\dx
(x-一些数字-x)。在这种情况下,使用原始字符串更容易,而且不必