Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xlrd
- import xlwt
- def iterative_levenshtein(s, t):
- if len(t) == 0:
- return (len(s) - len(t))
- """
- iterative_levenshtein(s, t) -> ldist
- ldist is the Levenshtein distance between the strings
- s and t.
- For all i and j, dist[i,j] will contain the Levenshtein
- distance between the first i characters of s and the
- first j characters of t
- """
- rows = len(s)+1
- cols = len(t)+1
- dist = [[0 for x in range(cols)] for x in range(rows)]
- # source prefixes can be transformed into empty strings
- # by deletions:
- for i in range(1, rows):
- dist[i][0] = i
- # target prefixes can be created from an empty source string
- # by inserting the characters
- for i in range(1, cols):
- dist[0][i] = i
- for col in range(1, cols):
- for row in range(1, rows):
- if s[row-1] == t[col-1]:
- cost = 0
- else:
- cost = 1
- dist[row][col] = min(dist[row-1][col] + 1, # deletion
- dist[row][col-1] + 1, # insertion
- dist[row-1][col-1] + cost) # substitution
- return (dist[row][col])
- def compare(a,b):
- return (iterative_levenshtein(a,b)) #replace with levenstein distace or something like that here
- #wb = xlrd.open_workbook("Psychology Test Results Real.xlsx")
- wb = xlrd.open_workbook("Psychology Test Results responses combined stripped.xlsx")
- wbs = wb.sheet_by_index(0)
- word_length_scores_list = []
- for i in range(wbs.nrows):
- cell_item = wbs.cell(i,2).value
- print (cell_item)
- print (type(cell_item))
- if len(str(cell_item)) > 100: #cheap verifier
- name = ""
- practice = ""
- pr_into_array = []
- j = 0
- while j < len(cell_item):
- len_next = int(cell_item[j])
- j += 1
- len_next_next = int(cell_item[j:j + len_next])
- j += len_next
- next_subitem = cell_item[j:j + len_next_next]
- pr_into_array.append(next_subitem)
- j += len_next_next
- print(pr_into_array)
- word_length_scores = [0 for i in range(8)]
- name = pr_into_array.pop(0)
- for j in range(25):
- pr_into_array.pop(0) #removing practice
- print (pr_into_array)
- while len(pr_into_array) > 0:
- str_length = len(pr_into_array[1])
- score = 0
- for k in range(int(int(pr_into_array.pop(0)) / 2)):
- compare_a = pr_into_array.pop(0)
- compare_b = pr_into_array.pop(0)
- score_add = str_length - compare(compare_a,compare_b)
- score += score_add
- print (compare_a, compare_b,score_add,str_length,compare(compare_a,compare_b))
- word_length_scores[str_length - 1] = score
- word_length_scores_list.append(word_length_scores)
- else:
- print ("skipped")
- print (word_length_scores_list)
- write_workbook = xlwt.Workbook()
- write_sheet = write_workbook.add_sheet("results")
- column_start = 2
- row_start = 2
- total_position = [4,2]
- write_sheet.write(total_position[0],total_position[1],len(word_length_scores_list))
- chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- word_length_scores_totals = [0 for i in range(8)]
- for i in range(8):
- word_length_scores_totals[i] += sum([j[i] for j in word_length_scores_list])
- write_sheet.write(row_start, column_start + i, word_length_scores_totals[i])
- write_sheet.write(row_start + 7, column_start + i, xlwt.Formula(chars[column_start + i] + str(row_start + 1) + "/" + "C5"))
- print(word_length_scores_totals)
- write_workbook.save("results.xls") #has to be xls
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement