Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
# Install NLTK directly within the script
|
| 5 |
+
subprocess.run(["pip", "install", "nltk"])
|
| 6 |
+
|
| 7 |
+
import nltk
|
| 8 |
+
nltk.download('punkt')
|
| 9 |
+
|
| 10 |
+
from nltk import ngrams
|
| 11 |
+
from nltk.tokenize import word_tokenize
|
| 12 |
+
|
| 13 |
+
# Function to generate n-grams from a given text
|
| 14 |
+
def generate_ngrams(text, n):
|
| 15 |
+
tokens = word_tokenize(text)
|
| 16 |
+
n_grams = ngrams(tokens, n)
|
| 17 |
+
return [' '.join(gram) for gram in n_grams]
|
| 18 |
+
|
| 19 |
+
# Streamlit web application
|
| 20 |
+
def main():
|
| 21 |
+
st.title("N-gram Generator")
|
| 22 |
+
|
| 23 |
+
# User input for text passage
|
| 24 |
+
text_input = st.text_area("Enter text passage:")
|
| 25 |
+
|
| 26 |
+
# User input for selecting n-gram type
|
| 27 |
+
n_gram_type = st.selectbox("Select n-gram type:", ["Bigram", "Trigram", "Custom N-gram"])
|
| 28 |
+
|
| 29 |
+
# Set n value based on user selection
|
| 30 |
+
if n_gram_type == "Bigram":
|
| 31 |
+
n_value = 2
|
| 32 |
+
elif n_gram_type == "Trigram":
|
| 33 |
+
n_value = 3
|
| 34 |
+
else:
|
| 35 |
+
n_value = st.number_input("Enter the value of N:", min_value=1, value=2, step=1)
|
| 36 |
+
|
| 37 |
+
# Generate n-grams and display the result
|
| 38 |
+
if st.button("Generate N-grams"):
|
| 39 |
+
if text_input:
|
| 40 |
+
ngrams_result = generate_ngrams(text_input, n_value)
|
| 41 |
+
st.write(f"{n_gram_type}s:")
|
| 42 |
+
for gram in ngrams_result:
|
| 43 |
+
st.write(gram)
|
| 44 |
+
else:
|
| 45 |
+
st.warning("Please enter a text passage.")
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
main()
|