applicationcounting word frequencies as case study for using mapconsider the problem of counting the number of occurrences of words in document this is standard task when performing statistical analysis of documentfor examplewhen categorizing an email or news article map is an ideal data structure to use herefor we can use words as keys and word counts as values we show such an application in code fragment we break apart the original document using combination of file and string methods that results in loop over lowercased version of all whitespace separated pieces of the document we omit all nonalphabetic characters so that parenthesesapostrophesand other such punctuation are not considered part of word in terms of map operationswe begin with an empty python dictionary named freq during the first phase of the algorithmwe execute the command freq[word freq get(word for each word occurrence we use the get method on the right-hand side because the current word might not exist in the dictionarythe default value of is appropriate in that case during the second phase of the algorithmafter the full document has been processedwe examine the contents of the frequency maplooping over freq itemsto determine which word has the most occurrences freq for piece in open(filenamereadlowersplit)only consider alphabetic characters within this piece word join( for in piece if isalpha)if wordrequire at least one alphabetic character freq[word freq get(word max word max count for ( ,cin freq items)(keyvaluetuples represent (wordcountif max countmax word max count printthe most frequent word is max wordprintits number of occurrences is max countcode fragment program for counting word frequencies in documentand reporting the most frequent word we use python' dict class for the map we convert the input to lowercase and ignore any nonalphabetic characters