import math


def calculateTermfrequency(query: str, resultIds: list, allDocsContainTermCount: int, allDocInDBCount:int, texts: dict) -> dict:
    """
    Calcualtes TF*IDF formula to show documents at the top of the result list
    in which the search query occurs the most.
    Example: 
        - Before -> ids: ['5d7bcb60f85903a2af72c4a3', '5d7bcb6bf85903a2af72c4a6']
        - After  -> {'5d7bcb6bf85903a2af72c4a6': {'wordFrequency': 5, 'textWordCount': 1440, 'tfidf': 0.003472222222222222}, 
                    ''5d7bcb60f85903a2af72c4a3': {'wordFrequency': 1, 'textWordCount': 1494, 'tfidf': 0.0006693440428380187}}
    source: https://stackoverflow.com/questions/722697/best-way-to-turn-word-list-into-frequency-dict
    tfIdf: https://www.onely.com/blog/what-is-tf-idf/

    :query: String
    :resultIds: List
    :allDocsContainTermCount: Integer
    :allDocInDBCount: Integer
    :texts: Dictionary

    :return: Dictionary
    """
    return  {
                id: {
                        'wordFrequency': resultIds.count(id), 
                        'textWordCount': len(texts[id].split()),
                        'tfidf': (resultIds.count(id) / len(texts[id].split())) * (math.log((allDocInDBCount / allDocsContainTermCount)))
                } 
                for id in set(resultIds)
            }