Skip to content

Commit b2d3679

Browse files
committed
Add Encode Feature
1 parent 6a6fc88 commit b2d3679

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

main.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,29 @@ def summary(model_path):
332332
print("[Model Config]")
333333
pprint(model.get_config())
334334

335+
336+
def encode(content, word_delimiter="|", tag_delimiter="/", num_step=60):
337+
# Create corpus instance
338+
corpus = Corpus(word_delimiter=word_delimiter, tag_delimiter=tag_delimiter)
339+
340+
# Add text to corpus
341+
corpus.add_text(content)
342+
343+
# Create index for character and tag
344+
char_index = index_builder(constant.CHARACTER_LIST,
345+
constant.CHAR_START_INDEX)
346+
tag_index = index_builder(constant.TAG_LIST, constant.TAG_START_INDEX)
347+
348+
# Generate input
349+
inb = InputBuilder(corpus, char_index, tag_index, num_step, y_one_hot=False)
350+
351+
# Display encoded content
352+
np.set_printoptions(threshold=np.inf)
353+
print("[Input]")
354+
print(inb.x)
355+
print("[Label]")
356+
print(inb.y)
357+
335358
def show(var):
336359
"""Show variable"""
337360

@@ -367,6 +390,7 @@ def show(var):
367390
"test": test,
368391
"reevaluate": reevaluate,
369392
"summary": summary,
393+
"encode": encode,
370394
"show": show
371395
})
372396

utils.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ def __init__(self, path, filename, content):
2323
class Corpus(object):
2424
"""Corpus Manager"""
2525

26-
def __init__(self, corpus_directory, word_delimiter=None, tag_delimiter=None):
26+
def __init__(self, corpus_directory=None, word_delimiter=None, tag_delimiter=None):
2727
# Global variable
2828
self.corpus_directory = corpus_directory
2929
self.word_delimiter = word_delimiter
3030
self.tag_delimiter = tag_delimiter
3131
self.__corpus = list()
3232

3333
# Load corpus to memory
34-
self._load()
34+
if corpus_directory is not None:
35+
self._load()
3536

3637
def _preprocessing(self, content):
3738
"""Text preprocessing"""
@@ -92,6 +93,16 @@ def _load(self):
9293
# Add text to corpus
9394
self.__corpus.append(text)
9495

96+
def add_text(self, content):
97+
# Preprocessing
98+
content = self._preprocessing(content)
99+
100+
# Create text instance
101+
text = Text(str(), str(), content)
102+
103+
# Add text to corpus
104+
self.__corpus.append(text)
105+
95106
@property
96107
def count(self):
97108
return len(self.__corpus)

0 commit comments

Comments
 (0)