1. Takayuki KONDO
  2. wordfilter

Commits

Takayuki KONDO  committed 48cc9c8

仕様変更対応その2 censored 文字列を変更可能にしました

  • Participants
  • Parent commits c0b0f68
  • Branches default

Comments (0)

Files changed (2)

File test_wordfilter.py

View file
         msg = "t_wada:昨日Arsenal vs Chelsea 熱かった!"
         self.assertEqual("t_wada:昨日<censored> vs Chelsea 熱かった!", wordfilter.censor(msg), "<censored>で置換されるはず")
 
+    def test_censor_unmatch_changekeyword(self):
+        wordfilter = WordFilter(["Arsenal"], "<xxxxx>")
+        msg = "t_wada:昨日 vs Chelsea 熱かった!"
+        self.assertEqual(msg, wordfilter.censor(msg), "そのままの文字列が返ってくるはず")
+
+    def test_censor_match_changekeyword(self):
+        wordfilter = WordFilter(["Arsenal"], "<xxxxx>")
+        msg = "t_wada:昨日Arsenal vs Chelsea 熱かった!"
+        self.assertEqual("t_wada:昨日<xxxxx> vs Chelsea 熱かった!", wordfilter.censor(msg), "<censored>で置換されるはず")
 
 if __name__ == "__main__":
     unittest.main()

File wordfilter.py

View file
 
 class WordFilter:
     wordlist = []
-    def __init__(self, word):
+    def __init__(self, word, keyword="<censored>"):
         self.wordlist = word
+        self.keyword = keyword
         
     def detect(self, msg):
         for word in self.wordlist:
     
     def censor(self, msg):
         for word in self.wordlist:
-            msg = msg.replace(word, "<censored>")
+            msg = msg.replace(word, self.keyword)
         return msg