week6/questions/questions.py
def compute_idfs(documents):
    """
    Given a dictionary of `documents` that maps names of documents to a list
    of words, return a dictionary that maps words to their IDF values.

    Any word that appears in at least one of the documents should be in the
    resulting dictionary.
    """
    idfs = dict()  # Initialize a dictionary to keep count of how many times each word appeared

    # Populate dictionary with the number of occurrences
    for document in documents.keys():
        for word in documents[document]:
            if word in idfs.keys():
                idfs[word] += 1
            else:
                idfs[word] = 1

    # Calculate IDF for each of thos words
    num_documents = len(documents.keys())
    for word in idfs.keys():
        frequency = idfs[word]
        idf = math.log(num_documents/frequency)
        idfs[word] = idf

    return idfs

    Given a dictionary of `documents` that maps names of documents to a list
    of words, return a dictionary that maps words to their IDF values.

    Any word that appears in at least one of the documents should be in the
    resulting dictionary.
    
Given a dictionary of `documents` that maps names of documents to a list
of words, return a dictionary that maps words to their IDF values.

Any word that appears in at least one of the documents should be in the
resulting dictionary.
Given a dictionary of ` documents ` that maps names of documents to a list of words return a dictionary that maps words to their IDF values . Any word that appears in at least one of the documents should be in the resulting dictionary .
def compute_idfs(documents):
    idfs = dict()  
    for document in documents.keys():
        for word in documents[document]:
            if word in idfs.keys():
                idfs[word] += 1
            else:
                idfs[word] = 1
    num_documents = len(documents.keys())
    for word in idfs.keys():
        frequency = idfs[word]
        idf = math.log(num_documents/frequency)
        idfs[word] = idf
    return idfs
def compute_idfs ( documents ) : idfs = dict ( ) for document in documents . keys ( ) : for word in documents [ document ] : if word in idfs . keys ( ) : idfs [ word ] += 1 else : idfs [ word ] = 1 num_documents = len ( documents . keys ( ) ) for word in idfs . keys ( ) : frequency = idfs [ word ] idf = math . log ( num_documents / frequency ) idfs [ word ] = idf return idfs
Given a dictionary of `documents` that maps names of documents to a list
of words, return a dictionary that maps words to their IDF values.
Given a dictionary of ` documents ` that maps names of documents to a list of words return a dictionary that maps words to their IDF values .
"""
    Given a dictionary of `documents` that maps names of documents to a list
    of words, return a dictionary that maps words to their IDF values.

    Any word that appears in at least one of the documents should be in the
    resulting dictionary.
    """ # Initialize a dictionary to keep count of how many times each word appeared # Populate dictionary with the number of occurrences # Calculate IDF for each of thos words
documents