Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import facebook
- import time
- import logging
- import nltk
- from nltk.tokenize import word_tokenize
- from nltk.corpus import stopwords
- from nltk.stem import WordNetLemmatizer
- import string
- from sklearn.feature_extraction.text import TfidfVectorizer
- from sklearn.linear_model import LogisticRegression
- from sklearn.pipeline import Pipeline
- # Set up logging
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
- # Set up the Facebook Graph API
- graph = facebook.GraphAPI(access_token='YOUR_ACCESS_TOKEN', version='YOUR_API_VERSION')
- # Example post ID
- post_id = 'POST_ID'
- # Download NLTK resources
- nltk.download('punkt')
- nltk.download('stopwords')
- nltk.download('wordnet')
- # Initialize NLTK components
- lemmatizer = WordNetLemmatizer()
- stop_words = set(stopwords.words('english'))
- # Initialize the TF-IDF vectorizer
- vectorizer = TfidfVectorizer(tokenizer=word_tokenize, stop_words=stop_words)
- # Initialize the logistic regression model
- model = LogisticRegression()
- # Create a pipeline
- pipeline = Pipeline([
- ('vectorizer', vectorizer),
- ('model', model)
- ])
- def fetch_comments(post_id):
- try:
- # Fetch comments
- comments = graph.get_object(id=post_id, fields='comments')['comments']['data']
- return comments
- except facebook.GraphAPIError as e:
- logging.error(f"Error fetching comments: {e}")
- return []
- def preprocess_text(text):
- # Tokenize text
- tokens = word_tokenize(text.lower())
- # Remove punctuation and stop words, and lemmatize tokens
- cleaned_tokens = [lemmatizer.lemmatize(token) for token in tokens if token not in string.punctuation and token not in stop_words]
- return ' '.join(cleaned_tokens)
- def is_spam(comment):
- preprocessed_text = preprocess_text(comment['message'])
- # Use the trained model to predict if the comment is spam
- prediction = pipeline.predict([preprocessed_text])[0]
- return prediction == 1
- def remove_spam(comments):
- for comment in comments:
- if is_spam(comment):
- try:
- graph.delete_object(id=comment['id'])
- logging.info(f"Removed spam comment: {comment['message']}")
- except facebook.GraphAPIError as e:
- logging.error(f"Error removing comment: {e}")
- while True:
- comments = fetch_comments(post_id)
- remove_spam(comments)
- time.sleep(60) # Check every minute (adjust as needed)
Advertisement
Comments
-
- In this code, we use a TfidfVectorizer to convert the comment text into a numerical representation suitable for the model, and then use a logistic regression model to classify the comments as spam or not. You can train the model on a spam dataset using supervised learning techniques to improve its accuracy in identifying spam comments.
Add Comment
Please, Sign In to add comment