- Conect to remote translation service and translate array of strings - Create SentencesParser module with texts management methods: - Detect split position method: When the text requested to translate is too large, we need split it in smaller parts for we can translate. This method search first valid point (dot or whitespace) for split this text so then we can get an response without dividing the word in half.
25 lines
786 B
Ruby
25 lines
786 B
Ruby
module SentencesParser
|
|
|
|
def detect_split_position(text)
|
|
minimum_valid_index = text.size - MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST
|
|
valid_point = text[minimum_valid_index..text.size].index(".")
|
|
valid_whitespace = text[minimum_valid_index..text.size].index(" ")
|
|
|
|
get_split_position(valid_point, valid_whitespace, minimum_valid_index)
|
|
end
|
|
|
|
def get_split_position(valid_point, valid_whitespace, minimum_valid_index)
|
|
split_position = minimum_valid_index
|
|
if valid_point.present? || valid_whitespace.present?
|
|
valid_position = valid_point.present? ? valid_point : valid_whitespace
|
|
split_position = split_position + valid_position
|
|
end
|
|
split_position
|
|
end
|
|
|
|
def characters_count(texts)
|
|
texts.map(&:size).reduce(:+)
|
|
end
|
|
|
|
end
|