-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (100 loc) · 3.1 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var iconv = require('iconv-lite');
var fs = require('fs');
var os = require('os');
var nodepos = require('node-pos');
var fastcsv = require('fast-csv');
//var str = "I think it's Steve Jobs standing in a dark room,and he seems 😁. ";
var preposition = "";
var str;
exports.toText = function (sentence, callback) {
str = sentence;
var utf8 = unescape(encodeURIComponent(str));
var arr = []
//convers to unicode
for (var i = 0; i < utf8.length; i++) {
arr.push(utf8.charCodeAt(i).toString(16));
}
var unicode = []
var u = 0;
for(var i = 0; i < arr.length; i++){
if(arr[i] == "f0" || arr[i] == "e2"){
unicode[u] = "\\x" + arr[i].toUpperCase()+
"\\x" + arr[i+1].toUpperCase()+
"\\x" + arr[i+2].toUpperCase()+
"\\x" + arr[i+3].toUpperCase();
u++;
}
}
var stream = fs.createReadStream(__dirname+"/emDict.csv");
var newstr = -1;
var csvStream = fastcsv.parse({objectMode:true,headers: true, delimiter:";"})
.on("data", function(data){
for(var u = 0; u < unicode.length ; u++){
var unicodeSmile = unicode[u].replace(/\\\\/g,"\\");
if(data.Bytes == unicodeSmile){
str = generatePreposition(str,data.Description,data.Native);
var str_split = str.toString().split(data.Native)
var index
if(str_split[0].length == 0){ //in the begining
index = 1
}else if(str_split[str_split.length-1].length == 0){ //in the end
index = 0
}else{ //in the middle
index = -1
}
if(index == 0){
newstr = str_split[index]+ preposition + data.Description.toLowerCase();
}else if(index == 1){
newstr = preposition+data.Description.toLowerCase()+","+str_split[index];
newstr = newstr.toString().charAt(0).toUpperCase() + newstr.toString().slice(1).toLowerCase();
}else{
if(str_split[1].charAt(0) != " ")
str_split[1]= " "+str_split[1]
newstr = str_split[0] + data.Description.toLowerCase()+str_split[1];
}
}
}
})
.on("end", function(){
callback(null, newstr);
});
stream.pipe(csvStream);
};
function generatePreposition(str,desc,emoji) {
var sentence = str.split(" ");
var e_index = -1;
var found = false;
var withWord = false;
//get emoji index
for(var e = 0; e < sentence.length; e++){
if(sentence[e] == emoji){
e_index = e;
found = true;
}
}
console.log(str);
var tmp_str = str.split(emoji)
if(!found){
if( str.indexOf(emoji) > -1 ){
e_index = str.indexOf(emoji);
withWord = true;
}
}
if(withWord){
if(str.charAt(e_index-1) != " "){ //has space before emoji
str = tmp_str[0]+" "+emoji + tmp_str[1];
console.log(str);
}else if(str.charAt(e_index+1) != " "){ //has space after emoji
str = tmp_str[0]+emoji+ " " + tmp_str[1];
}
}
if(desc.indexOf("FACE") > -1){
preposition = "with a "
}else{
if(e != 0){
if(str[e-2] != "a" )
preposition = "a "
}
}
return str;
}