Advertisement
Alberknyis

Results Converter

Jun 11th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. import xlrd
  2. import xlwt
  3.  
  4.  
  5. def iterative_levenshtein(s, t):
  6.     if len(t) == 0:
  7.         return (len(s) - len(t))
  8.  
  9.     """
  10.         iterative_levenshtein(s, t) -> ldist
  11.         ldist is the Levenshtein distance between the strings
  12.         s and t.
  13.         For all i and j, dist[i,j] will contain the Levenshtein
  14.         distance between the first i characters of s and the
  15.         first j characters of t
  16.     """
  17.     rows = len(s)+1
  18.     cols = len(t)+1
  19.     dist = [[0 for x in range(cols)] for x in range(rows)]
  20.     # source prefixes can be transformed into empty strings
  21.     # by deletions:
  22.     for i in range(1, rows):
  23.         dist[i][0] = i
  24.     # target prefixes can be created from an empty source string
  25.     # by inserting the characters
  26.     for i in range(1, cols):
  27.         dist[0][i] = i
  28.        
  29.     for col in range(1, cols):
  30.         for row in range(1, rows):
  31.             if s[row-1] == t[col-1]:
  32.                 cost = 0
  33.             else:
  34.                 cost = 1
  35.             dist[row][col] = min(dist[row-1][col] + 1,      # deletion
  36.                                  dist[row][col-1] + 1,      # insertion
  37.                                  dist[row-1][col-1] + cost) # substitution
  38.  
  39.     return (dist[row][col])
  40.  
  41. def compare(a,b):
  42.     return (iterative_levenshtein(a,b)) #replace with levenstein distace or something like that here
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. #wb = xlrd.open_workbook("Psychology Test Results Real.xlsx")
  52. wb = xlrd.open_workbook("Psychology Test Results responses combined stripped.xlsx")
  53.  
  54.  
  55. wbs = wb.sheet_by_index(0)
  56.  
  57. word_length_scores_list = []
  58.  
  59. for i in range(wbs.nrows):
  60.     cell_item = wbs.cell(i,2).value
  61.     print (cell_item)
  62.     print (type(cell_item))
  63.     if len(str(cell_item)) > 100: #cheap verifier
  64.         name = ""
  65.         practice = ""
  66.         pr_into_array = []
  67.         j = 0
  68.         while j < len(cell_item):
  69.             len_next = int(cell_item[j])
  70.             j += 1
  71.             len_next_next = int(cell_item[j:j + len_next])
  72.             j += len_next
  73.             next_subitem = cell_item[j:j + len_next_next]
  74.             pr_into_array.append(next_subitem)
  75.             j += len_next_next
  76.         print(pr_into_array)
  77.         word_length_scores = [0 for i in range(8)]
  78.         name = pr_into_array.pop(0)
  79.         for j in range(25):
  80.             pr_into_array.pop(0) #removing practice
  81.         print (pr_into_array)
  82.         while len(pr_into_array) > 0:
  83.             str_length = len(pr_into_array[1])
  84.             score = 0
  85.             for k in range(int(int(pr_into_array.pop(0)) / 2)):
  86.                 compare_a = pr_into_array.pop(0)
  87.                 compare_b = pr_into_array.pop(0)
  88.                 score_add = str_length - compare(compare_a,compare_b)
  89.                 score += score_add
  90.                 print (compare_a, compare_b,score_add,str_length,compare(compare_a,compare_b))
  91.             word_length_scores[str_length - 1] = score
  92.         word_length_scores_list.append(word_length_scores)
  93.     else:
  94.         print ("skipped")
  95. print (word_length_scores_list)
  96.  
  97. write_workbook = xlwt.Workbook()
  98.  
  99. write_sheet = write_workbook.add_sheet("results")
  100.  
  101. column_start = 2
  102. row_start = 2
  103. total_position = [4,2]
  104.  
  105. write_sheet.write(total_position[0],total_position[1],len(word_length_scores_list))
  106.  
  107. chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  108.  
  109. word_length_scores_totals = [0 for i in range(8)]
  110. for i in range(8):
  111.     word_length_scores_totals[i] += sum([j[i] for j in word_length_scores_list])
  112.     write_sheet.write(row_start, column_start + i, word_length_scores_totals[i])
  113.     write_sheet.write(row_start + 7, column_start + i, xlwt.Formula(chars[column_start + i] + str(row_start + 1)  + "/" + "C5"))
  114.  
  115. print(word_length_scores_totals)
  116.  
  117. write_workbook.save("results.xls") #has to be xls
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement