File size: 3,305 Bytes
786eb26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# import streamlit as st
# from pymongo import MongoClient
# import pandas as pd

# # Initialize MongoDB client
# client = MongoClient('mongodb+srv://vazeswaroop:[email protected]/')
# db = client['word_classification_db']
# collection = db['word_classification']

# # Function to save word and its category
# def save_word(word, category):
#     collection.insert_one({"word": word, "category": category})

# # Function to load words
# def load_words():
#     return pd.DataFrame(list(collection.find({}, {'_id': 0})))

# # Streamlit UI
# st.title('Word Classification')

# # Input fields for word and category
# word = st.text_input('Enter Word')
# category = st.selectbox('Select Category', ['Theme', 'Subtheme', 'Keywords'])

# if st.button('Save Word'):
#     save_word(word, category)
#     st.success(f'Word "{word}" saved under category "{category}"!')

# if st.button('View All Entries'):
#     df = load_words()
#     st.dataframe(df)

# # Close the MongoDB connection when the app is done
# client.close()
import streamlit as st
from pymongo import MongoClient
import pandas as pd

# Initialize MongoDB client
client = MongoClient('mongodb+srv://vazeswaroop:[email protected]/')
db = client['word_classification_db']
collection = db['word_classification']

# Function to save word and its category
def save_word(word, category):
    collection.insert_one({"word": word, "category": category})

# Function to load words from the DB
def load_words():
    return pd.DataFrame(list(collection.find({}, {'_id': 0})))

# Streamlit UI
st.title('Word Classification and Prompt Matching')

# Input fields for word and category
word = st.text_input('Enter Word')
category = st.selectbox('Select Category', ['Theme', 'Subtheme', 'Keywords'])

if st.button('Save Word'):
    save_word(word, category)
    st.success(f'Word "{word}" saved under category "{category}"!')

if st.button('View All Entries'):
    df = load_words()
    st.dataframe(df)

# Prompt Input for Matching
st.header('Prompt Matching')
prompt = st.text_area('Enter a prompt to check for matches')

if st.button('Check Prompt for Matches'):
    df = load_words()
    
    # Separate the words by categories
    keywords = df[df['category'] == 'Keywords']['word'].tolist()
    themes = df[df['category'] == 'Theme']['word'].tolist()
    subthemes = df[df['category'] == 'Subtheme']['word'].tolist()

    # Function to count matches
    def count_matches(prompt, words_list):
        return [word for word in words_list if word in prompt]

    # Get the matches
    matched_keywords = count_matches(prompt, keywords)
    matched_themes = count_matches(prompt, themes)
    matched_subthemes = count_matches(prompt, subthemes)

    # Display the count and matched words
    st.write(f"Number of Keywords matched: {len(matched_keywords)}")
    st.write(f"Matched Keywords: {matched_keywords}")
    st.write(f"Number of Themes matched: {len(matched_themes)}")
    st.write(f"Matched Themes: {matched_themes}")
    st.write(f"Number of Subthemes matched: {len(matched_subthemes)}")
    st.write(f"Matched Subthemes: {matched_subthemes}")

# Close the MongoDB connection when the app is done
client.close()