import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

import re
import nltk
import spacy

from transformers import pipeline
from nltk.corpus import stopwords
from wordcloud import WordCloud
nltk.download('stopwords')
nlp=spacy.load("en_core_web_sm")
medical_texts = [
    "Patient has a history of hypertension and diabetes. Prescribed medication X.",
    "Asthma diagnosis confirmed. Patient advised to use inhaler daily.",
    "Hypertension patient. Needs regular monitoring of blood pressure.",
    "Diabetes patient. Recommended diet and exercise.",
    "Patient diagnosed with hypertension. Medication Y prescribed."
]
df=pd.DataFrame({'MedicalText':medical_texts})
df.head()
def preprocess(text):
    text=text.lower()
    text=re.sub(r'\d+','',text)
    text=re.sub(r'\W+',' ',text)
    text=' '.join([word for word in text.split() if word not in stopwords.words('english')])
    return text
df['CleanedText']=df['MedicalText'].apply(preprocess)
df.head()
def extract(text):
    doc=nlp(text)
    return [(ent.text,ent.label_) for ent in doc.ents]
df['Entities']=df['MedicalText'].apply(extract)
df.head()
sentiment=pipeline('sentiment-analysis')
df['Sentiment']=df['MedicalText'].apply(lambda x:sentiment(x)[0])
df.head()
all_text=' '.join(df['CleanedText'])
wordcloud=WordCloud(width=800,height=400,background_color='white').generate(all_text)
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
def extract_insights(row):
    entities=row['Entities']
    sentiment=row['Sentiment']
    return{
        'Entities':entities,
        'Sentiment':sentiment['label'],
        'SentimentScore':sentiment['score']
    }
df['Insights']=df.apply(extract_insights,axis=1)
df[['MedicalText','Insights']].head()
