Views
No views yet
from transformers import pipeline
pipe = pipeline("text-classification", model="omykhailiv/bert-fake-news-recognition")
pipe.predict('Some text')import re
import string
import spacy
from nltk.corpus import stopwords
lem = spacy.load('en_core_web_sm')
def testing_data_prep(text):
"""
Args:
text (str): The input text string.
Returns:
str: The preprocessed text string, or an empty string if the length
does not meet the specified criteria (6 to 20 words).
"""
# Convert text to lowercase for case-insensitive processing
text = str(text).lower()
# Remove HTML tags and their contents (e.g., "<tag>text</tag>")
text = re.sub('<.*?>+\w+<.*?>', '', text)
# Remove punctuation using regular expressions and string escaping
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
# Remove words containing alphanumeric characters followed by digits
# (e.g., "model2023", "data10")
text = re.sub('\w*\d\w*', '', text)
# Remove newline characters
text = re.sub('\n', '', text)
# Replace multiple whitespace characters with a single space
text = re.sub('\\s+', ' ', text)
# Lemmatize words (convert them to their base form)
text = lem(text)
words = [word.lemma_ for word in text]
# Removing stopwords, such as do, not, as, etc. (https://gist.github.com/sebleier/554280)
new_filtered_words = [
word for word in words if word not in stopwords.words('english')]
if 20 >= len(new_filtered_words) >= 6:
return ' '.join(new_filtered_words)
return ' ' precision recall f1-score support
0 0.93 0.94 0.94 3782
1 0.95 0.94 0.95 4335
accuracy 0.94 8117
macro avg 0.94 0.94 0.94 8117
weighted avg 0.94 0.94 0.94 8117 precision recall f1-score support
0 0.93 0.88 0.90 2297
1 0.89 0.93 0.91 2297
accuracy 0.91 4594
macro avg 0.91 0.91 0.91 4594
weighted avg 0.91 0.91 0.91 4594
precision recall f1-score support
0 0.9736 0.9750 0.9743 10455
1 0.9726 0.9711 0.9718 9541
accuracy 0.9731 19996
macro avg 0.9731 0.9731 0.9731 19996
weighted avg 0.9731 0.9731 0.9731 19996 precision recall f1-score support
0 0.87 0.80 0.84 492
1 0.82 0.89 0.85 508
accuracy 0.85 1000
macro avg 0.85 0.85 0.85 1000
weighted avg 0.85 0.85 0.85 1000