File size: 8,485 Bytes
6237b12
6165314
 
 
 
 
 
 
 
 
 
dd2cd7c
2e6a5a3
82c141f
 
 
 
 
 
 
 
 
 
 
 
 
9c2faf5
 
 
 
 
 
 
 
 
 
f756937
6165314
 
 
 
 
 
 
9c2faf5
6165314
 
9c2faf5
6165314
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c2faf5
6165314
 
 
 
 
 
 
 
9c2faf5
6165314
 
 
 
 
 
 
 
 
 
 
 
9c2faf5
 
6165314
 
 
 
 
 
9c2faf5
6165314
 
 
 
9c2faf5
 
 
6165314
 
 
9c2faf5
 
 
6165314
 
9c2faf5
6165314
9c2faf5
6165314
 
9c2faf5
6165314
 
 
 
 
 
 
 
 
 
9c2faf5
6165314
 
 
 
9c2faf5
 
6165314
 
9c2faf5
6165314
 
9c2faf5
6165314
9c2faf5
6165314
9c2faf5
 
6165314
 
 
9c2faf5
 
6165314
9c2faf5
6165314
9c2faf5
 
6165314
9c2faf5
 
6165314
 
9c2faf5
 
 
 
6165314
9c2faf5
 
6165314
9c2faf5
6165314
 
 
9c2faf5
6165314
9c2faf5
6165314
9c2faf5
 
6165314
9c2faf5
 
6165314
9c2faf5
 
 
6165314
9c2faf5
6165314
 
 
9c2faf5
6165314
 
9c2faf5
 
 
 
 
 
6237b12
82c141f
 
 
 
6237b12
9c2faf5
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import streamlit as st
import pandas as pd
import pickle
from tqdm import tqdm
from Levenshtein import distance as lev
import joblib
from googletrans import Translator
from indictrans import Transliterator
from pyphonetics import RefinedSoundex
from bs4 import BeautifulSoup
import re
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Load sentiment analysis model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("Seethal/sentiment_analysis_generic_dataset")
model = AutoModelForSequenceClassification.from_pretrained("Seethal/sentiment_analysis_generic_dataset")

# Define a function to get the sentiment from the model
def get_sentiment(text):
    inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True)
    outputs = model(**inputs)
    sentiment = torch.argmax(outputs.logits, dim=1).item()
    return 'Positive' if sentiment == 1 else 'Negative'


def closest_match(word, vocabulary):
    best_match = None
    best_distance = float('inf')
    for vocab_word in vocabulary:
        dist = lev(word, vocab_word)
        if dist < best_distance:
            best_distance = dist
            best_match = vocab_word
    return best_match

