import operator


def calculate_number_words(references, candidate):
    """ Returns the number of words from the reference closest to the length of the prediction 
        and the number of words in the candidate sentence """

    # Calculate the number of words for each reference file
    nw_ref = list()
    for ref in references:
        nw_ref.append(sum([len(line.strip('\n').split()) for line in open(ref, 'r').readlines()]))

    # Calculate the number of words in the candidate translation
    nw_can = sum([len(line.strip('\n').split()) for line in open(candidate, 'r').readlines()])

    # Return the reference length closest to the candidate
    diff_list = [x-nw_can for x in nw_ref]
    min_ix, min_v = min(enumerate(diff_list), key=operator.itemgetter(1))

    return nw_ref[min_ix], nw_can