-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtrickster.js
56 lines (52 loc) · 1.39 KB
/
trickster.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
var MAGIC_TRICK_STRING = "<PPPPPP>",
magic_regex = new RegExp(MAGIC_TRICK_STRING + "_.{2,3} \._\.\n", "g"),
newline_end_regex = /\n$/;
/**
* Wraps text in a magic string so that paragraphs can be preserved
* after the text is POS-tagged by the Stanford POS-tagger.
*/
var Trickster = function() {
/**
* Decorate the paragraphs of a text blob.
* Call before tagging.
*
* @param string text
* The text to POS-tag.
*
* @param string
* The paragraph-decorated text blob.
*/
this.trick = function(text) {
var paragraphs = [];
text.split("\n").forEach(function(paragraph) {
paragraph = paragraph.trim();
if (paragraph.length === 0) return;
paragraphs.push(paragraph);
});
var temp_paragraphs = [];
paragraphs.forEach(function(paragraph) {
temp_paragraphs.push(paragraph);
temp_paragraphs.push(MAGIC_TRICK_STRING + ".");
});
temp_paragraphs.pop();
return temp_paragraphs.join("\n");
};
/**
* Undecorates a POS-tagged text blob.
* Call after tagging.
*
* @param string text
* The POS-tagged text.
*
* @param array
* The POS-tagged text, separated into paragarphs.
*/
this.untrick = function(text) {
return text
.split(magic_regex)
.map(function(p) {
return p.replace(newline_end_regex, "");
});
};
}
module.exports.Trickster = Trickster;