Skip to content

Instantly share code, notes, and snippets.

@ianmcnally
Created July 2, 2014 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianmcnally/6e7c83a087753dbcbc3b to your computer and use it in GitHub Desktop.
Save ianmcnally/6e7c83a087753dbcbc3b to your computer and use it in GitHub Desktop.
A function to retrieve the most frequent word pair in a string
#
# 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