Skip to content

Commit a814d64

Browse files
author
Judit Acs
committed
Merge branch 'master' of github.com:juditacs/wordcount
2 parents 031f205 + 01c60bd commit a814d64

File tree

7 files changed

+96
-54
lines changed

7 files changed

+96
-54
lines changed

javascript/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "wordcount",
33
"version": "1.0.0",
4+
"readme": "../README.md",
45
"description": "Javascript implementation of wordcount for https://github.com/juditacs/wordcount",
56
"main": "wordcount.js",
67
"repository": "juditacs/wordcount",
@@ -10,6 +11,6 @@
1011
"author": "Laci",
1112
"license": "MIT",
1213
"dependencies": {
13-
"lodash": "^3.10.1"
14+
"timsort": "^0.2.1"
1415
}
1516
}

javascript/wordcount.js

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,58 @@
1-
var _ = require('lodash');
2-
var Console = require('console').Console;
1+
"use strict";
2+
var readline = require('readline');
3+
var TimSort = require('timsort');
34

4-
var data = {};
5+
process.stdin.setEncoding('utf8');
56

6-
var log = new Console(process.stderr, process.stderr);
7+
let rl = readline.createInterface({ input: process.stdin, terminal: false });
8+
let wordCounts = {};
9+
const regExp = /[ \t\n\r]+/g;
710

8-
process.stdin.setEncoding('utf8');
9-
process.stdin.on('readable', function() {
10-
var chunk = process.stdin.read();
11-
if (chunk !== null) {
12-
_.forEach(chunk.split(/[\n\t\r\f ]+/g), function(word) {
13-
if (word == '') {return;}
14-
if (data[word]) {
15-
//console.log('Adding to ' + word + '.');
16-
data[word].count++;
17-
} else {
18-
//console.log('Inserting ' + word + '.');
19-
data[word] = {word: word, count: 1};
20-
}
21-
});
11+
rl.on('line', (line) => {
12+
let words = line.trim().split(regExp);
13+
14+
for(let i = 0, len = words.length; i < len; i++) {
15+
let word = words[i];
16+
17+
if (!word)
18+
return;
19+
20+
if(wordCounts.hasOwnProperty(word)) {
21+
wordCounts[word]++;
22+
}
23+
else {
24+
wordCounts[word] = 1;
25+
}
2226
}
23-
});
27+
}).on('close', () => {
28+
let wordList = Object.keys(wordCounts);
29+
30+
let compare = (x, y) => {
31+
if(wordCounts[x] < wordCounts[y])
32+
return 1;
2433

25-
process.stdin.on('end', function() {
26-
var sorted = _.sortByOrder(data, ['count', 'word'], ['desc', 'asc']);
27-
//console.log('Output has '+sorted.length+' items:');
28-
_.forEach(sorted, function(item){
29-
process.stdout.write(item.word + '\t' + item.count + '\n');
30-
});
34+
if(wordCounts[x] === wordCounts[y] && x > y)
35+
return 1;
36+
37+
return -1;
38+
}
39+
40+
TimSort.sort(wordList, compare);
41+
42+
let word = '';
43+
let count = '';
44+
let buff = '';
45+
const maxBuff = 1000;
46+
for(let i = 0, len = wordList.length; i < len; i++) {
47+
word = wordList[i];
48+
count = wordCounts[word];
49+
50+
buff += word + '\t' + count + '\n';
51+
if (i % maxBuff === 0) {
52+
process.stdout.write(buff);
53+
buff = '';
54+
}
55+
}
56+
process.stdout.write(buff);
57+
process.exit(0);
3158
});

javascript/wordcount2.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ const regExp = /[ \t\n\r]+/g;
99

1010
rl.on('line', (line) => {
1111
var words = line.trim().split(regExp);
12-
12+
1313
for(var i = 0, len = words.length; i < len; i++) {
1414
var word = words[i];
15-
15+
1616
if (!word)
1717
return;
18-
18+
1919
if(wordCounts.hasOwnProperty(word)) {
2020
wordCounts[word]++;
2121
}
@@ -25,23 +25,23 @@ rl.on('line', (line) => {
2525
}
2626
}).on('close', () => {
2727
var wordList = Object.keys(wordCounts);
28-
28+
2929
wordList.sort((x, y) => {
3030
if(wordCounts[x] < wordCounts[y])
3131
return 1;
32-
32+
3333
if(wordCounts[x] === wordCounts[y] && x > y)
3434
return 1;
35-
35+
3636
return -1;
3737
});
38-
38+
3939
for(var i = 0, len = wordList.length; i < len; i++) {
4040
var word = wordList[i];
4141
var count = wordCounts[word];
42-
42+
4343
process.stdout.write(word + '\t' + count + '\n');
4444
}
45-
45+
4646
process.exit(0);
4747
});

julia/wordcount.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ customlt(a,b) = (b.second < a.second) ? true : b.second == a.second ? a.first <
22

33
function main()
44
wc = Dict{UTF8String,Int64}()
5-
for l in eachline(STDIN)
6-
for w in split(l)
5+
for l::UTF8String in eachline(STDIN)
6+
for w::UTF8String in split(l)
77
wc[w]=get(wc, w, 0) + 1
88
end
99
end

perl/wordcount.pl

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
#!/usr/bin/env perl
22

3+
my %words;
4+
while(<>) { $words{$_}++ for split }
35

4-
sub word_count {
5-
my %counter;
6-
my $line = <STDIN>;
7-
while($line ne "") {
8-
my @words = split(' ', $line);
9-
for my $word (@words) {
10-
$counter{$word} ++;
11-
}
12-
$line = <STDIN>;
13-
}
14-
15-
foreach my $word (sort { -$counter{$a} <=> -$counter{$b} or $a cmp $b } keys %counter) {
16-
printf "%s\t%s\n", $word, $counter{$word};
17-
}
18-
}
19-
20-
word_count();
6+
print "$_\t$words{$_}\n"
7+
for sort {$words{$b}<=>$words{$a} or $a cmp $b} keys %words;

python/wordcount_py2gabor.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python2
2+
from sys import stdin
3+
4+
def counter():
5+
keyWords = {}
6+
for l in stdin:
7+
for word in l.split():
8+
if word in keyWords:
9+
keyWords[word] += 1
10+
else:
11+
keyWords[word] = 1
12+
13+
keyCounts = {}
14+
for word in keyWords.keys():
15+
count = keyWords[word]
16+
if count in keyCounts:
17+
keyCounts[count].append(word)
18+
else:
19+
keyCounts[count] = [word]
20+
21+
for count in sorted(keyCounts, reverse = True):
22+
for word in sorted(keyCounts[count]):
23+
print word+'\t'+str(count)
24+
25+
if __name__ == '__main__':
26+
counter()

run_commands.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ nodejs javascript/wordcount.js
1010
nodejs javascript/wordcount2.js
1111
perl/wordcount.pl
1212
python/wordcount_py2.py
13+
python/wordcount_py2gabor.py
1314
python/wordcount_py3.py
1415
php php/wordcount.php
1516
go/bin/wordcount

0 commit comments

Comments
 (0)