blob: ec96b68796533c48a9de2c347aff527a5aa497ac [file] [log] [blame]
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
Module: This module contains common conversion functions
"""
def char2int(char):
"""
Convert character to integer.
"""
if char >= 'a' and char <= 'z':
return ord(char) - ord('a')
elif char == ' ':
return 26
return None
def int2char(num):
"""
Convert integer to character.
"""
if num >= 0 and num < 26:
return chr(num + ord('a'))
elif num == 26:
return ' '
return None
def word_to_vector(word):
"""
Convert character vectors to integer vectors.
"""
vector = []
for char in list(word):
vector.append(char2int(char))
return vector
def vector_to_word(vector):
"""
Convert integer vectors to character vectors.
"""
word = ""
for vec in vector:
word = word + int2char(vec)
return word
def char_conv(out):
"""
Convert integer vectors to character vectors for batch.
"""
out_conv = list()
for i in range(out.shape[0]):
tmp_str = ''
for j in range(out.shape[1]):
if int(out[i][j]) >= 0:
tmp_char = int2char(int(out[i][j]))
if int(out[i][j]) == 27:
tmp_char = ''
tmp_str = tmp_str + tmp_char
out_conv.append(tmp_str)
return out_conv