-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmostCommonWords.coffee
More file actions
91 lines (70 loc) · 2.13 KB
/
mostCommonWords.coffee
File metadata and controls
91 lines (70 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# INSTRUCTIONS:
#
# Install Coffeescript:
# npm install -g coffee-script
#
# Run from the terminal:
# coffee mostCommonWords.coffee ./file.txt
fs = require 'fs'
getWords = (string) ->
# Finds words and turns them into lower case
regex = /([A-Za-z\u00C4\u00E4\u00D6\u00F6]+)/gi
string = string.replace(regex, toLowerCase)
words = string.match(regex)
sortedWords = []
countedWords = []
# TODO Change countedWords to an object for faster iterating!
# Iterates all the words
for i of words
word = words[i]
index = words.indexOf(word)
indices = []
# Counts the word occurence according to the indices
while index != -1
indices.push index
index = words.indexOf(word, index + 1)
unless word in countedWords
# Discounts words that have already been counted
sortedWords.push [indices.length, word]
countedWords.push word
# Sorts words acording to the number of occurence
sortedWords.sort(sortWords).reverse()
#console.log sortedWords
# Let's take only the 5 most common words
commonWords = sortedWords.slice(0,5)
mostCommon(commonWords)
mostCommon = (sortedWords) ->
# Let's then list the words in a pretty format in the console
for i of sortedWords
count = sortedWords.indexOf(sortedWords[i]) + 1
word = sortedWords[i][1]
amount = sortedWords[i][0]
console.log count + ". The word '" + word + "' occurs " + amount + ' times'
toLowerCase = (string) ->
# Turns all words to lower case
return string.toLowerCase()
sortWords = (a, b) ->
# Properly sort the word amounts
if a < b
return -1
else if a > b
return +1
else
return 0
# Takes an argument and tests the path spelling just in case
if process.argv[2]?
# Recognizes Windows and Linux file paths
regex = /^(\.\/|\.\.\/|\/|\.?\\?\\|[A-Z]:\\)(\.?\w+(\/|\\))*(.+\.\w+)$/g
par = process.argv[2]
if par.match(regex)
filePath = par
# Opens a file and turns it into a stirng
fs.readFile filePath, (err, data) ->
if err then throw err
data = data.toString()
getWords data
else
console.log "Check the spelling of the file path."
else
console.log "The file path is missing!\n\nType:"
console.log "$ coffee mostCommonWords.coffee ./file.txt"