-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextPreprocessor.py
88 lines (52 loc) · 1.57 KB
/
textPreprocessor.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
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
#!/usr/bin/env python
# coding: utf-8
# In[4]:
import pandas as pd
import numpy as np
import os
import sys
import re
# In[5]:
import sentencepiece as spm
from keras.preprocessing import text, sequence
# In[ ]:
def 구간(ascii_value):
if 48<=ascii_value<=57:
return 0
elif 65<=ascii_value<=122:
return 1
else:
return 2
# In[3]:
class Preprocessor():
def __init__(self):
self.SPM_orig = spm.SentencePieceProcessor()
self.SPM_orig.load("sentencepiece_trsfm.model")
def remove_punct(self,x):
pattern = re.compile('[^a-zA-Z0-9가-힣]')
x = str(x).strip()
x = re.sub(pattern, ' ', x)
return x
def sep_words(self, text):
save_idx=[]
for i in range(1,len(text)):
if 구간(ord(text[i]))==구간(ord(text[i-1])):
pass
else:
save_idx.append(i-1)
count=0
for k in range(len(save_idx)):
count+=1
text=text[:save_idx[k]+count]+" "+text[save_idx[k]+count:]
return text
def remove_multispace(self, x):
x = str(x).strip()
x = re.sub(' +', ' ',x)
return x
def spm_encoding(self,x):
return self.SPM_orig.encode_as_ids(x)
def spm_token(self, x):
return self.SPM_orig.encode_as_pieces(x)
def padding(self,data,token_col, max_len = 75):
padded_tokens=sequence.pad_sequences(data[token_col], max_len, padding="post")
return padded_tokens