Created
July 2, 2014 23:50
-
-
Save ianmcnally/6e7c83a087753dbcbc3b to your computer and use it in GitHub Desktop.
A function to retrieve the most frequent word pair in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Write a function `wordPair` that for a | |
# given a string, returns the most frequent word pair (word, space, word) | |
# E.g., 'I want to be a part of it New York, New York' -> 'New York' | |
# | |
wordPair = (str) -> | |
getWordPairCounts = (str) -> | |
words = str.replace(/[^a-zA-Z\s]/g, '').split ' ' | |
pairCounts = {} | |
words.forEach (word, index) -> | |
return if index is words.length - 1 | |
pair = "#{word} #{words[index+1]}" | |
pairCounts[pair] = (pairCounts[pair] or 0) + 1 | |
pairCounts | |
maxPair = (pairCounts) -> | |
frequentPair = null | |
pairFrequency = 0 | |
for pair, frequency of pairCounts | |
if frequency > pairFrequency | |
frequentPair = pair | |
pairFrequency = frequency | |
frequentPair | |
pairCounts = getWordPairCounts str | |
maxPair pairCounts | |
#str = 'I want to be a part of it New York, New York' | |
#expect(maxPair(str)).toEqual 'New York' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment