1+ import pandas as pd
2+ from pandas import DataFrame
3+ import re
4+ import random
5+ import string
6+ from parrot import Parrot
7+
8+
9+ parrot = Parrot (model_tag = "prithivida/parrot_paraphraser_on_T5" , use_gpu = False )
10+
11+
12+
13+ def modify_question (ques : str ) -> str :
14+ # Regular expressions to match variable, class, and function names
15+ variable_pattern :str = r'\b(int|double|float|char|bool)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*;'
16+ class_pattern :str = r'\bclass\s+([a-zA-Z_][a-zA-Z0-9_]*)\b'
17+ function_pattern :str = r'\b(int|double|float|char|bool)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\((.*)\)\s*{'
18+ inherit_pattern :str = r'\bclass\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*public\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*{'
19+ # new object created pattern eg :- B b;
20+ new_object_pattern :str = r'\b([a-zA-Z_][a-zA-Z0-9_]*)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*;'
21+ # Objects calling functions pattern eg :- b.func();
22+ object_function_pattern :str = r'\b([a-zA-Z_][a-zA-Z0-9_]*)\.([a-zA-Z_][a-zA-Z0-9_]*)\s*\((.*)\)\s*;'
23+
24+ function_declaration_pattern :str = r'\b(int|double|float|char|bool|void)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\((.*)\)\s*;'
25+ pattern = r"(?i)(?:what|how|when|why|which|who|whom|where|can|do|is)\s.*[?].*"
26+
27+
28+ # Function to generate random word
29+ def generate_random_word () -> str :
30+ prefix :str = random .choice (['Alpha' , 'Beta' , 'Gamma' , 'Delta' , 'Epsilon' , 'Zeta' , 'Eta' , 'Theta' ])
31+ suffix = '' .join (random .choices (string .ascii_uppercase , k = random .randint (1 , 3 )))
32+ return f'{ prefix } { suffix } '
33+
34+ def random_letter () -> str :
35+ return random .choice (string .ascii_lowercase )
36+
37+
38+ # Replace variable, class, and function names with random words
39+ # Does code have classes
40+ replaced_code = ques
41+
42+
43+ if re .search (pattern , ques ):
44+ # Replace class names with random words
45+ questions = re .findall (pattern , ques )
46+ print (questions [0 ])
47+ new_questions = parrot .augment (input_phrase = questions [0 ], use_gpu = False )
48+ # choose any random from the new_questions that is a list of tuples
49+ if new_questions :
50+ new_question = random .choice (new_questions )
51+ new_question = new_question [0 ]
52+ replaced_code = re .sub (pattern , new_question , ques )
53+
54+
55+ if re .search (class_pattern , ques ):
56+ # Replace class names with random words
57+ new_class_name = generate_random_word ()
58+ replaced_code = re .sub (class_pattern , 'class ' + new_class_name , ques )
59+
60+ # Does code have inheritance
61+ if re .search (inherit_pattern , replaced_code ):
62+ # Replace inheritance names with random words
63+ replaced_code = re .sub (inherit_pattern , 'class ' + generate_random_word () + ' : public ' + new_class_name , replaced_code )
64+ # Doe
65+ # Does code have functions
66+ if re .search (function_pattern , replaced_code ):
67+ # Replace function names with random words
68+ replaced_code = re .sub (function_pattern , 'int ' + generate_random_word () + '()' , replaced_code )
69+
70+ # Does code have function declarations
71+ if re .search (function_declaration_pattern , replaced_code ):
72+ # Replace function names with random words
73+ replaced_code = re .sub (function_declaration_pattern , 'int ' + generate_random_word () + '();' , replaced_code )
74+
75+ # Does code have variables
76+ if re .search (variable_pattern , replaced_code ):
77+ # Replace variable names with random words
78+ replaced_code = re .sub (variable_pattern , 'int ' + random_letter () + ';' , replaced_code )
79+
80+ # Does code have new objects
81+ if re .search (new_object_pattern , replaced_code ):
82+ # Replace new object names with random words with new class names
83+ object_name = random_letter ()
84+ replaced_code = re .sub (new_object_pattern , new_class_name + ' ' + object_name + ';' , replaced_code )
85+
86+ # Does code have objects calling functions
87+ if re .search (object_function_pattern , replaced_code ):
88+ # Replace object names with random words
89+ replaced_code = re .sub (object_function_pattern , object_name + '.' + generate_random_word () + '();' , replaced_code )
90+ return replaced_code
91+
92+
93+
94+ def generate_question (input_ques : DataFrame ) -> str :
95+ print ("Hello" )
96+ ques :str = ""
97+ for i in range (len (input_ques )):
98+ ques += f"Q.{ i + 1 } " + modify_question (input_ques .iloc [i ]['question' ]) + "<br/><br/>" + "A." + str (input_ques .iloc [i ]['a' ]) + "<br/>" + "B." + str (input_ques .iloc [i ]['b' ]) + "<br />" + "C." + str (input_ques .iloc [i ]['c' ]) + "<br/>" + "D." + str (input_ques .iloc [i ]['d' ]) + "<br /><br/>"
99+
100+ return ques
0 commit comments