format_sv.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright 2017 Mycroft AI Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 
18 from mycroft.util.lang.format_common import convert_to_mixed_fraction
19 
20 FRACTION_STRING_SV = {
21  2: 'halv',
22  3: 'tredjedel',
23  4: 'fjärdedel',
24  5: 'femtedel',
25  6: 'sjättedel',
26  7: 'sjundedel',
27  8: 'åttondel',
28  9: 'niondel',
29  10: 'tiondel',
30  11: 'elftedel',
31  12: 'tolftedel',
32  13: 'trettondel',
33  14: 'fjortondel',
34  15: 'femtondel',
35  16: 'sextondel',
36  17: 'sjuttondel',
37  18: 'artondel',
38  19: 'nittondel',
39  20: 'tjugondel'
40 }
41 
42 
43 def nice_number_sv(number, speech, denominators):
44  """ Swedish helper for nice_number
45 
46  This function formats a float to human understandable functions. Like
47  4.5 becomes "4 och en halv" for speech and "4 1/2" for text
48 
49  Args:
50  number (int or float): the float to format
51  speech (bool): format for speech (True) or display (False)
52  denominators (iter of ints): denominators to use, default [1 .. 20]
53  Returns:
54  (str): The formatted string.
55  """
56  result = convert_to_mixed_fraction(number, denominators)
57  if not result:
58  # Give up, just represent as a 3 decimal number
59  return str(round(number, 3))
60 
61  whole, num, den = result
62 
63  if not speech:
64  if num == 0:
65  # TODO: Number grouping? E.g. "1,000,000"
66  return str(whole)
67  else:
68  return '{} {}/{}'.format(whole, num, den)
69 
70  if num == 0:
71  return str(whole)
72  den_str = FRACTION_STRING_SV[den]
73  if whole == 0:
74  if num == 1:
75  return_string = 'en {}'.format(den_str)
76  else:
77  return_string = '{} {}'.format(num, den_str)
78  elif num == 1:
79  return_string = '{} och en {}'.format(whole, den_str)
80  else:
81  return_string = '{} och {} {}'.format(whole, num, den_str)
82  if num == 2:
83  return_string += 'a'
84  if num > 2:
85  return_string += 'ar'
86  return return_string
def convert_to_mixed_fraction(number, denominators)
def nice_number_sv(number, speech, denominators)
Definition: format_sv.py:43


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40