How to use query_dictionary method in pyresttest

Best Python code snippet using pyresttest_python

main.py

Source:main.py Github

copy

Full Screen

1# order of functions2# 1. collectDocuments(path) -> Collect the documents to be indexed.3# 1.01 tokenization(document) -> Tokenize the text.4# 1.1 Index the documents that each term occurs in5# 2. processingQuery(query) -> taking query input6# 2.02 postfixQuery(query_stream) -> converting infix boolean query to postfix7# 2.03 postfixEvaluation()8# 2.03.1 intersection and union of postings lists9import os10import glob11import re12from contractions import contractions13from porterstemmer import PorterStemmer14from stack import Stack15sizes_of_stemmed_lists = []16term_document_dictionary = {}17list_of_documentID = []18query_dictionary = {}19#collecting documents20def collectDocuments(path):21 # opening files one by one in the directory22 for filename in glob.glob(os.path.join(path, '*.txt')):23 with open(os.path.join(os.getcwd(), filename), 'r') as f:24 #handling the encoding of the text files25 f = open(filename, "r", encoding="utf8")26 #Reading from file27 file_contents = f.read()28 f.close()29 # sending the document for tokenization30 final_tokenized_terms = []31 final_tokenized_terms = tokenization(file_contents)32 #extracting documentid from the path33 path_split = os.path.split(filename)34 document_id = path_split[1][:-4]35 integer_document_id = int(document_id)36 #saving document id37 list_of_documentID.append(integer_document_id)38 #maintaining dictionary with posting list39 for word in final_tokenized_terms:40 if word in term_document_dictionary.keys():41 #if key exists then append the document list42 term_document_dictionary[word].append(integer_document_id)43 #sorting the postings list after a new posting is entered44 term_document_dictionary[word].sort()45 else:46 #if key does not exist then create a document list for the key47 term_document_dictionary[word] = [integer_document_id]48#tokenizing49def tokenization(document):50 #CASEFOLDING51 # casefolding or lower casing the whole document52 document_to_work = document.casefold()53 #OPENING CONTRACTIONS54 # making a list for contractions processing55 list_terms = document_to_work.split()56 list_terms_to_work = list()57 for word in list_terms:58 if word in contractions:59 #using an imported dictionary for contractions60 list_terms_to_work.append(contractions[word])61 else:62 list_terms_to_work.append(word)63 #REMOVING PUNCTUATIONS64 document_to_work = ' '.join(list_terms_to_work)65 document_to_work = re.sub(r'[^\w\s]', '', document_to_work)66 #REMOVING FINAL LEFT OVER WHITESPACES67 finalised_terms_with_stopwords = document_to_work.split()68 # REMOVING STOP WORDS AND DUPLICATE WORDS69 # opening stopwords file70 f = open('dataset/Stopword-List.txt', 'r', encoding='utf8')71 stop_words = f.read()72 stop_list = stop_words.split()73 f.close()74 #removing stop words75 finalised_terms_without_stopwords = list(set(finalised_terms_with_stopwords)^set(stop_list))76 finalised_terms_without_stopwords = list(finalised_terms_without_stopwords)77 #STEMMING78 # stemmer = PorterStemmer()79 # stem_list = list()80 # stem_list = [stemmer.stem(word) for word in finalised_terms_without_stopwords]81 # #removing stemmed duplicates82 # stem_list = set(stem_list)83 # stem_list = list(stem_list)84 #SORTING THE TERMS85 # stem_list.sort()86 # sizes_of_stemmed_lists.append(len(stem_list))87 return finalised_terms_without_stopwords88#Query processing89def processingQuery(query):90 answer = []91 postfixed_query = list()92 #LOWER CASE HANDLING AND LISTING TERMS IN A QUERY93 query_stream = query.lower().split()94 # print(query_stream)95 #not a proximity query96 if len(query_stream) == 1:97 if query_stream[0] in term_document_dictionary.keys():98 answer = term_document_dictionary[query_stream[0]]99 return answer100 elif len(query_stream) > 1:101 if "/" not in query:102 #postfixing an infix boolean query103 postfixed_query = postfixQuery(query_stream)104 # print(postfixed_query)105 answer = evaluatePostfixQuery(postfixed_query)106 else:107 #handling proximity query108 if "and" not in query_stream:109 query=" + ".join(query_stream)110 # print(query)111 query_stream_positional=query.split()112 proximity = query_stream_positional.pop()113 query_stream_positional.pop()114 # print(query_stream_positional)115 proximity = proximity.replace("/","")116 proximity_len = int(proximity) + 1117 answer = evaluatePostfixQuery(query_stream_positional)118 query_stream.pop()119 prox_answer = []120 for document in answer:121 #checking proximity122 path = 'dataset/ShortStories/' + str(document)+'.txt'123 f = open(path, "r", encoding="utf8")124 # Reading from file125 file_contents = f.read()126 f.close()127 #fixing punctuations128 file_contents = re.sub(r'[^\w\s]', '', file_contents)129 #lower casing130 terms = file_contents.split()131 terms = [i.lower() for i in terms]132 # 2 term proximity query133 if len(query_stream) == 2:134 index_1 = []135 index_2 = []136 count_1 = int(0)137 for item in terms:138 if item == query_stream[0]:139 index_1.append(count_1)140 if item == query_stream[1]:141 index_2.append(count_1)142 count_1 += 1143 for item in index_1:144 i = int(1)145 while i != proximity_len + 1:146 if item + i in index_2:147 prox_answer.append(document)148 elif item - i in index_2:149 prox_answer.append(document)150 i += 1151 # 3 term proximity query152 else:153 index_1 = []154 index_2 = []155 index_3 = []156 count_1 = int(0)157 for item in terms:158 if item == query_stream[0]:159 index_1.append(count_1)160 if item == query_stream[1]:161 index_2.append(count_1)162 if item == query_stream[2]:163 index_3.append(count_1)164 count_1 += 1165 for item in index_1:166 i = int(0)167 while i != proximity_len + 1:168 if item + i in index_2:169 prox_answer.append(document)170 elif item - i in index_2:171 prox_answer.append(document)172 i += 1173 answer = prox_answer174 return answer175#converting logical infix to postfix176def postfixQuery(query_stream):177 temp_stream = []178 for i in query_stream:179 if i == "not":180 temp_stream.append(i.replace("not", "-"))181 elif i == "and":182 temp_stream.append(i.replace("and", "*"))183 elif i == "or":184 temp_stream.append(i.replace("or", "+"))185 else:186 temp_stream.append(i)187 query_stream = temp_stream188 precedence = {"-": 3, "*": 2, "+": 1}189 postfix_query = []190 stack = Stack()191 for term_token in query_stream:192 if term_token.isidentifier():193 postfix_query.append(term_token)194 elif term_token == '(':195 stack.push(term_token)196 elif term_token == ')':197 topToken = stack.pop()198 while topToken != '(':199 postfix_query.append(topToken)200 topToken = stack.pop()201 else: # must be operator202 if not stack.empty():203 while not stack.empty() and precedence[stack.peek()] >= precedence[term_token]:204 postfix_query.append(stack.pop())205 stack.push(term_token)206 while not stack.empty():207 postfix_query.append(stack.pop())208 return postfix_query209#evaluating the postfix expression210def evaluatePostfixQuery(postfixed_query):211 stack = Stack()212 operators = ["*","+","-"]213 for token in postfixed_query:214 if token not in operators:215 stack.push(token)216 else:217 if token == "-":218 term_1 = stack.pop()219 # print(term_1)220 if term_1 in term_document_dictionary.keys():221 not_term_1 = list(set(list_of_documentID)^set(term_document_dictionary[term_1]))222 query_dictionary[term_1] = not_term_1223 else:224 query_dictionary[term_1] = []225 stack.push(term_1)226 elif token == "*":227 term_2 = stack.pop()228 term_1 = stack.pop()229 # print(term_1, term_2)230 if term_1 not in query_dictionary.keys():231 if term_1 in term_document_dictionary.keys():232 query_dictionary[term_1] = term_document_dictionary[term_1]233 else:234 query_dictionary[term_1] = []235 if term_2 not in query_dictionary.keys():236 if term_2 in term_document_dictionary.keys():237 query_dictionary[term_2] = term_document_dictionary[term_2]238 else:239 query_dictionary[term_2] = []240 query_dictionary["answer"] = intersect(term_1,term_2)241 stack.push("answer")242 elif token == "+":243 term_2 = stack.pop()244 term_1 = stack.pop()245 # print(term_1, term_2)246 if term_1 not in query_dictionary.keys():247 if term_1 in term_document_dictionary.keys():248 query_dictionary[term_1] = term_document_dictionary[term_1]249 else:250 query_dictionary[term_1] = []251 if term_2 not in query_dictionary.keys():252 if term_2 in term_document_dictionary.keys():253 query_dictionary[term_2] = term_document_dictionary[term_2]254 else:255 query_dictionary[term_2] = []256 query_dictionary["answer"] = union(term_1, term_2)257 stack.push("answer")258 return query_dictionary["answer"]259#intersection of two postings lists260def intersect(term_1,term_2):261 answer = []262 postings_term_1 = query_dictionary[term_1]263 postings_term_2 = query_dictionary[term_2]264 len_p1 = len(postings_term_1)265 len_p2 = len(postings_term_2)266 index_p1 = int(0)267 index_p2 = int(0)268 while len_p1 != 0 and len_p2 != 0:269 if index_p1 <len(postings_term_1) and index_p2<len(postings_term_2):270 if postings_term_1[index_p1] == postings_term_2[index_p2]:271 answer.append(postings_term_1[index_p1])272 index_p1 += 1273 len_p1 -= 1274 index_p2 += 1275 len_p2 -= 1276 else:277 if postings_term_1[index_p1] < postings_term_2[index_p2]:278 index_p1 += 1279 len_p1 -= 1280 else:281 index_p2 += 1282 len_p2 -= 1283 return answer284#union of two postings lists285def union(term_1,term_2):286 answer = []287 postings_term_1 = query_dictionary[term_1]288 postings_term_2 = query_dictionary[term_2]289 answer = [term for term in postings_term_1]290 answer = [term for term in postings_term_2] + answer291 answer = set(answer)292 answer = list(answer)293 answer.sort()294 return answer295if __name__ == '__main__':296 collectDocuments('dataset/ShortStories')297 print("Size of the dictionary: "+ str(len(term_document_dictionary)))298 q = "god and man and love"299 print("Query: ",q)...

