Detailed explanation of the principle of Blocking foul language in various Python chat systems, and the python Chat System

Source: Internet
Author: User

Detailed explanation of the principle of Blocking foul language in various Python chat systems, and the python Chat System

I suddenly thought that the bullet screen in a video was found interesting by a harmonious full screen *, and then I wanted to try to write and read it in python. The result was really amazing, first of all, you have to have a warehouse that stores dirty words so that you can check them at that time. Therefore, you still like to use lists because lists are easy to use and expand flexibly, with the foul language library, we are thinking about how to shield the foul language at the core. You should remember what type of bullet screen data the player entered is first, followed by the character string. If there is no special requirement, there are always these data structures. It's much easier to replace character strings with character strings, right? Today we're talking about a complete structure, in order to make everyone better understand, 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 function to replace the dirty Code as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-import datetimetime = datetime. datetime. now () speak = 'your dog's day, fuckR, your mom, fuck your fairy Board, an old batch of 'dirty = ['fuck', 'dog's day ', 'taobao ', 'batch put', 'sin Ren banban ', 'r your mapp', 'fuck you', 'grass you'] for I in dirty: speak = speak. replace (I, '*') print speak + "|" + str (time)

Is it easy to block out the dirty words, which is imported into the time module datetime to get the current local time, and will be used to write and save logs later, of course, the current time is also displayed in many game conversations, and then the speak is the foul language to be output by the player (now the demo will change to the real Input interaction after the Input ), dirty is the definition of the dirty Library (of course, not too much because each region is too different and cannot be written), followed by a for loop to search, as long as you say it contains the dirty library, then replace it with replace, and print the subtitles displayed on the screen... of course, we also need to think about how to track who said what at what time, so that association will use classes, and we will certainly wonder why classes are used, the first class is a summary of things (such as tables with square tables, round tables, conference tables, and desks). For example, a group of players Player has their own game name, then all of them have the chat function (also called Action), so that they can be classified and achieve the effect of differentiation. 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', ' put') print "Public screen display: % s -- % s" % (self. name, self. string) def log (self): print "log record: % s -- % s" % (self. name, self. string) t1 = Player ('white') t1.talk ()

Here we fully introduce the usage of the class and the effect of the workflow. Here we create a Player class first, and init initializes its name. Among them, it has the chat function talk, the logging function is also available, so the chat operation process. After each player registers, a game name will be logged on to the game (you can understand it as after logging on to QQ). Here, self. name = name is equal to your personal name (network name), and then enter the talk, self. string the chat information you entered, self. log record logs. The key point is to first record your original words to the chat log so that you can check your records, self. string. replace to replace your swearing words, then only the replaced words will be displayed on the screen. For example, the printed language is fuck programmed with... isn't this workflow very clear? Of course you don't know about class methods. It will be very confusing here. It will be easy to learn this idea later. Of course it won't end like this. Finally, the basic complete code is put out as follows:

Import datetimetime = str (datetime. datetime. now () [:-7] dirty = ['fuck', 'dogs', 'taobao', 'maobao', 'xiangren board ', 'r your mom ', 'Fuck you', 'grass you'] class Player (object): def _ init _ (self, name): self. name = name def talk (self): self. string = raw_input ("input-write:") # self. string = 'your dog's day, R your Mother's Day, fuck your fairy Board, an old pub 'self. log () for I in dirty: self. string = self. string. replace (I, ' put') 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 "Nima is teasing me? "

The test results are as follows:

You can create a new file named zanghua.txt with empty text to demonstrate the log storage behavior. The figure above shows that change Player has made a simple switch to demonstrate different user input, in fact, after you log on to the system, you seldom switch users or the like, just chatting with the current account. The code here adds the File Processing Method with open (file name, open mode) for short, "f" and "write" (remember to add the \ n line break at the end of the write, otherwise it is hard to recognize the first line of full write) about datetime [: 7] The Slice ignores the remainder of the time after the printing above, which is more concise. Basically, there can be a lot of extensions, such as how many times the number of swear words exceeds, limiting the number of minutes to speak, and regularly clearing logs. This depends on how much code is needed.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.