10

Trying to write a code that will compare multiple files and return the highest fuzzratio between multiple options.

Problem is I'm getting an error message:

WARNING:root:Applied processor reduces input query to empty string, all comparisons will have score 0. [Query: '/'] WARNING:root:Applied processor reduces input query to empty string, all comparisons will have score 0. [Query: '.']

And the exported file is essentially blank. Any clue why this is happening?

from fuzzywuzzy import fuzz, process
import csv

def readfile( filen ):
    with open(filen,'r') as f:
        contents = f.readlines()
    return contents

def write_fuzzy( fileo, file1, file2 ):
    matches=[]
    for  item1 in file1:
        matches.append(process.extract( str(item1), file2, limit=2 )[0][0])
    with open( fileo, 'w' ) as f:
        w = csv.writer( f, delimiter = ',' )
        w.writerows( matches )

filenames = ['Documents/test_CSV_1.csv',\
             'Documents/test_CSV_2.csv']

 file_contents = []
 for filen in filenames: 
    file_contents.append( readfile( filen ) )

write_fuzzy( 'out.csv', filenames[0], filenames[1] )
Hofbr
  • 868
  • 9
  • 31

3 Answers3

10

Sorry for the late answer. I am facing a similar issue and found your question.

The problem is not really one it's just a warning and wont actually result in any differences in your matches.

WARNING:root:Applied processor reduces input query to empty string, all comparisons will have score 0. [Query: '/']

Means that the query string for process.extract() didn't contain any common characters of a natural language: [Query: '/'].

After digging into fuzzywuzzy, i found that a string processor reduces input query to empty string, which wont match any pattern in the search text you provide.

My approach to get rid of these warnings is to validate the query string with that string processor before doing the fuzzy search.

from fuzzywuzzy import utils

invalid_query = " ... // "

if utils.full_process(invalid_query):
    # wont execute and not produce a warning
    process.extract(invalid_query, patterns)
Lars
  • 166
  • 3
  • 5
3

As mentioned above, it's just a warning and won't change any results. There is a simple workaround/solution to suppressing this warning. The library is using the logging module and not the warning module. One can include these lines in the code to suppress any such root warnings unless it is of severity level ERROR.

import logging
logging.getLogger().setLevel(logging.ERROR)

Had it not been a root warning, one could use the following snippet instead.

import warnings
warnings.filterwarnings("ignore")

To understand this better, one can also look at this python code. https://github.com/seatgeek/fuzzywuzzy/blob/master/test_fuzzywuzzy_pytest.py

Nikhil K.
  • 161
  • 9
1

The answers are really useful. However, I prefer solving the problem instead of skipping warnings.

When I got the error WARNING:root:Applied processor reduces input query to empty string, all comparisons will have score 0. [Query: ' '], It was because the value I was trying to match contained a space at the early beginning of the string. So, it would be better to clean the text before fuzzy matching instead of handling the errors.

Esraa Abdelmaksoud
  • 1,307
  • 12
  • 25