Skip to content Skip to sidebar Skip to footer

Python Finding Average Fuzzy Ratio Of All Lines

I am trying a code to find the average fuzzyratio of all the lines in the file with the word good: from fuzzywuzzy import fuzz from collections import defaultdict with open(qwer.tx

Solution 1:

For a memory-efficient solution, I would probably do something like that.

defgood_ratio(a):
    return fuzz.ratio(a, 'good')

withopen('qwer.txt', 'r') as my_file:
   my_sum, my_len = reduce(lambda a, b: (a[0]+b[0], a[1]+b[1]), ((good_ratio(i), 1) for i in my_file))
   print(my_sum/my_len)

Please note that this is a float division in python3 and you will need something like my_sum/(float)my_len in python2.

Also, you might want to filter the lines with 'good' (or maybe 'good ' or something else), for instance with ((good_ratio(i), 1) for i in my_file if 'good' in i).

Solution 2:

I think you're shooting for something like this:

 div = 0
 ratio_total = 0
 for line in my_file:
     for word in line:
           ratio = fuzz.ratio(word, 'good') # might want to use partial_ratio insteadif ratio > 0: # or some threshold:
                ratio_total += ratio
                div += 1

  print("Average ratio: %s" % (ratio_total/div,))

I think what you want to do is go through each word in a line and compare each word to "good". This should get you close?

Post a Comment for "Python Finding Average Fuzzy Ratio Of All Lines"