from textblob import TextBlob
def analyze_sentiment(text):
blob = TextBlob(text)
sentiment = blob.sentiment.polarity
if sentiment > 0:
return 'positive'
elif sentiment < 0:
return 'negative'
else:
return 'neutral'
# Example Usage:
text = "I am happy to inform you that we have approved your request."
sentiment = analyze_sentiment(text)
print(sentiment) # Output: positive
-------------------------------------------------
import spacy
nlp = spacy.load("en_core_web_sm")
def extract_entities(text):
doc = nlp(text)
entities = []
for ent in doc.ents:
entities.append((ent.text, ent.label_))
return entities
# Example Usage:
text = "Please send the report to John Smith at john.smith@example.com."
entities = extract_entities(text)
print(entities) # Output: [('John Smith', 'PERSON'), ('john.smith@example.com', 'EMAIL')]
from gensim import corpora, models
def perform_topic_modeling(texts, num_topics=5, num_words=5):
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
lda = models.LdaModel(corpus, num_topics=num_topics, id2word=dictionary)
topics = lda.print_topics(num_topics=num_topics, num_words=num_words)
return topics
# Example Usage:
texts = ["We need to finalize the proposal by the end of the week.",
"Can you please provide an update on the project status?",
Do'stlaringiz bilan baham: |