-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdocument.py
More file actions
409 lines (348 loc) · 13.7 KB
/
document.py
File metadata and controls
409 lines (348 loc) · 13.7 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import re
import jieba
import nltk
import pygments.token
import zhconv
import pygments
from pygments.lexers import get_lexer_by_name
from typing import Tuple
from functools import partial
from itertools import chain
from redpajama.core.document import Document, split_paragraphs, _compute_ngrams
from redpajama.core.data_types import TextSlice
from utils.text_utils import normalize_text
from artifacts.extension_to_program import EXTENSION_TO_PROGRAM
from artifacts.filename_to_program import FILENAME_TO_PROGRAM
import sys
sys.setrecursionlimit(10000) # increase the recursive depth limit to 10000
# English and other tokenizer
def _nltk_tokenize(x): return nltk.wordpunct_tokenize(x)
# Chinese tokenizer
def _jieba_tokenize(x): return [w for w in jieba.lcut(x) if w.strip()]
# Japanese tokenizer
def _mecab_tokenize(x):
import MeCab
mecab = MeCab.Tagger("-Owakati")
result = mecab.parse(x)
if not result:
result = ""
return result.strip().split()
class QSCodeDocument(Document):
def __init__(
self,
content:str,
filename: str,
language: str,
extension: str,
file_size_in_byte: int,
program_lang: str,
doc_type: str,
):
# initialize the document general attributes
self.init_doc_attributes(content, language)
# initialize the code specific attributes
self.init_code_attributes(
filename,
extension,
file_size_in_byte,
program_lang,
doc_type
)
def init_doc_attributes(self, content, language):
self._lang = language
if language == 'zh':
self._raw_content = zhconv.convert(content, 'zh-cn')
self._len_raw_content = len(content)
self.tokenize_func = _jieba_tokenize
elif language == 'ja':
self._raw_content = content
self._len_raw_content = len(content)
self.tokenize_func = _mecab_tokenize
else:
self._raw_content = content
self._len_raw_content = len(content)
self.tokenize_func = _nltk_tokenize
self._normalize_func = partial(normalize_text,
remove_control=True,
halfpunct=True,
punct='replace',
lowercase=True,
white_space=True,
unicode='NFKC',
remove_zh_whitespace=False,
remove_digits=False)
self._normalized_content = None
self._raw_lines = None
self._normalized_lines = None
self._raw_words_in_line = None
self._raw_words = None
self._normalized_words_in_line = None
self._normalized_words = None
self._num_raw_words = None
self._num_normalized_words = None
self._raw_2grams = None
self._raw_3grams = None
self._norm_2grams = None
self._norm_3grams = None
self._norm_4grams = None
def init_code_attributes(self, filename, extension, file_size_in_byte, program_lang, doc_type):
self.filename = filename
self._extension = extension
self._file_size_in_byte = file_size_in_byte
self._program_lang = program_lang
self._doc_type = doc_type
self._ast = None
self._is_code = None
# code content without comment
self._code_raw_content = None
self._code_raw_lines = None
self._code_normalized_lines = None
# comment content without code
self._comment_raw_content = None
self._comment_raw_lines = None
self._comment_normalized_lines = None
# content without blank characters
self._visiable_content = None
def __len__(self):
return self._len_raw_content
@classmethod
def get_program_language(cls, extension, filename=''):
if filename in FILENAME_TO_PROGRAM:
return FILENAME_TO_PROGRAM[filename]
if extension in EXTENSION_TO_PROGRAM:
return EXTENSION_TO_PROGRAM[extension]
return "unknown"
@property
def language(self):
return self._lang
@property
def raw_content(self):
return self._raw_content
@property
def normalized_content(self):
if not self._normalized_content:
# the normalized content: lowercased and punctuation removed
self._normalized_content = ' '.join([l.text for l in self.normalized_lines])
return self._normalized_content
@property
def raw_lines(self):
if not self._raw_lines:
# the lines of the document (split by newline)
self._raw_lines: Tuple[TextSlice] = split_paragraphs(
text=self.raw_content, normalizer=lambda x: x, remove_empty=False
)
return self._raw_lines
@property
def normalized_lines(self):
if not self._normalized_lines:
# the lines of the document (split by newline), normalized
self._normalized_lines: Tuple[TextSlice] = split_paragraphs(
text=self.raw_content, normalizer=self._normalize_func, remove_empty=False
)
return self._normalized_lines
@property
def raw_words_in_line(self):
if not self._raw_words_in_line:
self._raw_words_in_line = tuple(tuple(self.tokenize_func(textslice.text)) for textslice in self.raw_lines)
return self._raw_words_in_line
@property
def raw_words(self):
if not self._raw_words:
self._raw_words = tuple(chain(*self.raw_words_in_line))
return self._raw_words
@property
def normalized_words_in_line(self):
if not self._normalized_words_in_line:
# the normalized words of the document (split by whitespace)
if self._lang == 'zh':
self._normalized_words_in_line = tuple(tuple(self.tokenize_func(textslice.text)) for textslice in self.normalized_lines)
else:
self._normalized_words_in_line = tuple(tuple(self.tokenize_func(textslice.text)) for textslice in self.normalized_lines)
return self._normalized_words_in_line
@property
def normalized_words(self):
if not self._normalized_words:
self._normalized_words = tuple(chain(*self.normalized_words_in_line))
return self._normalized_words
@property
def num_raw_words(self):
if not self._num_raw_words:
self._num_raw_words = len(self.raw_words)
return self._num_raw_words
@property
def num_normalized_words(self):
if not self._num_normalized_words:
self._num_normalized_words = len(self.normalized_words)
return self._num_normalized_words
@property
def raw_1grams(self):
return self._raw_words
@property
def raw_2grams(self):
if not self._raw_2grams:
self._raw_2grams = _compute_ngrams(self.raw_words, 2)
return self._raw_2grams
@property
def raw_3grams(self):
if not self._raw_3grams:
self._raw_3grams = _compute_ngrams(self.raw_words, 3)
return self._raw_3grams
@property
def norm_1grams(self):
return self.normalized_words
@property
def norm_2grams(self):
if not self._norm_2grams:
self._norm_2grams = _compute_ngrams(self.normalized_words, 2)
return self._norm_2grams
@property
def norm_3grams(self):
if not self._norm_3grams:
self._norm_3grams = _compute_ngrams(self.normalized_words, 3)
return self._norm_3grams
@property
def norm_4grams(self):
if not self._norm_4grams:
self._norm_4grams = _compute_ngrams(self.normalized_words, 4)
return self._norm_4grams
@property
def visible_content(self):
if not self._visiable_content:
self._visiable_content = re.sub(r'\s+', '', self.raw_content)
return self._visiable_content
@property
def extension(self):
return self._extension
@property
def program_lang(self):
if self._program_lang is None:
self._program_lang = self.get_program_language(self.extension)
return self._program_lang
@property
def doc_type(self):
return self._doc_type
@property
def file_size_in_byte(self):
return self._file_size_in_byte
@property
def code_raw_content(self):
if self.program_lang is None:
self._comment_raw_content = self.raw_content
self._code_raw_content = self.raw_content
return self._code_raw_content
if self._code_raw_content:
return self._code_raw_content
if self.program_lang == 'perl':
self._code_raw_content, self._comment_raw_content = self.get_perl_comment()
return self._code_raw_content
if self.program_lang == 'python':
pass
code_raw_content = ""
comment_raw_content = ""
try:
#1. parse by pygments lexer
lexer = get_lexer_by_name(self.program_lang)
tokens = pygments.lex(self.raw_content, lexer)
for token_type, token in tokens:
if not (
token_type == pygments.token.Comment.Multiline
or token_type == pygments.token.Comment.Single
or token_type == pygments.token.Literal.String.Doc
or token_type == pygments.token.Comment.Hashbang
or token_type == pygments.token.Comment.Special
):
code_raw_content += token
else:
comment_raw_content += token
except pygments.util.ClassNotFound:
#2. parse by hand
code_raw_content, comment_raw_content = self.get_comment_by_hand()
self._code_raw_content = code_raw_content
self._comment_raw_content = comment_raw_content
return self._code_raw_content
@property
def comment_raw_content(self):
if not self._comment_raw_content:
# comment and code are simultanoeusly generated
self.code_raw_content
return self._comment_raw_content
@property
def code_normalized_lines(self):
if not self._code_normalized_lines:
self._code_normalized_lines = split_paragraphs(text=self.code_raw_content,normalizer=lambda x:x, remove_empty=True)
return self._code_normalized_lines
@property
def comment_normalized_lines(self):
if not self._comment_normalized_lines:
self._comment_normalized_lines = split_paragraphs(text=self.comment_raw_content,normalizer=lambda x:x, remove_empty=True)
return self._comment_normalized_lines
# remove blank characters
@property
def visible_content(self):
if not self._visiable_content:
self._visiable_content = re.sub(r'\s+', '', self.raw_content)
return self._visiable_content
@property
def valid_lines_len(self):
return len(self.code_normalized_lines)
@property
def ast(self):
from utils.code.js_utils import parse_js
from utils.code.python_utils import parse_py
from utils.code.html_utils import parse_html
if not self._ast:
if self.program_lang == 'javascript':
self._ast = parse_js(self.raw_content)
elif self.program_lang == 'python':
self._ast = parse_py(self.raw_content)
elif self.program_lang == 'html':
self._ast = parse_html(self.raw_content)
return self._ast
def get_comment_by_hand(self):
code_raw_content = ""
comment_raw_content = ""
in_multiline_comment = False
comment_flag = False
for line in self.raw_lines:
strip_line = line.text.strip()
if strip_line.startswith(('"""','/*')) or strip_line.endswith(('"""', '*/')):
in_multiline_comment = not in_multiline_comment
comment_flag = in_multiline_comment
comment_raw_content += line.text
continue
if strip_line.startswith(('#','//','--')) or in_multiline_comment:
in_multiline_comment = True
comment_raw_content += line.text
continue
if strip_line == '\n' and comment_flag:
comment_raw_content += line.text
continue
comment_flag = False
code_raw_content += line.text
return code_raw_content, comment_raw_content
def get_perl_comment(self):
code_raw_content = ""
comment_raw_content = ""
in_multiline_comment = False
comment_flag = False
for line in self.raw_lines:
strip_line = line.text.strip()
if strip_line.startswith('=cut'):
in_multiline_comment = False
comment_flag = False
comment_raw_content += line.text
continue
if strip_line.startswith('='):
in_multiline_comment = True
comment_flag = True
comment_raw_content += line.text
continue
if strip_line.startswith('#') or in_multiline_comment:
comment_raw_content += line.text
continue
if strip_line == '\n' and comment_flag:
comment_raw_content += line.text
continue
comment_flag = False
code_raw_content += line.text
return code_raw_content, comment_raw_content