|
| 1 | +import speech_recognition as sr |
| 2 | +import requests |
| 3 | +import nltk |
| 4 | +import csv |
| 5 | +from nltk.tokenize import word_tokenize |
| 6 | +from nltk.corpus import stopwords |
| 7 | +from nltk.stem import WordNetLemmatizer |
| 8 | +from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer |
| 9 | +import tkinter as tk |
| 10 | +from tkinter import filedialog |
| 11 | + |
| 12 | +nltk.download('wordnet') |
| 13 | + |
| 14 | +nltk.download('punkt') |
| 15 | +nltk.download('stopwords') |
| 16 | + |
| 17 | +# Initialize the recognizer |
| 18 | +r = sr.Recognizer() |
| 19 | + |
| 20 | +# Ask the user whether to record from the microphone or select a file |
| 21 | +root = tk.Tk() |
| 22 | +root.withdraw() |
| 23 | +mode = input("Enter 'mic' to record from microphone or 'file' to select a file: ") |
| 24 | + |
| 25 | +if mode == 'mic': |
| 26 | + # Record audio from the user |
| 27 | + with sr.Microphone() as source: |
| 28 | + print("Say something!") |
| 29 | + audio = r.listen(source) |
| 30 | + |
| 31 | + # Use Google Speech Recognition to convert audio to text |
| 32 | + try: |
| 33 | + text = r.recognize_google(audio) |
| 34 | + print("You said: " + text) |
| 35 | + |
| 36 | + except sr.UnknownValueError: |
| 37 | + print("Google Speech Recognition could not understand audio") |
| 38 | + except sr.RequestError as e: |
| 39 | + print("Could not request results from Google Speech Recognition service; {0}".format(e)) |
| 40 | + |
| 41 | +elif mode == 'file': |
| 42 | + # Select a file using a dialog box |
| 43 | + file_path = filedialog.askopenfilename() |
| 44 | + print("Selected file:", file_path) |
| 45 | + |
| 46 | + # Convert audio file to audio data |
| 47 | + with sr.AudioFile(file_path) as source: |
| 48 | + audio = r.record(source) |
| 49 | + |
| 50 | + # Use Google Speech Recognition to convert audio to text |
| 51 | + try: |
| 52 | + text = r.recognize_google(audio) |
| 53 | + print("Transcription: " + text) |
| 54 | + |
| 55 | + |
| 56 | + except sr.UnknownValueError: |
| 57 | + print("Google Speech Recognition could not understand audio") |
| 58 | + except sr.RequestError as e: |
| 59 | + print("Could not request results from Google Speech Recognition service; {0}".format(e)) |
| 60 | + |
| 61 | +else: |
| 62 | + print("Invalid mode selected.") |
| 63 | + |
| 64 | + # Preprocess the text |
| 65 | +stop_words = set(stopwords.words('english')) |
| 66 | +lemmatizer = WordNetLemmatizer() |
| 67 | +tokens = word_tokenize(text) |
| 68 | +filtered_tokens = [lemmatizer.lemmatize(w.lower()) for w in tokens if not w.lower() in stop_words] |
| 69 | +filtered_text = ' '.join(filtered_tokens) |
| 70 | + |
| 71 | +# Check for profanity in the text using the API |
| 72 | +response = requests.get("https://www.purgomalum.com/service/json?text=" + filtered_text) |
| 73 | +result = response.json() |
| 74 | + |
| 75 | +if result['result'] == filtered_text: |
| 76 | + print("No profanity detected!") |
| 77 | +else: |
| 78 | + print("Profanity detected!") |
| 79 | + print("Censored text: " + result['result']) |
| 80 | + |
| 81 | +# Analyze sentiment of the text using VADER |
| 82 | +analyzer = SentimentIntensityAnalyzer() |
| 83 | +sentiment_scores = analyzer.polarity_scores(text) |
| 84 | + |
| 85 | +# Print sentiment scores |
| 86 | +print("\nSentiment Scores:") |
| 87 | +for key, value in sentiment_scores.items(): |
| 88 | + print(key, ': ', value) |
| 89 | + |
| 90 | +# Print sentiment label based on compound score |
| 91 | +sentiment_label = '' |
| 92 | +if sentiment_scores['compound'] > 0.5: |
| 93 | + sentiment_label = 'Very Positive' |
| 94 | +elif sentiment_scores['compound'] > 0: |
| 95 | + sentiment_label = 'Positive' |
| 96 | +elif sentiment_scores['compound'] == 0: |
| 97 | + sentiment_label = 'Neutral' |
| 98 | +elif sentiment_scores['compound'] < -0.5: |
| 99 | + sentiment_label = 'Very Negative' |
| 100 | +else: |
| 101 | + sentiment_label = 'Negative' |
| 102 | + |
| 103 | +print("\nSentiment Label: " + sentiment_label) |
| 104 | + |
| 105 | +# Write input and output to CSV file |
| 106 | +with open('profanitycheckeroutput.csv', 'a', newline='') as file: |
| 107 | + writer = csv.writer(file) |
| 108 | + #name the columns |
| 109 | + writer.writerow(['Input Text', 'Filtered Text', 'Positive Score', 'Negative Score', 'Neutral Score', 'Compound Score', 'Sentiment Label']) |
| 110 | + #write the data |
| 111 | + |
| 112 | + |
| 113 | + writer.writerow([text, filtered_text, sentiment_scores['pos'], sentiment_scores['neg'], sentiment_scores['neu'], sentiment_scores['compound'], sentiment_label]) |
| 114 | + |
0 commit comments