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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# global
from typing import Tuple, List
import re
import numpy as np
import pandas as pd

import tensorflow as tf
from tensorflow import keras
from keras.utils import pad_sequences
from keras.preprocessing.text import Tokenizer
from gensim.models.doc2vec import Doc2Vec

import transformers
from transformers import pipeline, BertTokenizer

import fasttext

# local
from preprocessing import Preprocessor
from utils import read_data


# read data
X_train, X_test, y_train, y_test = read_data()

# instantiate preprocessor object
preprocessor = Preprocessor()

# load models
doc2vec_model_embeddings = Doc2Vec.load(
    "./models/best_doc2vec_embeddings")
doc2vec_model = keras.models.load_model(
    "./models/best_doc2vec_model.h5")
tfidf_model = keras.models.load_model(
    "./models/best_tfidf_model.h5")
cnn_model = keras.models.load_model(
    "./models/best_cnn_model.h5")
glove_model = keras.models.load_model(
    "./models/best_glove_model.h5")
lstm_model = keras.models.load_model(
    "./models/best_lstm_model.h5")
bert_model = keras.models.load_model(
    "./models/best_bert_model.h5", custom_objects={"TFBertModel": transformers.TFBertModel})
fasttext_model = fasttext.load_model(
    "./models/best_fasttext_model.bin")
summarization_model = pipeline(
    "summarization", model="facebook/bart-large-cnn")


# TODO: Add Docstrings
def extract_case_information(case_content: str):
    content_list = case_content.split("\n")
    petitioner = re.findall(r"petitioner:(.+)", content_list[0])[0]
    respondent = re.findall(r"respondent:(.+)", content_list[1])[0]
    facts = re.findall(r"facts:(.+)", content_list[2])[0]

    return petitioner, respondent, facts


def generate_random_sample() -> Tuple[str, str, str, int]:
    """
    Randomly fetch a random case from `X_test` to test it.

    Returns:
    --------
    A tuple contains the following:
        - petitioner : str
            Contains petitioner name.
        - respondent : str
            Contains respondent name.
        - facts : str
            Contains case facts.
        - label : int
            Represents the winning index(0 = petitioner, 1 = respondent).
    """

    random_idx = np.random.randint(low=0, high=len(X_test))

    petitioner = X_test["first_party"].iloc[random_idx]
    respondent = X_test["second_party"].iloc[random_idx]
    facts = X_test["Facts"].iloc[random_idx]
    label = y_test.iloc[random_idx][0]

    return petitioner, respondent, facts, label


def generate_highlighted_words(facts: str, petitioner_words: List[str], respondent_words: List[str]):
    """
    Highlight `petitioner_words` and `respondent_words` for model
    interpretation.

    Parameters:
    -----------
        - facts : str
            Facts of a specific case.
        - petitioner_words : List[str]
            Contains all words that model pays attention 
            to be a petetioner words.
        - respondent_words : List[str]
            Contains all words that model pays attention
            to be a respondent words.

    Returns:
    --------
        - rendered_text : str
            Contains the `facts` but with adding
            highlighting mechanism to visualize it using CSS in HTML format.

    Example:
    --------
        >>> facts_ = 'Mohammed shot Aly after a hot negotiation happened  between
        ... them about the profits of their company'
        >>> petitioner_words_ = ['shot', 'hot']
        >>> respondent_words_ = ['profits']
        >>> generate_highlighted_words(facts, petitioner_words_, respondent_words_)

        >>> output:
        <div class='text-facts'> Mohammed <span class='highlight-petitioner'>shot</span>
        Aly after a <span class='highlight-petitioner'>hot</span> negotiation happened
        between them about <span class='highlight-respondent'>profits</span> of their
        company </div>
    """

    rendered_text = '<div class="text-facts"> '

    for word in facts.split():
        if word in petitioner_words:
            highlight_word = ' <span class="highlight-petitioner"> ' + word + " </span> "
            rendered_text += highlight_word

        elif word in respondent_words:
            highlight_word = ' <span class="highlight-respondent"> ' + word + " </span> "
            rendered_text += highlight_word

        else:
            rendered_text += " " + word

    rendered_text += " </div>"

    return rendered_text


