Extend Python Alphabet with config file and decode method
This commit is contained in:
parent
8b0abd5364
commit
bb4551caa9
@ -30,7 +30,7 @@ from util.feeding import DataSet, ModelFeeder
|
|||||||
from util.preprocess import preprocess
|
from util.preprocess import preprocess
|
||||||
from util.gpu import get_available_gpus
|
from util.gpu import get_available_gpus
|
||||||
from util.shared_lib import check_cupti
|
from util.shared_lib import check_cupti
|
||||||
from util.text import sparse_tensor_value_to_texts, wer, levenshtein, Alphabet, ndarray_to_text
|
from util.text import sparse_tensor_value_to_texts, wer, levenshtein, Alphabet
|
||||||
from xdg import BaseDirectory as xdg
|
from xdg import BaseDirectory as xdg
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
16
util/text.py
16
util/text.py
@ -10,6 +10,7 @@ from functools import reduce
|
|||||||
|
|
||||||
class Alphabet(object):
|
class Alphabet(object):
|
||||||
def __init__(self, config_file):
|
def __init__(self, config_file):
|
||||||
|
self._config_file = config_file
|
||||||
self._label_to_str = []
|
self._label_to_str = []
|
||||||
self._str_to_label = {}
|
self._str_to_label = {}
|
||||||
self._size = 0
|
self._size = 0
|
||||||
@ -29,9 +30,18 @@ class Alphabet(object):
|
|||||||
def label_from_string(self, string):
|
def label_from_string(self, string):
|
||||||
return self._str_to_label[string]
|
return self._str_to_label[string]
|
||||||
|
|
||||||
|
def decode(self, labels):
|
||||||
|
res = ''
|
||||||
|
for label in labels:
|
||||||
|
res += self.string_from_label(label)
|
||||||
|
return res
|
||||||
|
|
||||||
def size(self):
|
def size(self):
|
||||||
return self._size
|
return self._size
|
||||||
|
|
||||||
|
def config_file(self):
|
||||||
|
return self._config_file
|
||||||
|
|
||||||
def text_to_char_array(original, alphabet):
|
def text_to_char_array(original, alphabet):
|
||||||
r"""
|
r"""
|
||||||
Given a Python string ``original``, remove unsupported characters, map characters
|
Given a Python string ``original``, remove unsupported characters, map characters
|
||||||
@ -76,12 +86,6 @@ def sparse_tuple_to_texts(tuple, alphabet):
|
|||||||
# List of strings
|
# List of strings
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def ndarray_to_text(value, alphabet):
|
|
||||||
results = ''
|
|
||||||
for i in range(len(value)):
|
|
||||||
results += alphabet.string_from_label(value[i])
|
|
||||||
return results
|
|
||||||
|
|
||||||
def wer(original, result):
|
def wer(original, result):
|
||||||
r"""
|
r"""
|
||||||
The WER is defined as the editing/Levenshtein distance on word level
|
The WER is defined as the editing/Levenshtein distance on word level
|
||||||
|
Loading…
Reference in New Issue
Block a user