-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtokenizer.py
33 lines (31 loc) · 1.23 KB
/
tokenizer.py
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
import re
def tokenizer(texts):
# buka dulu txtnya
# buat dulu regex special case
special_cases = ['=', '+', '-', '*', '/', r'\%', '(', ')', '[', ']'
, '{', '}', '#', '>', '<', r'\>\=', r'\<\=', r'\=\=', r'\!\='
, r'\`\`\`', ':', '\n', '"', ",", ".", r'\*\*', "'", "=="]
# Buat spasi dan tempWord
spasi = " "
tempWord = ""
# ntar tokenized nyimpen hasil akhir
tokenized = []
for i in range(len(texts)):
# selama dia bukan spasi, concat ke tempWord
if texts[i] != spasi:
tempWord += texts[i]
# Cek klo setelah ini spasi atau dia ada di spesial case, klo iya, berarti dia udah satu kata dan diappend
if i + 1 < len(texts):
if ((texts[i] + texts[i + 1]) in special_cases or texts[i + 1] in special_cases or
texts[i + 1] == spasi or tempWord in special_cases):
tokenized.append(tempWord)
tempWord = ""
tokenized.append(tempWord) # buat append sisa terakhir
tempToken = tokenized
tokenized = []
# buang string kosong
for token in tempToken:
if token != "" or token != '':
tokenized.append(token)
# print(tokenized)
return tokenized