forked from 6/stopwords-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
93 lines (83 loc) · 2.72 KB
/
Gruntfile.js
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
91
92
93
var fs = require('fs'),
glob = require('glob'),
_ = require('underscore'),
languages = require('languages');
module.exports = function(grunt) {
var pkg = grunt.file.readJSON('package.json');
var wordsInFile = function(file) {
var words = [];
var lines = file.split(/[\n\r]+/);
_.each(lines, function(line) {
line = line.trim();
if (line === "" || line[0] === "#") {
return;
}
words.push(line);
});
return words;
};
var _stopwords = null; // Memoize method
var getStopwords = function() {
if (_stopwords) return _stopwords;
var stopwords = {};
_.each(glob.sync('src/**/*.txt'), function(filename) {
var file = grunt.file.read(filename),
language = filename.match(/([a-z]{2})\.txt$/)[1];
stopwords[language] = stopwords[language] || [];
_.each(wordsInFile(file), function(word) {
if (stopwords[language].indexOf(word) === -1) {
stopwords[language].push(word);
}
});
});
for(var language in stopwords) {
stopwords[language] = stopwords[language].sort();
}
_stopwords = stopwords;
return stopwords;
};
grunt.registerTask('stopwordsToJson', function() {
var stopwords = getStopwords();
for(var language in stopwords) {
fs.writeFileSync("dist/"+language+".json", JSON.stringify(stopwords[language]), 'utf-8', {flags: 'w+'});
}
fs.writeFileSync("stopwords-all.json", JSON.stringify(stopwords), 'utf-8', {flags: 'w+'});
});
grunt.registerTask('stopwordsDocs', function() {
var stopwords = getStopwords();
var languageCodes = Object.keys(stopwords);
var table = "There are a total of "+languageCodes.length+" supported languages:\n\n";
table += "Language | Stopword count | Filename\n";
table += "--- | --- | ---\n";
var rows = [];
for (var languageCode in stopwords) {
var wordCount = stopwords[languageCode].length,
file = "["+languageCode + ".json](dist/"+languageCode+".json)",
language = languages.getLanguageInfo(languageCode);
rows.push([language.name, wordCount, file]);
}
rows = _.sortBy(rows, function(row) {
return row[0]; // sort by language name
});
table += _.map(rows, function(row) {
return row.join(" | ");
}).join("\n");
fs.writeFileSync("docs/supported-languages.md", table, 'utf-8', {flags: 'w+'});
});
grunt.initConfig({
pkg: pkg,
readme: {
options: {
alt: {
src: ['docs/README.tmpl.md'],
dest: './'
},
metadata: {
licenseName: pkg.license
}
}
}
});
grunt.loadNpmTasks('grunt-readme');
grunt.registerTask('default', ['stopwordsToJson', 'stopwordsDocs', 'readme']);
};