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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab89f96
 
 
 
 
 
 
 
 
 
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import streamlit as st

st.markdown("""
    <style>
    /* Set a soft background color */
    body {
        background-color: #eef2f7;
    }
    /* Style for main title */
    h1 {
        color: black;
        font-family: 'Roboto', sans-serif;
        font-weight: 700;
        text-align: center;
        margin-bottom: 25px;
    }
    /* Style for headers */
    h2 {
        color: black;
        font-family: 'Roboto', sans-serif;
        font-weight: 600;
        margin-top: 30px;
    }
    
    /* Style for subheaders */
     h3 {
        color: red;
        font-family: 'Roboto', sans-serif;
        font-weight: 500;
        margin-top: 20px;
    }
    .custom-subheader {
        color: black;
        font-family: 'Roboto', sans-serif;
        font-weight: 600;
        margin-bottom: 15px;
    }
    /* Paragraph styling */
    p {
        font-family: 'Georgia', serif;
        line-height: 1.8;
        color: white;
        margin-bottom: 20px;
    }
    /* List styling with checkmark bullets */
    .icon-bullet {
        list-style-type: none;
        padding-left: 20px;
    }
    .icon-bullet li {
        font-family: 'Georgia', serif;
        font-size: 1.1em;
        margin-bottom: 10px;
        color: white;
    }
    .icon-bullet li::before {
        content: "β—†";
        padding-right: 10px;
        color: blue;
    }
    /* Sidebar styling */
    .sidebar .sidebar-content {
        background-color: #ffffff;
        border-radius: 10px;
        padding: 15px;
    }
    .sidebar h2 {
        color: #495057;
    }
    .step-box {
        font-size: 18px;
        background-color: #F0F8FF;
        padding: 15px;
        border-radius: 10px;
        box-shadow: 2px 2px 8px #D3D3D3;
        line-height: 1.6;
    }
    .box {
        font-size: 18px;
        background-color: blue;
        padding: 15px;
        border-radius: 10px;
        box-shadow: 2px 2px 8px #D3D3D3;
        line-height: 1.6;
    }
    .title {
        font-size: 26px;
        font-weight: bold;
        color: #E63946;
        text-align: center;
        margin-bottom: 15px;
    }
    .formula {
        font-size: 20px;
        font-weight: bold;
        color: #2A9D8F;
        background-color: #F7F7F7;
        padding: 10px;
        border-radius: 5px;
        text-align: center;
        margin-top: 10px;
    }
    /* Custom button style */
    .streamlit-button {
        background-color: #00FFFF;
        color: white;
        font-weight: bold;
    }
    </style>
    """, unsafe_allow_html=True)

st.header("Vectorization🧭")
st.markdown(
    """
    <div class='info-box'>
        <p>Vectorization is the process of converting text into vector.</p>
        <p>This allows ML models to process text data effectively.</p>
    </div>
    """,
    unsafe_allow_html=True
)

st.markdown("""
    There are advance vectorization techniques.They are :
    <ul class="icon-bullet">
        <li>Word Embedding </li>
            <li>Word2Vec </li>
            <li>Fasttext</li>
    </ul>
""", unsafe_allow_html=True) 

st.sidebar.title("Navigation 🧭")
file_type = st.sidebar.radio(
    "Choose a Vectorization technique :",
    ("Word2Vec", "Fasttext"))

st.header("Word Embedding Technique")
st.markdown('''
- It is a advanced vectorization technique it converts text into vectors in such a way that it preserves semantic meaning
- All the techniques which preserves semantic meaning while converting text into vector is word embedding technique
- There are 2 word embedding techniques:
    - Word2Vec
    - Fasttext
''')

