This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.py
93 lines (62 loc) · 2.38 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
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
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize, RegexpTokenizer
class Token:
def __init__(self, text):
self.words_list=[]
self.alphaNumeric = []
self.punctuation = []
self.sentences = []
self.number_of_sentences = 0
self.word_frequencies = []
self.words_are_tokenized(text)
self.alpha_num_characters(text)
self.list_of_punctuation(text)
self.sentence_list(text)
self.sentence_count(text)
self.word_frequency(text)
def phrase_to_string(self, phrase):
#"assures the input is not an empty string, and converts input into lower case"
text = phrase.lower()
return text
def words_are_tokenized(self, text):
wordCount = text.lower()
wordCount = word_tokenize(text)
self.words_list = wordCount
return wordCount
def alpha_num_characters(self, text):
aplhaNumText = "".join(c for c in text if c not in ('!','.',':','?',',',"'",'-',' '))
aplhaNumText = aplhaNumText.lower()
df = nltk.FreqDist(aplhaNumText)
alphaNums = list(df.keys())
alphaNums.sort()
self.alphaNumeric = alphaNums
return alphaNums
def list_of_punctuation(self,text):
punctuation_list = []
for i in text:
if i =="," or i=="." or i=="!" or i =="?" or i ==":" or i ==";":
punctuation_list.append(i)
self.punctuation = punctuation_list
return punctuation_list
def sentence_list(self, text):
sentence_list = (sent_tokenize(text))
self.sentences = sentence_list
return sentence_list
def sentence_count(self, text):
sentence_list = (sent_tokenize(text))
sentence_count = len(sentence_list)
self.number_of_sentences = sentence_count
return sentence_count
def word_frequency(self,text):
text = text.lower()
regex_tokenizer = RegexpTokenizer(r'\w+')
token_words = regex_tokenizer.tokenize(text)
fd = nltk.FreqDist(token_words)
word_frequency = list(fd.items())
word_frequency.sort()
self.word_frequencies = word_frequency
return word_frequency
def word_position(phrase,text):
word_position = dict(enumerate(text))
word_position = {k+1:v for (k,v) in word_position.items()}
return word_position