File size: 1,361 Bytes
c468147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import streamlit as st
import subprocess

# Install NLTK directly within the script
subprocess.run(["pip", "install", "nltk"])

import nltk
nltk.download('punkt')

from nltk import ngrams
from nltk.tokenize import word_tokenize

# Function to generate n-grams from a given text
def generate_ngrams(text, n):
    tokens = word_tokenize(text)
    n_grams = ngrams(tokens, n)
    return [' '.join(gram) for gram in n_grams]

# Streamlit web application
def main():
    st.title("N-gram Generator")

    # User input for text passage
    text_input = st.text_area("Enter text passage:")

    # User input for selecting n-gram type
    n_gram_type = st.selectbox("Select n-gram type:", ["Bigram", "Trigram", "Custom N-gram"])

    # Set n value based on user selection
    if n_gram_type == "Bigram":
        n_value = 2
    elif n_gram_type == "Trigram":
        n_value = 3
    else:
        n_value = st.number_input("Enter the value of N:", min_value=1, value=2, step=1)

    # Generate n-grams and display the result
    if st.button("Generate N-grams"):
        if text_input:
            ngrams_result = generate_ngrams(text_input, n_value)
            st.write(f"{n_gram_type}s:")
            for gram in ngrams_result:
                st.write(gram)
        else:
            st.warning("Please enter a text passage.")

if __name__ == "__main__":
    main()