我想編寫一個函數來使用正則表達式替換字符串。但是,它並沒有做必要的事情。不知道什麼是錯的。正則表達式基於模式的詞幹行不通(Python)
我使用Python 3.4.3在Windows 10
這是從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)
你好爍爍的字符串,縮進是問題。我只是爲了調試目的而放置印刷品。錯過縮進問題。謝謝.. !!,Bonson – Bonson