Spaces:
Runtime error
Runtime error
File size: 18,184 Bytes
17cde60 301fee0 17cde60 301fee0 17cde60 c94e335 301fee0 c94e335 afa07cf c94e335 17cde60 301fee0 17cde60 301fee0 17cde60 301fee0 17cde60 301fee0 17cde60 301fee0 17cde60 fd7447e 17cde60 fd7447e 17cde60 3a64875 17cde60 3a64875 17cde60 301fee0 17cde60 301fee0 17cde60 301fee0 17cde60 44d1a52 17cde60 c94e335 17cde60 4546cca 17cde60 06e1dab 17cde60 06e1dab 17cde60 06e1dab 17cde60 06e1dab 17cde60 06e1dab 17cde60 06e1dab 17cde60 83b3e3a 17cde60 06e1dab 17cde60 3a64875 17cde60 c94e335 17cde60 3a64875 17cde60 c94e335 17cde60 5cd173e 17cde60 c94e335 17cde60 c94e335 17cde60 c94e335 17cde60 3a64875 17cde60 3a64875 17cde60 44d1a52 17cde60 06e1dab 17cde60 3a64875 17cde60 06e1dab 3a64875 bda0f5a 3a64875 17cde60 f120eee 17cde60 44d1a52 |
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 |
import gradio as gr
import re
import json
import numpy as np
import nltk
import stanza
from stanza.models.constituency.parse_tree import Tree
from transformers import AutoTokenizer, AutoModelForTokenClassification, TokenClassificationPipeline
from sentence_transformers import CrossEncoder
from autocorrect import Speller
from transformers import BertTokenizer, BertForSequenceClassification
import torch
from torch.nn.utils.rnn import pad_sequence
# ***************************** Load needed models *****************************
nlp = stanza.Pipeline(lang='en', processors='tokenize,pos,constituency')
pos_tokenizer = AutoTokenizer.from_pretrained("QCRI/bert-base-multilingual-cased-pos-english")
pos_model = AutoModelForTokenClassification.from_pretrained("QCRI/bert-base-multilingual-cased-pos-english")
#sentences_similarity_model = CrossEncoder('cross-encoder/stsb-roberta-base')
sentences_similarity_model = CrossEncoder('WillHeld/roberta-base-stsb')
nli_model = BertForSequenceClassification.from_pretrained("nouf-sst/bert-base-MultiNLI", use_auth_token="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")
nli_tokenizer = BertTokenizer.from_pretrained("nouf-sst/bert-base-MultiNLI", use_auth_token="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", do_lower_case=True)
# ***************************** TGRL Parsing *****************************
def parse_tgrl(file_obj):
with open(file_obj.name, 'r') as f:
tgrl_text = f.read()
tgrl_text = tgrl_text.replace('\t', '')
tgrl_text = tgrl_text.replace('\n', '')
return tgrl_text
def extract_elements(tgrl_text):
# Extract actors
actors = re.findall("(?:.*?actor\s\S+\s?{\s?name\s?=\s?\")([A-Za-z\s;.,!?:-]*)(?:\")", tgrl_text)
# Extract goals
goals = re.findall("(?:.*?goal\s\S+\s?{\s?name\s?=\s?\")([A-Za-z\s;.,!?:-]*)(?:\")", tgrl_text)
# Extract softGoals
softGoals = re.findall("(?:.*?softGoal\s\S+\s?{\s?name\s?=\s?\")([A-Za-z\s;.,!?:-]*)(?:\")", tgrl_text)
# Extract tasks
tasks = re.findall("(?:.*?task\s\S+\s?{\s?name\s?=\s?\")([A-Za-z\s;.,!?:-]*)(?:\")", tgrl_text)
# Extract resources
resources = re.findall("(?:.*?resource\s\S+\s?{\s?name\s?=\s?\")([A-Za-z\s;.,!?:-]*)(?:\")", tgrl_text)
elements = {
"actors": actors,
"goals": goals,
"softGoals": softGoals,
"tasks": tasks,
"resources": resources
}
# get elements per actor
elements_per_actor = {}
for goal in goals:
corresponding_actor = tgrl_text.rfind('actor', 0, tgrl_text.index(goal))
corresponding_actor = re.split(' |{', tgrl_text[corresponding_actor:])[1]
if corresponding_actor not in elements_per_actor:
elements_per_actor[corresponding_actor] = []
elements_per_actor[corresponding_actor].append(goal)
for softGoal in softGoals:
corresponding_actor = tgrl_text.rfind('actor', 0, tgrl_text.index(softGoal))
corresponding_actor = re.split(' |{', tgrl_text[corresponding_actor:])[1]
if corresponding_actor not in elements_per_actor:
elements_per_actor[corresponding_actor] = []
elements_per_actor[corresponding_actor].append(softGoal)
for task in tasks:
corresponding_actor = tgrl_text.rfind('actor', 0, tgrl_text.index(task))
corresponding_actor = re.split(' |{', tgrl_text[corresponding_actor:])[1]
if corresponding_actor not in elements_per_actor:
elements_per_actor[corresponding_actor] = []
elements_per_actor[corresponding_actor].append(task)
# get decomposed elements
new_lines = tgrl_text
decomposed_elements = {}
main_elements = re.findall("\w+(?=\s+decomposedBy)", new_lines)
for main_element in main_elements:
sub_elements = []
sub_element = (re.findall(main_element+"(?: decomposedBy )([A-Za-z\s]*)", new_lines)[0])
sub_elements.append(sub_element)
new_lines = new_lines.replace(sub_element+', ', '')
temp = main_element + " decomposedBy "
for idx, sub_element in enumerate(sub_elements):
if idx+1 == len (sub_elements):
temp = temp + sub_element + ";"
else:
temp = temp + sub_element + ", "
while temp not in tgrl_text:
sub_element = (re.findall(main_element+"(?: decomposedBy )([A-Za-z\s]*)", new_lines)[0])
sub_elements.append(sub_element)
new_lines = new_lines.replace(sub_element+', ', '')
temp = main_element + " decomposedBy "
for idx, sub_element in enumerate(sub_elements):
if idx+1 == len (sub_elements):
temp = temp + sub_element + ";"
else:
temp = temp + sub_element + ", "
decomposed_elements[main_element] = sub_elements
# Replace elements IDs with names
new_decomposed_elements = {}
for key, _ in decomposed_elements.items():
new_key = re.findall("(?:"+key+"\s*{\s*name\s=\s\")([A-Za-z\s;.,!?:-]*)", tgrl_text)[0]
new_values = []
for element in decomposed_elements[key]:
new_value = re.findall("(?:"+element+"\s*{\s*name\s=\s\")([A-Za-z\s;.,!?:-]*)", tgrl_text)[0]
new_values.append(new_value)
new_decomposed_elements[new_key] = new_values
return elements, elements_per_actor, new_decomposed_elements
# ************************************************************************
# ************************* Bad Smells Detection *************************
# ########### Long Elements ###########
def get_long_elements(elements, size_threshold): # Using RegEx
long_elements = []
for key, value in elements.items():
for i in range(0, len(elements[key])):
if len(re. findall(r'\w+', elements[key][i])) > size_threshold:
long_elements.append(elements[key][i])
if long_elements:
long_elements = "\n".join(long_elements)
return "Long elements:\n" + long_elements
else:
return "Long elements:\nNone."
# #####################################
# ######### Complex Sentences #########
def is_complex_sentence(sentence):
nlp = stanza.Pipeline(lang='en', processors='tokenize,pos,constituency')
doc = nlp(sentence)
for sentence in doc.sentences:
unique_constituent_labels = Tree.get_unique_constituent_labels(sentence.constituency)
if 'SBAR' in unique_constituent_labels:
return True
else:
return False
def get_complex_sentences(elements):
complex_sentences = []
for key, value in elements.items():
for i in range(0, len(elements[key])):
if is_complex_sentence(elements[key][i]):
complex_sentences.append(elements[key][i])
if complex_sentences:
complex_sentences = "\n".join(complex_sentences)
return "Complex sentences:\n" + complex_sentences
else:
return "Complex sentences:\nNone."
# #####################################
# ########## Punctuations #########
def get_punctuations(elements):
punctuations = []
for key, value in elements.items():
for i in range(0, len(elements[key])):
if len(re.findall("[^\s\w\d-]", elements[key][i])) > 0:
punctuations.append(elements[key][i])
if punctuations:
punctuations = "\n".join(punctuations)
return "Punctuations:\n" + punctuations
else:
return "Punctuations:\nNone."
# #################################
# ########## Incorrect Actor Syntax ##########
def find_non_NPs(sentences):
pipeline = TokenClassificationPipeline(model=pos_model, tokenizer=pos_tokenizer)
outputs = pipeline(sentences)
Non_NPs = []
for idx, output in enumerate(outputs):
if output[0]['entity'].startswith('V'):
Non_NPs.append(sentences[idx])
return Non_NPs
def check_actor_syntax(actors):
incorrect_actor_syntax = find_non_NPs(actors)
if incorrect_actor_syntax:
incorrect_actor_syntax = "\n".join(incorrect_actor_syntax)
return "Incorrect Actors Syntax:\n" + incorrect_actor_syntax
else:
return "All actors are syntactically correct."
# ############################################
# ########## Incorrect Goal Syntax ###########
def check_goal_syntax(goals):
incorrect_goal_syntax = find_non_NPs(goals)
if incorrect_goal_syntax:
incorrect_goal_syntax = "\n".join(incorrect_goal_syntax)
return "Incorrect Goals Syntax:\n" + incorrect_goal_syntax
else:
return "All goals are syntactically correct."
# ############################################
# ########## Incorrect Softgoal Syntax ###########
def check_softgoal_syntax(softgoals):
incorrect_softgoal_syntax = find_non_NPs(softgoals)
if incorrect_softgoal_syntax:
incorrect_softgoal_syntax = "\n".join(incorrect_softgoal_syntax)
return "Incorrect Softgoals Syntax:\n" + incorrect_softgoal_syntax
else:
return "All softgoal are syntactically correct."
# ############################################
# ########## Incorrect Task Syntax ###########
def find_NPs(sentences):
pipeline = TokenClassificationPipeline(model=model, tokenizer=tokenizer)
outputs = pipeline(sentences)
NPs = []
for idx, output in enumerate(outputs):
if not output[0]['entity'].startswith('V'):
NPs.append(sentences[idx])
return NPs
def check_task_syntax(tasks):
incorrect_task_syntax = find_NPs(tasks)
if incorrect_task_syntax:
incorrect_task_syntax = "\n".join(incorrect_task_syntax)
return "Incorrect Tasks Syntax:\n" + incorrect_task_syntax
else:
return "All tasks are syntactically correct."
# ############################################
# ########## Incorrect Resource Syntax ###########
def check_resource_syntax(resources):
incorrect_resource_syntax = find_non_NPs(resources)
if incorrect_resource_syntax:
incorrect_resource_syntax = "\n".join(incorrect_resource_syntax)
return "Incorrect Resources Syntax:\n" + incorrect_resource_syntax
else:
return "All resources are syntactically correct."
# ############################################
# ########## Similarity ###########
def get_similar_elements(elements_per_actor, similarity_threshold):
# Prepare sentence pair array
sentence_pairs = []
for key, value in elements_per_actor.items():
for i in range(len(elements_per_actor[key])):
for j in range(i+1,len(elements_per_actor[key])):
sentence_pairs.append([elements_per_actor[key][i], elements_per_actor[key][j]])
# Predict semantic similarity
semantic_similarity_scores = sentences_similarity_model.predict(sentence_pairs, show_progress_bar=True)
similar_elements = []
for index, value in enumerate(sentence_pairs):
if semantic_similarity_scores[index] > similarity_threshold:
similar_elements.append(value)
#semantic_similarity["pair_"+str(index+1)] = [value,semantic_similarity_scores[index]]
if similar_elements:
similar_elements = [' and '.join(ele) for ele in similar_elements]
similar_elements = "\n".join(similar_elements)
return "The following elements are semantically similar:\n" + similar_elements
else:
return "There are no similar elements."
return semantic_similarity
# #################################
# ########## Misspelling ###########
def get_misspelled_words(sentence):
spell = Speller(only_replacements=True)
misspelled= []
for word in sentence.split():
correct_word = spell(word)
if word != correct_word:
misspelled.append([word, correct_word])
return misspelled
def check_spelling(elements):
spelling_mistakes = []
spelling_mistakes_string = ""
for key, value in elements.items():
for i in range(0, len(elements[key])):
if get_misspelled_words(elements[key][i]):
spelling_mistakes.append([elements[key][i], get_misspelled_words(elements[key][i])])
for idx, element in enumerate(spelling_mistakes):
for spelling_mistake in element[1]:
temp = ' should be written as '.join(spelling_mistake)
spelling_mistakes_string = spelling_mistakes_string + "\n" + element[0] + ": " + temp
return spelling_mistakes_string
# ##################################
# ########## NLI ###########
def do_nli(premise, hypothesis):
# Tokenization
token_ids = []
seg_ids = []
mask_ids = []
premise_id = nli_tokenizer.encode(premise, add_special_tokens = False)
hypothesis_id = nli_tokenizer.encode(hypothesis, add_special_tokens = False)
pair_token_ids = [nli_tokenizer.cls_token_id] + premise_id + [nli_tokenizer.sep_token_id] + hypothesis_id + [nli_tokenizer.sep_token_id]
premise_len = len(premise_id)
hypothesis_len = len(hypothesis_id)
segment_ids = torch.tensor([0] * (premise_len + 2) + [1] * (hypothesis_len + 1)) # sentence 0 and sentence 1
attention_mask_ids = torch.tensor([1] * (premise_len + hypothesis_len + 3)) # mask padded values
token_ids.append(torch.tensor(pair_token_ids))
seg_ids.append(segment_ids)
mask_ids.append(attention_mask_ids)
# Forward pass
token_ids = pad_sequence(token_ids, batch_first=True)
mask_ids = pad_sequence(mask_ids, batch_first=True)
seg_ids = pad_sequence(seg_ids, batch_first=True)
with torch.no_grad():
output = nli_model(token_ids,
token_type_ids=seg_ids,
attention_mask=mask_ids)
# Output predication
result = ""
prediction = np.argmax(output.logits.cpu().numpy()).flatten().item()
if prediction == 0:
result = "Entailment"
#print("Entailment")
elif prediction == 1:
result = "Contradiction"
#print("Contradiction")
elif prediction == 2:
result = "Neutral"
#print("Neutral")
return result
# Entailment
def check_entailment(decomposed_elements):
sentence_pairs = []
non_matching_elements = []
for key, value in decomposed_elements.items():
#print(key, value)
for i in decomposed_elements[key]:
#print(key, i)
sentence_pairs.append([key, i])
for sentence_pair in sentence_pairs:
result = do_nli(sentence_pair[0], sentence_pair[1])
print(result)
if result != "Entailment":
non_matching_elements.append(sentence_pair)
if non_matching_elements:
non_matching_elements = [' and '.join(ele) for ele in non_matching_elements]
non_matching_elements = "\n".join(non_matching_elements)
return "The following elements are miss matching:\n" + non_matching_elements
else:
return "There are no miss matched elements."
return result
# Contradiction
def check_contradiction(elements_per_actor):
sentence_pairs = []
contradicting_elements = []
for key, value in elements_per_actor.items():
for i in range(len(elements_per_actor[key])):
for j in range(i+1,len(elements_per_actor[key])):
sentence_pairs.append([elements_per_actor[key][i], elements_per_actor[key][j]])
#print(sentence_pairs)
# Check contradiction
for sentence_pair in sentence_pairs:
result = do_nli(sentence_pair[0], sentence_pair[1])
#print(result)
if result == "Contradiction":
contradicting_elements.append(sentence_pair)
if contradicting_elements:
contradicting_elements = [' and '.join(ele) for ele in contradicting_elements]
contradicting_elements = "\n".join(contradicting_elements)
return "The following elements are contradicting:\n" + contradicting_elements
else:
return "There are no contradicting elements."
# ##########################
# ************************* User Interface *************************
def identify_bad_smells(tgrl_file, selected_bad_smells, size_threshold, similarity_threshold):
output = ""
tgrl_text = parse_tgrl(tgrl_file)
elements, elements_per_actor, decomposed_elements = extract_elements(tgrl_text)
if 'Size' in selected_bad_smells:
output = output + get_long_elements(elements, size_threshold) + "\n\n"
if 'Complexity' in selected_bad_smells:
output = output + get_complex_sentences(elements) + "\n\n"
if 'Punctuations' in selected_bad_smells:
output = output + get_punctuations(elements) + "\n\n"
if 'Actors Syntax' in selected_bad_smells:
output = output + check_actor_syntax(elements['actors']) + "\n\n"
if 'Goals Syntax' in selected_bad_smells:
output = output + check_goal_syntax(elements['goals']) + "\n\n"
if 'Softgoals Syntax' in selected_bad_smells:
output = output + check_softgoal_syntax(elements['softGoals']) + "\n\n"
if 'Tasks Syntax' in selected_bad_smells:
output = output + check_task_syntax(elements['tasks']) + "\n\n"
if 'Resources Syntax' in selected_bad_smells:
output = output + check_resource_syntax(elements['resources']) + "\n\n"
if 'Similar Elements' in selected_bad_smells:
output = output + get_similar_elements(elements_per_actor, similarity_threshold) + "\n\n"
if 'Spelling Mistakes' in selected_bad_smells:
output = output + check_spelling(elements) + "\n\n"
if 'Goal-Subgoal Mismatch' in selected_bad_smells:
output = output + check_entailment(decomposed_elements) + "\n\n"
if 'Contradicting Elements' in selected_bad_smells:
output = output + check_contradiction(elements_per_actor) + "\n\n"
return output
interface = gr.Interface(fn = identify_bad_smells,
inputs = [gr.File(label="TGRL File"),
gr.CheckboxGroup(["Size", "Complexity", "Punctuations", "Actors Syntax", "Goals Syntax", "Softgoals Syntax", "Tasks Syntax", "Resources Syntax", "Similar Elements", "Spelling Mistakes", "Goal-Subgoal Mismatch", "Contradicting Elements"],
label="Which bad smells you want to detect?"),
gr.Slider(label= "Size threshold", value = 5, minimum = 2, maximum = 10, step = 1),
gr.Slider(label= "Similarity threshold", value = 0.9, minimum = 0, maximum = 1, step = 0.1)],
outputs = [gr.Textbox(label= "Detected bad smells:")],
title = "TGRL Bad Smells Detection",
description = "Upload your .xgrl file and we will find the bad smells for you!",
theme = gr.themes.Soft())
interface.launch(inline = False) |