Create intro.py
Browse files
intro.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from textblob import TextBlob
|
| 3 |
+
from collections import Counter
|
| 4 |
+
|
| 5 |
+
import re
|
| 6 |
+
import spacy
|
| 7 |
+
|
| 8 |
+
# ... (Rest of the code for Title, Introduction, and Basic Libraries) ...
|
| 9 |
+
|
| 10 |
+
# Sample Project 2: Word Frequency Analysis
|
| 11 |
+
st.subheader("2. Word Frequency Analysis")
|
| 12 |
+
|
| 13 |
+
st.write("""
|
| 14 |
+
Word frequency analysis helps us understand which words are most commonly used in a given text.
|
| 15 |
+
""")
|
| 16 |
+
|
| 17 |
+
# Input for Word Frequency
|
| 18 |
+
word_input = st.text_area("Enter text for word frequency analysis:", "Natural language processing is a branch of artificial intelligence. NLP is a fascinating field.")
|
| 19 |
+
|
| 20 |
+
def word_frequency(text):
|
| 21 |
+
"""Calculates word frequencies in the given text.
|
| 22 |
+
Args:
|
| 23 |
+
text: The input text.
|
| 24 |
+
Returns:
|
| 25 |
+
A list of tuples, where each tuple contains a word and its frequency.
|
| 26 |
+
"""
|
| 27 |
+
# Preprocess the text:
|
| 28 |
+
# - Convert to lowercase
|
| 29 |
+
# - Remove punctuation
|
| 30 |
+
# - Remove stop words (optional)
|
| 31 |
+
text = text.lower()
|
| 32 |
+
text = re.sub(r"[^\w\s]", "", text)
|
| 33 |
+
# Uncomment the line below if you want to remove stop words
|
| 34 |
+
# from nltk.corpus import stopwords
|
| 35 |
+
# stop_words = set(stopwords.words('english'))
|
| 36 |
+
# words = [word for word in text.split() if word not in stop_words]
|
| 37 |
+
words = text.split()
|
| 38 |
+
|
| 39 |
+
# Count word frequencies
|
| 40 |
+
word_counts = Counter(words)
|
| 41 |
+
return word_counts.most_common(10)
|
| 42 |
+
|
| 43 |
+
# Display the word frequency result
|
| 44 |
+
if word_input:
|
| 45 |
+
word_counts = word_frequency(word_input)
|
| 46 |
+
st.write("Top 10 most frequent words:")
|
| 47 |
+
for word, count in word_counts:
|
| 48 |
+
st.write(f"- {word}: {count}")
|
| 49 |
+
|
| 50 |
+
# Sample Project 3: Named Entity Recognition (NER) with spaCy
|
| 51 |
+
st.subheader("3. Named Entity Recognition (NER) with spaCy")
|
| 52 |
+
|
| 53 |
+
st.write("""
|
| 54 |
+
Named Entity Recognition (NER) identifies and classifies named entities like people, organizations, locations, dates, etc.
|
| 55 |
+
""")
|
| 56 |
+
|
| 57 |
+
# Load spaCy model
|
| 58 |
+
nlp = spacy.load("en_core_web_sm")
|
| 59 |
+
|
| 60 |
+
# Input for NER
|
| 61 |
+
ner_input = st.text_area("Enter text for Named Entity Recognition:", "Apple Inc. is looking to buy a startup in San Francisco.")
|
| 62 |
+
|
| 63 |
+
def extract_entities(text):
|
| 64 |
+
"""Extracts named entities from the given text using spaCy.
|
| 65 |
+
Args:
|
| 66 |
+
text: The input text.
|
| 67 |
+
Returns:
|
| 68 |
+
A list of tuples, where each tuple contains the entity text and its label.
|
| 69 |
+
"""
|
| 70 |
+
doc = nlp(text)
|
| 71 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 72 |
+
return entities
|
| 73 |
+
|
| 74 |
+
# Display the NER result
|
| 75 |
+
if ner_input:
|
| 76 |
+
entities = extract_entities(ner_input)
|
| 77 |
+
st.write("Named Entities found:")
|
| 78 |
+
for entity in entities:
|
| 79 |
+
st.write(f"- {entity[0]} ({entity[1]})")
|