Advertisement
aribahaz

Untitled

Jan 22nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. # Submitter: hazaria(Hazari, Areba)
  2. # Partner : hwasim(Wasim, Hibah)
  3. # We certify that we worked cooperatively on this programming
  4. # assignment, according to the rules for pair programming
  5.  
  6. import prompt
  7. from goody import safe_open,irange
  8. from collections import defaultdict # Use defaultdict for prefix and query
  9.  
  10.  
  11. def all_prefixes(fq : (str,)) -> {(str,)}:
  12. #Has a tuple of str (words) parameter
  13. #It returns a set of tuple of str: all the prefixes of the full query argument.
  14. #For example, all_prefixes(('a', 'b', 'c')) returns {('a',), ('a', 'b'), ('a', 'b', 'c')}.
  15.  
  16. x = {fq[0:c:1] for c in irange(1, len(fq))}
  17. return x
  18.  
  19.  
  20. def add_query(prefix : {(str,):{(str,)}}, query : {(str,):int}, new_query : (str,)) -> None:
  21. #Has a prefix dictionary, query dictionary, and new full query (tuple of str) as parameters
  22. #It returns None but updates these two dictionaries based on the new full query.
  23. #It adds the new full query's prefixes to the prefix dictionary (each associated with the new full query)
  24. #and increments the integer value associated with that full query in the query dictionary
  25. #(or, if the full query is not in the dictionary, associates that full query with 1)
  26.  
  27. prefix_set = all_prefixes(new_query)
  28. for pref in prefix_set:
  29. prefix[pref].add(new_query)
  30. query[new_query] += 1
  31. #if pref not in prefix[pref]:
  32. # prefix[pref].add(pref)
  33.  
  34.  
  35. def read_queries(open_file : open) -> ({(str,):{(str,)}}, {(str,):int}):
  36. #Has an open (file) parameter
  37. #It returns a 2-tuple containing the prefix and query dictionaries (in that order) built
  38. #by reading and processing each full query in this file.
  39.  
  40. prefix_dict = defaultdict(set)
  41. query_dict = defaultdict(int)
  42. for line in open_file:
  43. line = line.rstrip()
  44. my_split = line.split(' ')
  45. full_query = tuple(my_split)
  46. add_query(prefix_dict, query_dict, full_query)
  47.  
  48. return (prefix_dict, query_dict)
  49.  
  50.  
  51. def dict_as_str(d : {None:None}, key : callable=None, reverse : bool=False) -> str:
  52. #Has a dictionary, key function (default None) and bool (default False) as parameters
  53. #It returns a multi-line string (each line is ended by '\n'),
  54. #which when printed shows the contents of the dictionary in the appropriate textual form.
  55. #The key function determines the ordering and the bool determines whether to reverse it:
  56. #like the key and reverse parameters used for the sort/sorted functions in Python.
  57. #This function is used to print both the prefix and query dictionaries.
  58.  
  59. output = ''
  60. for keys in sorted(sorted(d.keys()), key = key, reverse = reverse):
  61. output += ' '+ str(keys) + ' -> ' + str(d[keys]) + '\n'
  62.  
  63. return output
  64.  
  65.  
  66. def top_n(a_prefix : (str,), n : int, prefix : {(str,):{(str,)}}, query : {(str,):int}) -> [(str,)]:
  67. #Has a prefix (tuple of str), int, prefix dictionary, and query dictionary as parameters
  68. #It returns a list of full queries (tuple of str) whose length is the integer parameter,
  69. #containing the most frequent full queries with that prefix
  70. #If the number of full queries with that prefix is less than that integer parameter, return all the full queries.
  71. #If no full queries have this prefix, return the empty list.
  72.  
  73. return_match = []
  74.  
  75. if a_prefix in prefix.keys():
  76. pre_val = prefix[a_prefix]
  77. return_match = (sorted(pre_val, key = lambda x: query[x], reverse = True))
  78. else:
  79. return []
  80. return return_match[:n]
  81.  
  82.  
  83. # Script
  84.  
  85. if __name__ == '__main__':
  86. # Write script here
  87. file = safe_open('Type some file name storing the full query', 'r', 'Illegal file name')
  88. pre_dict, que_dict = read_queries(file)
  89. print('Prefix dictionary:')
  90. print(dict_as_str(pre_dict, key = lambda x: len(x)))
  91. print()
  92. print('Query dictionary:')
  93. print(dict_as_str(que_dict, key = lambda x: que_dict[x], reverse = True))
  94. print()
  95. while True:
  96. pref_seq = input('Type some prefix sequence (or just quit): ')
  97. if pref_seq == 'quit':
  98. break
  99.  
  100. best_match = top_n(tuple(pref_seq.split(' ')), 3, pre_dict, que_dict)
  101. print(' Up to 3 (best) matching full queries = ', best_match, '\n' )
  102.  
  103.  
  104. full_query = input('Type some full query sequence (or just quit): ')
  105. if full_query == 'quit':
  106. break
  107.  
  108. add_query(pre_dict, que_dict, tuple(full_query.split(' ')))
  109.  
  110. print('Prefix dictionary:')
  111. print(dict_as_str(pre_dict, key = lambda x: len(x)))
  112. print()
  113. print('Query dictionary:')
  114. print(dict_as_str(que_dict, key = lambda x: que_dict[x], reverse = True))
  115.  
  116. # For running batch self-tests
  117. print()
  118. import driver
  119. driver.default_file_name = "bsc5.txt"
  120. # driver.default_show_traceback = True
  121. # driver.default_show_exception = True
  122. # driver.default_show_exception_message = True
  123. driver.driver()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement