Detailed explanation of the principle of blocking swear words in Python's major chat systems

WBOY
Release: 2016-12-05 13:27:17
Original
1731 people have browsed it

Suddenly I thought of a video in which the barrage was harmoniously filled with * signs. I thought it was very interesting, and then I wanted to try writing it in python. It turned out to be somewhat effective. The idea is that first you have to have a curse word. The stored warehouse is ready to be tested later, so I personally still like to use lists, because lists are easy to use and expand flexibly. With the swear words library, we are thinking about how to block swear words in the core. You have to remember what the first barrage data type input by the player is. Next is the string. If there are no special requirements, then it is. These data structures are always the same. It is much easier to replace the * sign with the string, right? What we are talking about today is a complete one. In order to let everyone learn the structure more clearly, I will split the code and then assemble it, so that everyone will have a deeper understanding. First, let’s take a look at the core functions and replace the swear words code as follows:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import datetime
time = datetime.datetime.now()

speak = '你个狗日的,fuckR你妈哟,操你个仙人板板,个老麻批'
dirty = ['fuck','狗日的','犊子','麻批','仙人板板','R你妈','操你','草你']

for i in dirty:
  speak = speak.replace(i,'*')
print speak+" | "+str(time)

Copy after login

Is it simple and easy to block swear words? This imports the time module datetime to get the current local time, which will be used to write and save the log later. Of course, many game dialogues will also display the current time, and then speak It is the curse word that the player wants to output (now it is a demonstration and will be changed to the real input interaction of Input later), dirty is the defined curse word library (of course I didn’t write too many because each region is different and there are too many to finish), followed by a for Loop through the search. As long as what you say contains curse words, we will replace them with replace. The final printing means that all the subtitles displayed on the public screen are *... Of course, we also need to think about how to track it. Who said what and at what time, such a correlation requires the use of classes. There will definitely be doubts about why classes are used. First of all, class is a summary of the description of things of a type (for example, tables include square tables, round tables, conference tables, office desks, etc.) , for example, a group of players Player, they all have their own game names, and all have chat functions (we also call them actions), so that they can be classified and distinguished. The code is as follows:

class Player(object):

  def __init__(self,name):
    self.name = name

  def talk(self):
    self.string = 'whatever fuck no joke'
    self.log()
    self.string = self.string.replace('fuck','雅蠛蝶')
    print "公屏显示:%s--%s"%(self.name,self.string) 

  def log(self):
    print "日志记录为:%s--%s"%(self.name,self.string)

t1 = Player('white')
t1.talk()

Copy after login

Here is a comprehensive introduction to the use of classes and workflow effects. Here we first create a player class like Player, and init initializes his name. Among them, he has the chat function talk and the logging function. Let’s talk about the chat operation process. , each player will have a game name after registering and logging into the game (you can understand it as after logging into QQ), where self.name=name is equal to getting your personal name (screen name), and then opening the chat window to enter talk, self. String the chat information you entered, self.log records the log, the focus is to first record your original words in the chat log so that your records can be found, self.string.replace replaces the swear words you said, then the screen Only the replaced words will be displayed. For example, what was printed was originally fuck programming Ya Zhidie... Is this workflow very clear? Of course, if you don’t understand the class method, it will be very confusing here. Take your time as long as you have This idea becomes very easy after learning the class. Of course it won’t end like this. Finally, the basic complete code is released as follows:

import datetime

time = str(datetime.datetime.now())[:-7]

dirty = ['fuck','狗日的','犊子','麻批','仙人板板','R你妈','操你','草你']

class Player(object):

  def __init__(self,name):
    self.name = name

  def talk(self):
    self.string = raw_input("input-write:")
    # self.string = '你个狗日的,R你妈哟,操你个仙人板板,个老麻批'
    self.log()
    for i in dirty:
      self.string = self.string.replace(i,'雅蠛蝶')
    print "{} {}-speak:{}".format(time,self.name,self.string,)

  def log(self):
    with open('zanghua.txt','a') as f:
      f.write("{} {}-speak:{}\n".format(time,self.name,self.string))


t1 = Player('white')
t2 = Player('black')
t3 = Player('green')
while True:
  n = raw_input('change Player:')
  if n == '1':
    t1.talk()
  elif n == '2':
    t2.talk()
  elif n == '3':
    t3.talk()
  elif n == 'q':
    print 'Bye'
    break
  else:
    print "尼玛在逗我?"

Copy after login

The test results are as follows:

You can create a new file named zanghua.txt with empty text. It is just used to demonstrate the behavioral effect of storing logs. The above print says that change Player is a simple switch to demonstrate different user inputs. In fact, the system will be very easy after you log in. There is less need to switch users, just chat with the current account. The code here adds the file processing method with open (file name, open mode), abbreviated as f or the like, and then writes write (remember to add at the end of writing n newline character, otherwise it will be difficult to read the first line in full) Regarding datetime[:7] slicing, it is more concise to ignore the remainder after the time when printing above. The basic process is like this. Of course, there can be many extensions, such as prohibiting speech after the number of curse words exceeds a certain number, limiting speech to a few minutes, logs can be cleared regularly, etc. It’s up to you to research it yourself. The amount of code is a bit large and I won’t show it here.

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Script Home.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!