class VectorizerGenerator:
    """Responsible for creation and generation of tokenizers and text 
    vectorizers for JudgerAIs' models"""

    def __init__(self) -> None:
        pass

    def generate_tf_idf_vectorizer(self) -> keras.layers.TextVectorization:
        """
        Generating best text vectroizer of the tf-idf model (3rd combination).

        Returns:
        -------
        - text_vectorizer : keras.layers.TextVectorization
            Represents the case facts' vectorizer that converts case facts to 
            numerical tensors. 
        """

        first_party_names = X_train["first_party"]
        second_party_names = X_train["second_party"]
        facts = X_train["Facts"]

        anonymized_facts = preprocessor.anonymize_data(
            first_party_names, second_party_names, facts)

        text_vectorizer, _ = preprocessor.convert_text_to_vectors_tf_idf(
            anonymized_facts)

        return text_vectorizer

    def generate_cnn_vectorizer(self) -> keras.layers.TextVectorization:
        """
        Generating best text vectroizer of the cnn model (2nd combination).

        Returns:
        -------
        - text_vectorizer : keras.layers.TextVectorization
            Represents the case facts' vectorizer that converts case facts to 
            numerical tensors. 
        """

        balanced_df = preprocessor.balance_data(X_train["Facts"], y_train)
        X_train_balanced = balanced_df["Facts"]

        text_vectorizer, _ = preprocessor.convert_text_to_vectors_cnn(
            X_train_balanced)

        return text_vectorizer

    def generate_glove_tokenizer(self) -> keras.preprocessing.text.Tokenizer:
        """
        Generating best glove tokenizer of the GloVe model (2nd combination).

        Returns:
        -------
        - glove_tokenizer : keras.preprocessing.text.Tokenizer
            Represents the case facts' tokenizer that converts case facts to 
            numerical tensors. 
        """

        balanced_df = preprocessor.balance_data(X_train["Facts"], y_train)
        X_train_balanced = balanced_df["Facts"]

        glove_tokenizer, _ = preprocessor.convert_text_to_vectors_glove(
            X_train_balanced)

        return glove_tokenizer

    def generate_lstm_tokenizer(self) -> keras.preprocessing.text.Tokenizer:
        """
        Generating best text tokenizer of the LSTM model (1st combination).

        Returns:
        -------
        - lstm_tokenizer : keras.preprocessing.text.Tokenizer
            Represents the case facts' tokenizer that converts case facts to 
            numerical tensors. 
        """

        lstm_tokenizer = Tokenizer(num_words=18430)
        lstm_tokenizer.fit_on_texts(X_train)

        return lstm_tokenizer

    def generate_bert_tokenizer(self) -> transformers.BertTokenizer:
        """
        Generating best bert tokenizer of the BERT model (1st combination).

        Returns:
        -------
        - bert_tokenizer : transformers.BertTokenizer
            Represents the case facts' tokenizer that converts case facts to 
            input ids tensors. 
        """

        bert_tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
        return bert_tokenizer


