File size: 10,368 Bytes
8fa594d
 
 
 
 
 
 
 
 
 
 
 
dd446cb
 
8fa594d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e1afd6
8fa594d
 
 
 
6e1afd6
 
 
 
 
 
 
 
 
 
 
 
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
import nltk
import sklearn_crfsuite
from sklearn_crfsuite import metrics
from nltk.stem import LancasterStemmer
import numpy as np
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import re
import gradio as gr
lancaster = LancasterStemmer()

nltk.download('brown')
nltk.download('universal_tagset')

class CRF_POS_Tagger:
    def __init__(self):
        self.corpus = nltk.corpus.brown.tagged_sents(tagset='universal')
        self.corpus = [[(word.lower(), tag) for word, tag in sentence] for sentence in self.corpus]
        self.actual_tag = []
        self.predicted_tag = []
        self.prefixes = [
            "a", "anti", "auto", "bi", "co", "dis", "en", "em", "ex", "in", "im", 
            "inter", "mis", "non", "over", "pre", "re", "sub", "trans", "un", "under"
        ]

        self.suffixes = [
            "able", "ible", "al", "ance", "ence", "dom", "er", "or", "ful", "hood", 
            "ic", "ing", "ion", "tion", "ity", "ty", "ive", "less", "ly", "ment", 
            "ness", "ous", "ship", "y", "es", "s"
        ]

        self.prefix_pattern = f"^({'|'.join(self.prefixes)})"
        self.suffix_pattern = f"({'|'.join(self.suffixes)})$"

        self.X = [[self.word_features(sentence, i) for i in range(len(sentence))] for sentence in self.corpus]
        self.y = [[postag for _, postag in sentence] for sentence in self.corpus]
        
        self.split = int(0.8 * len(self.X))
        self.X_train = self.X[:self.split]
        self.y_train = self.y[:self.split]
        self.X_test = self.X[self.split:]
        self.y_test = self.y[self.split:]
        self.crf_model = sklearn_crfsuite.CRF(algorithm='lbfgs', c1=0.1, c2=0.1, max_iterations=100, all_possible_transitions=True)
        self.train()

    def word_splitter(self, word):
        prefix = ""
        stem = word
        suffix = ""

        prefix_match = re.match(self.prefix_pattern, word)
        if prefix_match:
            prefix = prefix_match.group(1)
            stem = word[len(prefix):]

        suffix_match = re.search(self.suffix_pattern, stem)
        if suffix_match:
            suffix = suffix_match.group(1)
            stem = stem[: -len(suffix)]

        return prefix, stem, suffix
    
    # Define a function to extract features for each word in a sentence
    def word_features(self, sentence, i):
        word = sentence[i][0]
        prefix, stem, suffix = self.word_splitter(word)
        features = {
            'word': word,
            'prefix': prefix,
            # 'stem': stem,
            'stem': lancaster.stem(word),
            'suffix': suffix,
            'position': i,
            'is_first': i == 0, #if the word is a first word
            'is_last': i == len(sentence) - 1,  #if the word is a last word
            # 'is_capitalized': word[0].upper() == word[0],
            'is_all_caps': word.isupper(),      #word is in uppercase
            'is_all_lower': word.islower(),      #word is in lowercase
            
            'prefix-1': word[0],
            'prefix-2': word[:2],
            'prefix-3': word[:3],
            'suffix-1': word[-1],
            'suffix-2': word[-2:],
            'suffix-3': word[-3:],

            'prefix-un': word[:2] == 'un',   #if word starts with un
            'prefix-re': word[:2] == 're',   #if word starts with re
            'prefix-over': word[:4] == 'over',  #if word starts with over
            'prefix-dis': word[:4] == 'dis',   #if word starts with dis
            'prefix-mis': word[:4] == 'mis',   #if word starts with mis
            'prefix-pre': word[:4] == 'pre',   #if word starts with pre
            'prefix-non': word[:4] == 'non',   #if word starts with non
            'prefix-de': word[:3] == 'de',     #if word starts with de
            'prefix-in': word[:3] == 'in',     #if word starts with in
            'prefix-en': word[:3] == 'en',     #if word starts with en
            
            'suffix-ed': word[-2:] == 'ed',   #if word ends with ed
            'suffix-ing': word[-3:] == 'ing',  #if word ends with ing
            'suffix-es': word[-2:] == 'es',    #if word ends with es
            'suffix-ly': word[-2:] == 'ly',    #if word ends with ly
            'suffix-ment': word[-4:] == 'ment',  #if word ends with ment
            'suffix-er': word[-2:] == 'er',     #if word ends with er 
            'suffix-ive': word[-3:] == 'ive',
            'suffix-ous': word[-3:] == 'ous',
            'suffix-ness': word[-4:] == 'ness',
            'ends_with_s': word[-1] == 's', 
            'ends_with_es': word[-2:] == 'es',

            'has_hyphen': '-' in word,    #if word has hypen
            'is_numeric': word.isdigit(),  #if word is in numeric
            'capitals_inside': word[1:].lower() != word[1:],
            'is_title_case': word.istitle(),  #if first letter is in uppercase
            
        }
        
        if i > 0:
            # prev_word, prev_postag = sentence[i-1]
            prev_word = sentence[i-1][0]
            prev_prefix, prev_stem, prev_suffix = self.word_splitter(prev_word)
            
            features.update({
                'prev_word': prev_word,
                # 'prev_postag': prev_postag,
                'prev_prefix': prev_prefix,
                'prev_stem': lancaster.stem(prev_word),
                'prev_suffix': prev_suffix,
                'prev:is_all_caps': prev_word.isupper(),
                'prev:is_all_lower': prev_word.islower(),
                'prev:is_numeric': prev_word.isdigit(),
                'prev:is_title_case': prev_word.istitle(),  
            })

        if i < len(sentence)-1:
            next_word = sentence[i-1][0]
            next_prefix, next_stem, next_suffix = self.word_splitter(next_word)
            features.update({
                'next_word': next_word,
                'next_prefix': next_prefix,
                'next_stem': lancaster.stem(next_word),
                'next_suffix': next_suffix,
                'next:is_all_caps': next_word.isupper(),
                'next:is_all_lower': next_word.islower(),
                'next:is_numeric': next_word.isdigit(),
                'next:is_title_case': next_word.istitle(),  
            })
        
        return features

    def train(self):
        self.crf_model.fit(self.X_train, self.y_train)
    
    def predict(self, X_test):
        return self.crf_model.predict(X_test)
    
    def accuracy(self, test_data):
        X_test, y_test = zip(*test_data)
        y_pred = self.predict(X_test)
        self.actual_tag.extend([item for sublist in y_test for item in sublist])
        self.predicted_tag.extend([item for sublist in y_pred for item in sublist])
        return metrics.flat_accuracy_score(y_test, y_pred)

    def cross_validation(self, data):
        accuracies = []
        for i in range(5):
            n1 = int(i / 5.0 * len(data))
            n2 = int((i + 1) / 5.0 * len(data))
            test_data = data[n1:n2]
            train_data = data[:n1] + data[n2:]
            self.train(train_data)
            acc = self.accuracy(test_data)
            accuracies.append(acc)
        return accuracies, sum(accuracies) / 5.0

    def con_matrix(self):
        self.labels = np.unique(self.actual_tag) 
        conf_matrix = confusion_matrix(self.actual_tag, self.predicted_tag, labels=self.labels)
        
        plt.figure(figsize=(10, 7))
        sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=self.labels, yticklabels=self.labels)
        plt.xlabel('Predicted Tags')
        plt.ylabel('Actual Tags')
        plt.title('Confusion Matrix Heatmap')
        plt.savefig("Confusion_matrix.png")
        plt.show()
        
        return conf_matrix
    
    def per_pos_accuracy(self, conf_matrix):
        print("Per Tag Precision, Recall, and F-Score:")
        per_tag_metrics = {}

        for i, tag in enumerate(self.labels):
            true_positives = conf_matrix[i, i]
            false_positives = np.sum(conf_matrix[:, i]) - true_positives
            false_negatives = np.sum(conf_matrix[i, :]) - true_positives

            precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0
            recall = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0
            f1_score = (2 * precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
            beta_0_5 = 0.5
            beta_2 = 2.0

            f0_5_score = (1 + beta_0_5**2) * (precision * recall) / ((beta_0_5**2 * precision) + recall) if (precision + recall) > 0 else 0
            f2_score = (1 + beta_2**2) * (precision * recall) / ((beta_2**2 * precision) + recall) if (precision + recall) > 0 else 0

            per_tag_metrics[tag] = {
                'Precision': precision,
                'Recall': recall,
                'f1-Score': f1_score,
                'f05-Score': f0_5_score,
                'f2-Score': f2_score
            }

            print(f"{tag}: Precision = {precision:.2f}, Recall = {recall:.2f}, f1-Score = {f1_score:.2f}, "
                  f"f05-Score = {f0_5_score:.2f}, f2-Score = {f2_score:.2f}")
            
    def tagging(self, input):
        sentence = (re.sub(r'(\S)([.,;:!?])', r'\1 \2', input.strip())).split()
        sentence_list = [[word] for word in sentence]
        features = [self.word_features(sentence_list, i) for i in range(len(sentence_list))]

        predicted_tags = self.crf_model.predict([features])
        output = "".join(f"{sentence[i]}[{predicted_tags[0][i]}]    " for i in range(len(sentence)))
        return output

tagger = CRF_POS_Tagger()
interface = gr.Interface(fn = tagger.tagging, 
                            inputs = gr.Textbox(
                            label="Input Sentence",  
                            placeholder="Enter your sentence here...",
                        ),
                         outputs = gr.Textbox(
                            label="Tagged Output",  
                            placeholder="Tagged sentence appears here...",
                        ),
                         title = "Conditional Random Field POS Tagger",
                         description = "CS626 Assignment 1B (Autumn 2024)",
                         theme=gr.themes.Soft())
interface.launch()