def main():
    st.title('Text Processing App')
    rs = RefinedSoundex()
    normalized_string_final=[]
    translator = Translator()
    trn = Transliterator(source='eng', target='hin')

    with open(r'./english_vocab.pkl', "rb") as fp:
        english = pickle.load(fp)
    english_vocab=english 
    with open(r'./hinglish_vocab.pkl', "rb") as fp:
        hinglish = pickle.load(fp)
    hinglish_vocab=hinglish 

    english_vocab['and'] = ['and']
    english_vocab['is'] = ['is']

    def clean_tweet(tweet):
        text=re.sub(r'@ [A-Za-z0-9\']+','',tweet)
        text=BeautifulSoup(text,'lxml').get_text()
        text=re.sub(r'https (//)[A-Za-z0-9. ]*(/) [A-Za-z0-9]+','',text)
        text=re.sub(r'https[A-Za-z0-9/. ]*','',text)
        text=re.sub("[^a-zA-Z]"," ",text)
        text=re.sub(r'\bRT\b',' ',text)
        text=re.sub(r'\bnan\b',' ',text)
        return text

    input_text = st.text_area("Enter the text:")
    total_translated = []
    if st.button('Process'):
        data = {'Text': [input_text]}
        df1 = pd.DataFrame(data)
        df1['Text'] = df1['Text'].apply(clean_tweet)
        cleaned_text = df1['Text'].tolist()[0]
        total_text = [cleaned_text]
        st.write("Input Text:", total_text)

        for i in tqdm(total_text):
            test_text=i.split()
            not_changed_idx=[]
            for i in range(len(test_text)):
                not_changed_idx.append(0)
            changed_text=[]
            changed_idx=[]

            for i in range(len(test_text)):
                for key in english_vocab:
                    done=0
                    for val in  english_vocab[key]:
                        if(test_text[i]==val):
                            changed_text.append(key)
                            changed_idx.append(i)
                            not_changed_idx[i]=1
                            done=1
                            break
                    if done==1:
                        break


            normalized_string=[]
            res = dict(zip(changed_idx, changed_text))
            for i in range(len(test_text)):
                try:
                    normalized_string.append(res[i])
                except:
                    normalized_string.append(test_text[i])
            print("English Normalized String:", normalized_string)

            # hinglish word change
            test_list = [i for i in range(len(test_text))]
            changed_hing_idx = [i for i in test_list if i not in changed_idx]
            hinglish_text_part = [test_text[i] for i in changed_hing_idx]
            changed_text2 = []
            changed_idx2 = []

            for i in range(len(hinglish_text_part)):
                for key in hinglish_vocab:
                    done = 0
                    for val in hinglish_vocab[key]:
                        if hinglish_text_part[i] == val:
                            changed_text2.append(key)
                            changed_idx2.append(i)
                            done = 1
                            break
                    if done == 1:
                        break

            normalized_string2 = []
            res2 = dict(zip(changed_idx2, changed_text2))
            for i in range(len(hinglish_text_part)):
                try:
                    normalized_string2.append(res2[i])
                except:
                    normalized_string2.append(hinglish_text_part[i])

            for i in changed_idx:
                normalized_string2.append(res[i])

            print("Hinglish Normalized String:", normalized_string)

            # finding phoneme and leventise distance for unchanged word
            for i in range(len(not_changed_idx)):
                try:
                    if not_changed_idx[i] == 0:
                        eng_phoneme_correction = []
                        for j in english_vocab:
                            try:
                                phoneme = rs.distance(normalized_string2[i], j)
                            except:
                                pass
                            if phoneme <= 1:
                                eng_phoneme_correction.append(j)
                        eng_lev_correction = []
                        for k in eng_phoneme_correction:
                            dist = lev(normalized_string2[i], k)
                            if dist <= 2:
                                eng_lev_correction.append(k)

                        eng_lev_correction.extend(hing_lev_correction)
                        new_correction = eng_lev_correction
                        eng_lev_correction = []
                        for l in new_correction:
                            dist = lev(normalized_string2[i], l)
                            eng_lev_correction.append(dist)
                        min_val = min(eng_lev_correction)
                        min_idx = eng_lev_correction.index(min_val)

                        suggestion = closest_match(new_correction[min_idx], english_vocab.keys())
                        normalized_string2[i] = suggestion
                except:
                    pass

            normalized_string_final = normalized_string2
            print("Phoneme levenshtein Distionary suggestion Normalized String:", normalized_string_final)

            # sentence tagging
            classifier = joblib.load(r"./classifer.joblib")
            classify = []
            for i in normalized_string:
                test_classify = classifier(i)
                classify.append(test_classify[0].get("label"))

            for i in range(len(classify)):
                if classify[i] == 'en':
                    try:
                        normalized_string[i] = translator.translate(normalized_string[i], src='en', dest='hi').text
                    except:
                        normalized_string[i] = "delete"
            print("English -> Hindi Translated String:", normalized_string)

            conversion_list = [trn.transform(i) for i in normalized_string]
            print("Hinglish -> Hindi Transliterated String:", conversion_list)

            sentence = [" ".join(conversion_list)]
            translated = []
            for i in sentence:
                try:
                    translated_text = translator.translate(i, src='hi', dest='en')
                    translated.append(translated_text.text)
                except:
                    translated.append("delete")
            print("Hindi -> English Translated String:", translated)
            total_translated.append(translated[0])

            st.write("English Normalized String:", normalized_string)
            st.write("Hinglish Normalized String:", normalized_string)
            st.write("Phoneme Levenshtein Dictionary Suggestion Normalized String:", normalized_string_final)
            st.write("English -> Hindi Translated String:", normalized_string)
            st.write("Hinglish -> Hindi Transliterated String:", conversion_list)
            st.write("Hindi -> English Translated String:", translated)

            # Get the sentiment of the translated text
            sentiment = get_sentiment(translated[0])
            st.write("Sentiment of Translated Text:", sentiment)

if __name__ == '__main__':
    main()