class DataPreparator:
    """Responsible for preparing the case facts aka converting case facts to
    numerical vectors using `VectorizerGenerator` object."""

    def __init__(self) -> None:
        self.vectorizer_generator = VectorizerGenerator()

    def prepare_doc2vec(self, facts: str) -> pd.DataFrame:
        """
        Responsible for converting `facts` string to numerical vector
        using `doc2vec_model_embeddings`.

        Parameters:
        ----------
        - facts : str
            Represents the case facts.

        Returns:
        -------
        - facts_vector : pd.DataFrame
            A row DataFrame represents the 50-d vector of the `facts`. 
        """

        facts = pd.Series(facts)
        facts_processed = preprocessor.preprocess_data(facts)
        facts_vectors = preprocessor.convert_text_to_vectors_doc2vec(
            facts_processed, train=False, embeddings_doc2vec=doc2vec_model_embeddings)

        return facts_vectors

    def _anonymize_facts(self, first_party_name: str, second_party_name: str, facts: str) -> str:
        """
        Anonymize case `facts` by replacing `first_party_name` & `second_party_name` with 
        generic tag "__PARTY__".

        Parameters:
        -----------
        - first_party_name : str
            Represents the petitioner name.
        - second_party_name : str
            Represents the respondent name.    
        - facts : str
            Represents the case facts.

        Returns:
        -------
        - anonymized_facts : str
            Represents `facts` after anonymization. 
        """

        anonymized_facts = preprocessor._anonymize_case_facts(
            first_party_name, second_party_name, facts)

        return anonymized_facts

    def prepare_tf_idf(self, anonymized_facts: str) -> tf.Tensor:
        """
        Responsible for converting `facts` string to numerical vector
        using tf-idf `vectorizer_generator` in the 3rd combination.

        Parameters:
        -----------    
        - anonymized_facts : str
            Represents the case facts after anonymization.

        Returns:
        -------
        - facts_vector : tf.Tensor
            A Tensor of 10000-d represents `facts`. 
        """

        anonymized_facts = pd.Series(anonymized_facts)
        tf_idf_vectorizer = self.vectorizer_generator.generate_tf_idf_vectorizer()

        facts_vector = preprocessor.convert_text_to_vectors_tf_idf(
            anonymized_facts, train=False, text_vectorizer=tf_idf_vectorizer)

        return facts_vector

    def prepare_cnn(self, facts: str) -> tf.Tensor:
        """
        Responsible for converting `facts` string to numerical vector
        using cnn `vectorizer_generator` in the 2nd combination.

        Parameters:
        -----------
        - facts : str
            Represents the case facts.

        Returns:
        -------
        - facts_vector : tf.Tensor
            A Tensor of 2000-d represents `facts`. 
        """
        facts = pd.Series(facts)

        cnn_vectorizer = self.vectorizer_generator.generate_cnn_vectorizer()

        facts_vector = preprocessor.convert_text_to_vectors_cnn(
            facts, train=False, text_vectorizer=cnn_vectorizer)

        return facts_vector

    def prepare_glove(self, facts: str) -> np.ndarray:
        """
        Responsible for converting `facts` string to numerical vector
        using glove `vectorizer_generator` in the 2nd combination.

        Parameters:
        -----------
        - facts : str
            Represents the case facts.

        Returns:
        -------
        - facts_vector : np.ndarray
            A nd.ndarray of 50-d represents `facts`. 
        """

        facts = pd.Series(facts)

        glove_tokneizer = self.vectorizer_generator.generate_glove_tokenizer()

        facts_vector = preprocessor.convert_text_to_vectors_glove(
            facts, train=False, glove_tokenizer=glove_tokneizer)

        return facts_vector

    def prepare_lstm(self, facts: str) -> np.ndarray:
        """
        Responsible for converting `facts` string to numerical vector
        using lstm `vectorizer_generator` in the 1st combination.

        Parameters:
        -----------
        - facts : str
            Represents the case facts.

        Returns:
        -------
        - facts_vector_padded : np.ndarray
            A nd.ndarray of 974-d represents `facts`. 
        """

        facts = pd.Series(facts)
        lstm_tokenizer = self.vectorizer_generator.generate_lstm_tokenizer()
        facts_vector = lstm_tokenizer.texts_to_sequences(facts)
        facts_vector_padded = pad_sequences(facts_vector, 974)

        return facts_vector_padded

    def prepare_bert(self, facts: str) -> tf.Tensor:
        """
        Responsible for converting `facts` string to numerical vector
        using bert `vectorizer_generator` in the 1st combination.

        Parameters:
        -----------
        - facts : str
            Represents the case facts.

        Returns:
        -------
        - tf.Tensor
            A tf.Tensor of 256-d represents `facts` input ids. 
        """

        bert_tokenizer = self.vectorizer_generator.generate_bert_tokenizer()
        facts_vector_dict = bert_tokenizer.encode_plus(
            facts,
            max_length=256,
            truncation=True,
            padding='max_length',
            add_special_tokens=True,
            return_tensors='tf'
        )

        return facts_vector_dict["input_ids"]


