如何使用python3检查脏话

问题描述 投票:0回答:3

请帮我用代码纠正这个问题:

import urllib.request
import urllib.parse

def read_text():

    quotes= open("C:\\Users\\no1\\Desktop\\movie_quotes.txt")
    content_of_file=quotes.read()
    quotes.close()
    check_profanity(content_of_file)

def check_profanity(text_to_check):
    a=urllib.parse.urlencode(text_to_check)
    a=a.encode('utf-8')
    connection=urllib.request.urlopen("http://www.wdyl.com/profanity?q=",a)
    output=connection.read()
    print(output)
    connection.close()

read_text()

我想检查脏话,但代码不起作用。

这是来自python的反馈:

Traceback (most recent call last):
  File "C:\Python34\lib\urllib\parse.py", line 760, in urlencode
    raise TypeError
TypeError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\no1\Desktop\check profanity.py", line 19, in <module>
    read_text()
  File "C:\Users\no1\Desktop\check profanity.py", line 9, in read_text
    check_profanity(content_of_file)
  File "C:\Users\no1\Desktop\check profanity.py", line 12, in check_profanity
    a=urllib.parse.urlencode(text_to_check)
  File "C:\Python34\lib\urllib\parse.py", line 768, in urlencode
    "or mapping object").with_traceback(tb)
  File "C:\Python34\lib\urllib\parse.py", line 760, in urlencode
    raise TypeError
TypeError: not a valid non-string sequence or mapping object
python
3个回答
2
投票

www.wdyl.com 不再工作了。

您可以使用您喜欢什么来代替: http://www.wdylike.appspot.com/?q=foo

这是我的工作代码:

import urllib.request
import urllib.parse

def read_text():
    quotes = open("movie_quotes.txt")  # Returns a `file` object
    contents_of_file = quotes.read()
    print(contents_of_file)
    quotes.close()
    check_profanity(contents_of_file)

def check_profanity(text_to_check):
    encoded_text = urllib.parse.quote(text_to_check, 'utf-8')
    address = "http://www.wdylike.appspot.com/?q="+encoded_text
    print(address)
    connection = urllib.request.urlopen(address)
    output = connection.read()
    print(output)
    connection.close()

read_text()

0
投票

这是另一种类似的方法:

from django.utils.encoding import smart_str
import urllib

def check_profanity(to_check):
    try:
        connection = urllib.urlopen('http://www.wdyl.com/profanity?q='\ 
                    + smart_str(to_check))
        output = connection.read()
        connection.close()
        if 'true' in output:  
            return True
        else:
            return False  
    except:
        print "An error occurred!"
        return

0
投票

您可以使用

better_profanity
为此:

import better_profanity

word = input('Enter a word:\n')

def check_profanity(word1):
    word_contains_profanity = bool(profanity.contains_profanity(word))
    print(word_contains_profanity)

示例:

Enter a word:  
Hello  
False
Enter a word:  
F*** you
True

您也可以使用它进行审查。

以我正在编码的 rng 为例:

def detect_profanityf(feedback):
    profanityf = bool(profanity.contains_profanity(feedback))
    if profanityf:
        feedback = '!' * len(feedback)
    return feedback

示例:

What is your feedback?  
Hello

反馈文件中的输出:

Hello

What is your feedback?  
F!!! you

反馈文件中的输出:

!!!! !!!

别忘了

pip install better_profanity

© www.soinside.com 2019 - 2024. All rights reserved.