Full Screen

Full Screen

create_queries.py

Source:create_queries.py Github

copy

Full Screen

1'''2 Authors:3 Valentina Guerrero4 Aishwarya Varma 5'''6def create_movie_table_query(items_dictionary):7 query_dictionary = {}8 language_string = ''9 numbers = ['rating', 'revenue', 'runtime', 'release_year', 'budget']10 for item in items_dictionary:11 if items_dictionary[item]:12 if item not in numbers:13 query_dictionary[item] = "LIKE '%{}%'".format(items_dictionary[item])14 else:15 if item in ['rating', 'revenue', 'budget']:16 query_dictionary[item] = ">= '{}' AND movies.{} > 0".format(items_dictionary[item], item)17 elif item == 'runtime':18 query_dictionary[item] = "<= '{}' AND movies.runtime > 0 ".format(items_dictionary[item])19 else:20 query_dictionary[item] = "= '{}'".format(items_dictionary[item])21 else:22 query_dictionary[item] = 'IS NOT NULL'23 query_skeleton = '''24 movies.title {}25 AND26 movies.rating {}27 AND28 movies.revenue {}29 AND 30 movies.runtime {}31 AND32 movies.release_year {}33 AND 34 movies.budget {}35 AND36 languages.lang_full {}37 AND 38 countries.country_name {}39 AND40 companies.company_name {}41 AND42 genres.genre {}43 AND44 movie_langs.movie_id = movies.id45 AND 46 movie_langs.lang_id = languages.id47 AND48 movie_countries.movie_id = movies.id49 AND50 movie_countries.country_id = countries.id51 AND52 movie_companies.movie_id = movies.id53 AND54 movie_companies.company_id = companies.id55 AND56 movies.id = movie_genres.movie_id57 AND58 movie_genres.genre_id = genres.id59 60 '''.format(query_dictionary['title'], query_dictionary['rating'], query_dictionary['revenue'], query_dictionary['runtime'], query_dictionary['release_year'], query_dictionary['budget'],query_dictionary['language'], query_dictionary['country'], query_dictionary['company'], query_dictionary['genre'] )61 return query_skeleton62def create_genres_table_query(movie_id):63 query = '''64 SELECT genres.genre65 FROM 66 movies,67 genres,68 movie_genres69 WHERE70 movies.id = '{}'71 72 ;'''.format(movie_id)73 return query74def create_search_all_query(search_string):75 if search_string.isnumeric():76 int_keyword = "= '{}'".format(search_string)77 string_keyword = "LIKE '%{}%'".format(search_string)78 query_skeleton = '''79 release_year {int_key}80 OR81 rating {int_key}82 OR83 runtime {int_key}84 OR85 title {string_key}86 '''.format(int_key= int_keyword, string_key = string_keyword)87 else:88 string_keyword = "LIKE '%{}%'".format(search_string)89 query_skeleton = '''90 title {string_key}91 '''.format(string_key = string_keyword)92 return query_skeleton93def create_genre_query(keyword):94 genre_query = ''' 95 SELECT 96 movies.id, movies.title, movies.release_year, movies.rating, movies.poster_path97 FROM 98 movies,99 genres,100 movie_genres101 WHERE102 genres.genre LIKE '%{}%'103 AND104 movies.id = movie_genres.movie_id105 AND106 movie_genres.genre_id = genres.id107 '''.format(keyword)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pyresttest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful