-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_tools.py
31 lines (24 loc) · 958 Bytes
/
text_tools.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
import numpy as np
import pandas as pd
import nltk
import sys
from os import path
class TextAnalyzer:
@staticmethod
def prepare_data(df, text_column_id):
tokenizer = nltk.RegexpTokenizer(r"\w+")
sentence_bag = []
words_bag = []
for col in text_column_id: # itero sulla lista di id di colonna in input
for i in range(0, df[col].size):
sentence = df[col][i]
if type(sentence) == str:
sentence = sentence.lower()
tokens = tokenizer.tokenize(sentence)
stopwords = open("./stopwords.txt", "r",
encoding="utf-8").read().split()
tokens = [w for w in tokens if not w in stopwords]
sentence_bag.append(tokens)
for j in tokens:
words_bag.append(j)
return words_bag, sentence_bag