if file_type == "Word2Vec":
    st.title(":red[Word2Vec]")
    st.markdown(
    """
        <h3 style='color: #6A0572;'>πŸ“Œ How Word2Vec Works?</h3>
        <ul>
            <li>After <strong>training</strong>, we obtain the final <span class='highlight'>Word2Vec model</span></li>
            <li>The model stores a <strong>dictionary</strong> with word-vector pairs:</li>
        </ul>
        <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
        { w1: [v1], w2: [v2], w3: [v3] }
        </pre>
    """,
    unsafe_allow_html=True,
    )
    st.markdown(
    """
        <h3 style='color: #6A0572;'>βš™οΈ Training vs. Test Time</h3>
        <ul>
            <li><strong>Training Time</strong>: <span class='highlight'>Corpus + Deep Learning Algorithm</span> β†’ Generates Model</li>
            <li><strong>Test Time</strong>: <span class='highlight'>Word</span> β†’ Looked up in Dictionary β†’ Returns <span class='highlight'>Vector Representation</span></li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'>πŸ” How Does It Preserve Meaning?</h3>
        <ul>
            <li>It learns from the <strong>context</strong> of words in the <span class='highlight'>corpus</span></li>
            <li>When given a word, it checks in the dictionary and retrieves the <strong>semantic vector</strong></li>
            <li>Unlike other models, <span class='highlight'>dimensions are not words</span>, but their meanings</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'>πŸ“š Why is Corpus Important?</h3>
        <ul>
            <li>The <strong>Word2Vec algorithm</strong> is completely dependent on the corpus</li>
            <li>Better corpus β†’ Better word representation</li>
            <li>It <strong>preserves semantic meaning</strong> using neighborhood words (context)</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )
    st.markdown('''
    - Word2Vec is not converting document into vector, it is converting word to vector
    - There are 2 techniques by using which we can convert entire document into vector
    - They are :
        - Average Word2Vec
        - TIF-IDF Word2Vec
    ''')

    st.subheader(":blue[Average Word2Vec]")
    st.markdown(
    """
        <h3 style='color: #6A0572;'>πŸ“Œ Step-by-Step Process</h3>
        <ul>
            <li>Given a document <span class='highlight'>d1</span>: <strong>w1, w2, w3</strong></li>
            <li>Retrieve vector representations <strong>v1, v2, v3</strong> from Word2Vec</li>
            <li>Perform <span class='highlight'>element-wise addition</span> of vectors:
                <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
                v_total = v1 + v2 + v3
                </pre>
            </li>
            <li>Normalize by dividing by the total number of words (element-wise division):
                <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
                v_avg = v_total / len(d1)
                </pre>
            </li>
            <li>Final representation contains the <span class='highlight'>average meaning</span> of all words</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'>⚠️ Problem: Equal Importance to Every Word</h3>
        <ul>
            <li>Word2Vec assigns <span class='highlight'>equal weight</span> to all words</li>
            <li>No emphasis on <strong>important words</strong> that carry significant meaning</li>
            <li>This limits the effectiveness in understanding <span class='highlight'>word importance</span></li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <strong>Word2Vec averages word meanings, but lacks weightage for important words! </strong>
    """,
    unsafe_allow_html=True,
    )

    st.subheader(":blue[TF-IDF Word2Vec]")
    st.markdown(
    """
        <h3 style='color: #6A0572;'>⚠️ Issue with Word2Vec</h3>
        <ul>
            <li>Gives equal importance to every word</li>
            <li>Even words that appear frequently in a document but rarely in the corpus get equal weight</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'>πŸš€ Solution: Adding Weightage</h3>
        <ul>
            <li>Consider a document with 3 words: <strong>w1, w2, w3</strong></li>
            <li>Each word has a vector representation:
                <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
                w1 β†’ v1,  w2 β†’ v2,  w3 β†’ v3
                </pre>
            </li>
            <li>We use <span class='highlight'>two models</span>:
                <ul>
                    <li><strong>TF-IDF</strong> β†’ Computes weightage for each word</li>
                    <li><strong>Word2Vec</strong> β†’ Converts words into vectors</li>
                </ul>
            </li>
            <li>For each word, multiply its TF-IDF value with its vector</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <strong>Final Weighted Representation:</strong>
        <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
        v_final = (TF-IDF(w1) * v1 + TF-IDF(w2) * v2 + TF-IDF(w3) * v3) 
                 / (TF-IDF(w1) + TF-IDF(w2) + TF-IDF(w3))
        </pre>
    """,
    unsafe_allow_html=True,
    )
    st.subheader("How to train our own W2V model")
    st.markdown('''
    - At training time Corpus + W2V algorithm can be implemented by 2 techniques
    - They are:
        - Skip-gram
        - CBOW
    ''')

    st.subheader(":red[CBOW]")
    st.markdown(
    """
    <div class='box'>
        <h3 style='color: black;'>What is CBOW?</h3>
        <p><strong>CBOW (Continuous Bag of Words)</strong> is a technique where we use surrounding words (context) to predict the target word (focus word).</p>
    </div>
    """,
    unsafe_allow_html=True,
    )
    st.markdown(
    """
        <h3 style='color: #6A0572;'>πŸ“‚ Example Corpus</h3>
        <ul>
            <li><strong>d1:</strong> w1, w2, w3, w4, w5, w4</li>
            <li><strong>d2:</strong> w3, w4, w5, w2, w1, w2, w3, w4</li>
        </ul>
        <p>We first preprocess the data to extract meaningful relationships.</p>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'>πŸ“Œ Steps to Process the Data</h3>
        <ul>
            <li>Create a <span class='highlight'>vocabulary</span> from the entire corpus: <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">{w1, w2, w3, w4, w5}</pre></li>
            <li>Generate a <strong>tabular dataset</strong> with:
                <ul>
                    <li><strong>Feature variables (Context Words)</strong></li>
                    <li><strong>Class variables (Target Words)</strong></li>
                </ul>
            </li>
            <li>Apply a <span class='highlight'>window size</span> of 2 (how many neighbors we consider).</li>
            <li>Slide the window over the text with <span class='highlight'>slide = 1</span>.</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'> Handling Variable Context Length</h3>
        <ul>
            <li>To ensure a consistent feature length, we use <strong>zero-padding</strong> when needed.</li>
            <li>The model tries to understand relationships based on the surrounding <span class='highlight'>context words</span>.</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )
    st.markdown(
    """
        <strong>Mathematical Representation:</strong>
        <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
        y = f(xi)
        where,
        y = Focus Word (Target)
        xi = Context Words (Neighbors)
        </pre>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: white;'> Training with Artificial Neural Networks</h3>
        <p>The tabular data is passed to an <strong>Artificial Neural Network (ANN)</strong> which learns:</p>
        <ul>
            <li>How <span class='highlight'>context words</span> are related to <span class='highlight'>focus words</span>.</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.subheader(":red[Skipgram]")
    st.markdown(
    """
    <div class='box'>
        <h3 style='color:black;'>What is Skipgram?</h3>
        <p><strong>Skipgram</strong> is a technique where we use focus words to predict the context words.</p>
    </div>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'>πŸ“‚ Example Corpus</h3>
        <ul>
            <li><strong>d1:</strong> w1, w2, w3, w4, w5, w4</li>
            <li><strong>d2:</strong> w3, w4, w5, w2, w1, w2, w3, w4</li>
        </ul>
        <p>We first preprocess the data to extract meaningful relationships.</p>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'>πŸ“Œ Steps to Process the Data</h3>
        <ul>
            <li>Create a <span class='highlight'>vocabulary</span> from the entire corpus: <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">{w1, w2, w3, w4, w5}</pre></li>
            <li>Generate a <strong>tabular dataset</strong> with:
                <ul>
                    <li><strong>Feature variables (Focus Words)</strong></li>
                    <li><strong>Class variables (Context Words)</strong></li>
                </ul>
            </li>
            <li>Apply a <span class='highlight'>window size</span> of 2 (how many neighbors we consider).</li>
            <li>Slide the window over the text with <span class='highlight'>slide = 1</span>.</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'> Handling Variable Context Length</h3>
        <ul>
            <li>To ensure a consistent feature length, we use <strong>zero-padding</strong> when needed.</li>
            <li>The model tries to understand relationships<span class='highlight'>focus words</span>.</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <strong>Mathematical Representation:</strong>
        <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
        y = f(xi)
        where,
        y = Context Word 
        xi = Focus Words 
        </pre>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'> Training with Artificial Neural Networks</h3>
        <p>The tabular data is passed to an <strong>Artificial Neural Network (ANN)</strong> which learns:</p>
        <ul>
            <li>How <span class='highlight'>focus words</span> are related with <span class='highlight'>context words</span>.</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )


elif file_type == "Fasttext":
    st.title(":red[Fasttext]")
    st.markdown(
    """
        <p><strong>FastText</strong> is an advanced word vectorization technique that enhances word embeddings by considering subword information.</p>
        <p>It is a <span class='highlight'>simple extension</span> of Word2Vec, which converts words into vectors.</p>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'> Implementing FastText</h3>
        <p>FastText can be implemented using:</p>
        <ul>
            <li><strong>CBOW (Continuous Bag of Words)</strong></li>
            <li><strong>Skip-gram</strong></li>
        </ul>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <strong>CBOW Representation:</strong>
        <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
        y = f(xi)
        where,
        y = Focus Word
        xi = Context Words
        </pre>
        <strong>Skip-gram Representation:</strong>
        <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
        y = f(xi)
        where,
        y = Context Words
        xi = Focus Word
        </pre>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'> Problem: Out-of-Vocabulary (OOV)</h3>
        <p>Traditional word embedding techniques fail when encountering new or rare words.</p>
        <p><span class='highlight'>FastText overcomes this issue</span> by breaking words into subword units (character n-grams).</p>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'>Implementing CBOW with Character N-Grams</h3>
        <ul>
            <li><span class='highlight'>Window Size</span>: 5</li>
            <li><span class='highlight'>Window</span>: 2</li>
            <li><span class='highlight'>Slide</span>: 1</li>
        </ul>
        <p>A tabular format is created with <strong>context words</strong> and <strong>focus words</strong>.</p>
    """,
    unsafe_allow_html=True,
    )
    st.markdown(
    """
    ## Example Sentences:
    - **d1:** "apple is good for health"
    - **d2:** "biryani is not good for health"
    
    This application creates a table for **context words** and **focus words** using **character 2-grams**.
    """
    )

    st.markdown('''
    -Character 2-Gram Table:
    
        - "Context Words": ["ap", "pp", "pl", "le", "is"]
        
        - "Focus Words": ["go", "oo", "od"]
    ''')

    st.markdown(
    """
    - This representation provides an **average 2D vector** for words.
    """
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'>Vocabulary</h3>
        <p>The vocabulary consists of <span class='highlight'>unique character n-grams</span>.</p>
        <pre style="background-color:#F7F7F7; padding: 10px; border-radius: 5px;">
        { keys: values }
        where,
        - Keys: Character n-grams
        - Values: Vector representations
        </pre>
    """,
    unsafe_allow_html=True,
    )

    st.markdown(
    """
        <h3 style='color: #6A0572;'> FastText Model</h3>
        <ul>
            <li>The dictionary created is the <span class='highlight'>FastText model</span>.</li>
            <li>Text is broken down into <strong>character n-grams</strong> to generate vector representations.</li>
            <li>It follows <span class='highlight'>element-wise addition</span>, giving an <strong>average 2D representation</strong> of the word.</li>
        </ul>
    """,
    unsafe_allow_html=True,
    )