-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
54 lines (45 loc) · 1.28 KB
/
Copy pathutil.py
File metadata and controls
54 lines (45 loc) · 1.28 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
import tensorflow as tf
import tflearn
import numpy as np
import nltk
from nltk.stem.lancaster import LancasterStemmer
from collections import Counter
import os
class Dictionary:
def __init__(self, vocab_size):
self.counter = Counter()
self.word2id = {}
self.id2word = []
self.total = 0
self.vocab_size = vocab_size
#returns true until vocab_size reached
def add_word(self, word) -> bool:
if word not in self.word2id:
if len(self.id2word) < vocab_size:
self.id2word.append(word)
self.word2id[word] = len(self.id2word) - 1
else:
return False
token_id = word2id[word]
self.counter[token_id] += 1
self.total += 1
return True
def __len__(self):
return len(self.word_dict)
def process_file(path, vocab_size):
count = [['UNK', -1],['EOS',-1],[]]
stemmer = LancasterStemmer()
words = Dictionary(vocab_size)
assert os.path.exists(path)
with open(path,'r',encoding='utf-8') as f:
for line in f:
#tokenize each line
line = tokenize(line)
for token in set(line)
words.add_word(token)
def tokenize(line):
tokens = nltk.word_tokenize(line)
#applies the stemmer (such that words have their suffixes dropped: tired -> tir, whine -> whin, whining -> whin)
tokens = [stemmer.stem(w.lower()) for w in words]
tokens.append("<eos>")
return tokens