mycroft-core/mycroft/util/lang/format_sv.py

87 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright 2017 Mycroft AI Inc.
#
# Licensed 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.
#
from mycroft.util.lang.format_common import convert_to_mixed_fraction
FRACTION_STRING_SV = {
2: 'halv',
3: 'tredjedel',
4: 'fjärdedel',
5: 'femtedel',
6: 'sjättedel',
7: 'sjundedel',
8: 'åttondel',
9: 'niondel',
10: 'tiondel',
11: 'elftedel',
12: 'tolftedel',
13: 'trettondel',
14: 'fjortondel',
15: 'femtondel',
16: 'sextondel',
17: 'sjuttondel',
18: 'artondel',
19: 'nittondel',
20: 'tjugondel'
}
def nice_number_sv(number, speech, denominators):
""" Swedish helper for nice_number
This function formats a float to human understandable functions. Like
4.5 becomes "4 och en halv" for speech and "4 1/2" for text
Args:
number (int or float): the float to format
speech (bool): format for speech (True) or display (False)
denominators (iter of ints): denominators to use, default [1 .. 20]
Returns:
(str): The formatted string.
"""
result = convert_to_mixed_fraction(number, denominators)
if not result:
# Give up, just represent as a 3 decimal number
return str(round(number, 3))
whole, num, den = result
if not speech:
if num == 0:
# TODO: Number grouping? E.g. "1,000,000"
return str(whole)
else:
return '{} {}/{}'.format(whole, num, den)
if num == 0:
return str(whole)
den_str = FRACTION_STRING_SV[den]
if whole == 0:
if num == 1:
return_string = 'en {}'.format(den_str)
else:
return_string = '{} {}'.format(num, den_str)
elif num == 1:
return_string = '{} och en {}'.format(whole, den_str)
else:
return_string = '{} och {} {}'.format(whole, num, den_str)
if num == 2:
return_string += 'a'
if num > 2:
return_string += 'ar'
return return_string