class Predictor:
    """Responsible for get predictions of JudgerAIs' models"""

    def __init__(self) -> None:
        self.data_preparator = DataPreparator()

    def predict_doc2vec(self, facts: str) -> np.ndarray:
        """
        Get prediction of `facts` using `doc2vec_model`.

        Parameters:
        ----------
        - facts : str
            Represents the case facts.

        Returns:
        --------
        - pet_res_scores : np.ndarray
            An array contains 2 elements, one for probability of petitioner winning
            and the second for the probability of respondent winning.
        """

        facts_vector = self.data_preparator.prepare_doc2vec(facts)
        predictions = doc2vec_model.predict(facts_vector)

        pet_res_scores = []
        for i in predictions:
            temp = i[0]
            pet_res_scores.append(np.array([1 - temp, temp]))

        return np.array(pet_res_scores)

    def predict_tf_idf(self, anonymized_facts: str) -> np.ndarray:
        """
        Get prediction of `facts` using `tfidf_model`.

        Parameters:
        -----------
        - anonymized_facts : str
            Represents the case facts after anonymization.

        Returns:
        --------
        - pet_res_scores : np.ndarray
            An array contains 2 elements, one for probability of petitioner winning
            and the second for the probability of respondent winning.
        """

        facts_vector = self.data_preparator.prepare_tf_idf(anonymized_facts)
        predictions = tfidf_model.predict(facts_vector)

        pet_res_scores = []
        for i in predictions:
            temp = i[0]
            pet_res_scores.append(np.array([1 - temp, temp]))

        return np.array(pet_res_scores)

    def predict_cnn(self, facts: str) -> np.ndarray:
        """
        Get prediction of `facts` using `cnn_model`.

        Parameters:
        ----------
        - facts : str
            Represents the case facts.

        Returns:
        --------
        - pet_res_scores : np.ndarray
            An array contains 2 elements, one for probability of petitioner winning
            and the second for the probability of respondent winning.
        """

        facts_vector = self.data_preparator.prepare_cnn(facts)
        predictions = cnn_model.predict(facts_vector)

        pet_res_scores = []
        for i in predictions:
            temp = i[0]
            pet_res_scores.append(np.array([1 - temp, temp]))

        return np.array(pet_res_scores)

    def predict_glove(self, facts: str) -> np.ndarray:
        """
        Get prediction of `facts` using `glove_model`.

        Parameters:
        ----------
        - facts : str
            Represents the case facts.

        Returns:
        --------
        - pet_res_scores : np.ndarray
            An array contains 2 elements, one for probability of petitioner winning
            and the second for the probability of respondent winning.
        """

        facts_vector = self.data_preparator.prepare_glove(facts)
        predictions = glove_model.predict(facts_vector)

        pet_res_scores = []
        for i in predictions:
            temp = i[0]
            pet_res_scores.append(np.array([1 - temp, temp]))

        return np.array(pet_res_scores)

    def predict_lstm(self, facts: str) -> np.ndarray:
        """
        Get prediction of `facts` using `lstm_model`.

        Parameters:
        ----------
        - facts : str
            Represents the case facts.

        Returns:
        --------
        - pet_res_scores : np.ndarray
            An array contains 2 elements, one for probability of petitioner winning
            and the second for the probability of respondent winning.
        """

        facts_vector = self.data_preparator.prepare_lstm(facts)
        predictions = lstm_model.predict(facts_vector)

        pet_res_scores = []
        for i in predictions:
            temp = i[0]
            pet_res_scores.append(np.array([1 - temp, temp]))

        return np.array(pet_res_scores)

    def predict_bert(self, facts: str) -> np.ndarray:
        """
        Get prediction of `facts` using `bert_model`.

        Parameters:
        ----------
        - facts : str
            Represents the case facts.

        Returns:
        --------
        - predictions : np.ndarray
            An array contains 2 elements, one for probability of petitioner winning
            and the second for the probability of respondent winning.
        """

        facts_vector = self.data_preparator.prepare_bert(facts)
        predictions = bert_model.predict(facts_vector)

        return predictions

    def predict_fasttext(self, facts: str) -> np.ndarray:
        """
        Get prediction of `facts` using `fasttext`.

        Parameters:
        ----------
        - facts : str
            Represents the case facts.

        Returns:
        --------
        - pet_res_scores : np.ndarray
            An array contains 2 elements, one for probability of petitioner winning
            and the second for the probability of respondent winning.
        """

        prediction = fasttext_model.predict(facts)[1]
        prediction = np.array([prediction])

        pet_res_scores = []
        for i in prediction:
            temp = i[0]
            pet_res_scores.append(np.array([1 - temp, temp]))

        return np.array(pet_res_scores)

    def summarize_facts(self, facts: str) -> str:
        summarized_case_facts = summarization_model(facts)[0]['summary_text']
        return summarized_case_facts