Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Submitter: hazaria(Hazari, Areba)
- # Partner : hwasim(Wasim, Hibah)
- # We certify that we worked cooperatively on this programming
- # assignment, according to the rules for pair programming
- import prompt
- from goody import safe_open,irange
- from collections import defaultdict # Use defaultdict for prefix and query
- def all_prefixes(fq : (str,)) -> {(str,)}:
- #Has a tuple of str (words) parameter
- #It returns a set of tuple of str: all the prefixes of the full query argument.
- #For example, all_prefixes(('a', 'b', 'c')) returns {('a',), ('a', 'b'), ('a', 'b', 'c')}.
- x = {fq[0:c:1] for c in irange(1, len(fq))}
- return x
- def add_query(prefix : {(str,):{(str,)}}, query : {(str,):int}, new_query : (str,)) -> None:
- #Has a prefix dictionary, query dictionary, and new full query (tuple of str) as parameters
- #It returns None but updates these two dictionaries based on the new full query.
- #It adds the new full query's prefixes to the prefix dictionary (each associated with the new full query)
- #and increments the integer value associated with that full query in the query dictionary
- #(or, if the full query is not in the dictionary, associates that full query with 1)
- prefix_set = all_prefixes(new_query)
- for pref in prefix_set:
- prefix[pref].add(new_query)
- query[new_query] += 1
- #if pref not in prefix[pref]:
- # prefix[pref].add(pref)
- def read_queries(open_file : open) -> ({(str,):{(str,)}}, {(str,):int}):
- #Has an open (file) parameter
- #It returns a 2-tuple containing the prefix and query dictionaries (in that order) built
- #by reading and processing each full query in this file.
- prefix_dict = defaultdict(set)
- query_dict = defaultdict(int)
- for line in open_file:
- line = line.rstrip()
- my_split = line.split(' ')
- full_query = tuple(my_split)
- add_query(prefix_dict, query_dict, full_query)
- return (prefix_dict, query_dict)
- def dict_as_str(d : {None:None}, key : callable=None, reverse : bool=False) -> str:
- #Has a dictionary, key function (default None) and bool (default False) as parameters
- #It returns a multi-line string (each line is ended by '\n'),
- #which when printed shows the contents of the dictionary in the appropriate textual form.
- #The key function determines the ordering and the bool determines whether to reverse it:
- #like the key and reverse parameters used for the sort/sorted functions in Python.
- #This function is used to print both the prefix and query dictionaries.
- output = ''
- for keys in sorted(sorted(d.keys()), key = key, reverse = reverse):
- output += ' '+ str(keys) + ' -> ' + str(d[keys]) + '\n'
- return output
- def top_n(a_prefix : (str,), n : int, prefix : {(str,):{(str,)}}, query : {(str,):int}) -> [(str,)]:
- #Has a prefix (tuple of str), int, prefix dictionary, and query dictionary as parameters
- #It returns a list of full queries (tuple of str) whose length is the integer parameter,
- #containing the most frequent full queries with that prefix
- #If the number of full queries with that prefix is less than that integer parameter, return all the full queries.
- #If no full queries have this prefix, return the empty list.
- return_match = []
- if a_prefix in prefix.keys():
- pre_val = prefix[a_prefix]
- return_match = (sorted(pre_val, key = lambda x: query[x], reverse = True))
- else:
- return []
- return return_match[:n]
- # Script
- if __name__ == '__main__':
- # Write script here
- file = safe_open('Type some file name storing the full query', 'r', 'Illegal file name')
- pre_dict, que_dict = read_queries(file)
- print('Prefix dictionary:')
- print(dict_as_str(pre_dict, key = lambda x: len(x)))
- print()
- print('Query dictionary:')
- print(dict_as_str(que_dict, key = lambda x: que_dict[x], reverse = True))
- print()
- while True:
- pref_seq = input('Type some prefix sequence (or just quit): ')
- if pref_seq == 'quit':
- break
- best_match = top_n(tuple(pref_seq.split(' ')), 3, pre_dict, que_dict)
- print(' Up to 3 (best) matching full queries = ', best_match, '\n' )
- full_query = input('Type some full query sequence (or just quit): ')
- if full_query == 'quit':
- break
- add_query(pre_dict, que_dict, tuple(full_query.split(' ')))
- print('Prefix dictionary:')
- print(dict_as_str(pre_dict, key = lambda x: len(x)))
- print()
- print('Query dictionary:')
- print(dict_as_str(que_dict, key = lambda x: que_dict[x], reverse = True))
- # For running batch self-tests
- print()
- import driver
- driver.default_file_name = "bsc5.txt"
- # driver.default_show_traceback = True
- # driver.default_show_exception = True
- # driver.default_show_exception_message = True
- driver.driver()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement