format_pt.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 from mycroft.util.lang.common_data_pt import _FRACTION_STRING_PT, \
20  _NUM_STRING_PT
21 
22 
23 def nice_number_pt(number, speech, denominators):
24  """ Portuguese helper for nice_number
25 
26  This function formats a float to human understandable functions. Like
27  4.5 becomes "4 e meio" for speech and "4 1/2" for text
28 
29  Args:
30  number (int or float): the float to format
31  speech (bool): format for speech (True) or display (False)
32  denominators (iter of ints): denominators to use, default [1 .. 20]
33  Returns:
34  (str): The formatted string.
35  """
36 
37  result = convert_to_mixed_fraction(number, denominators)
38  if not result:
39  # Give up, just represent as a 3 decimal number
40  return str(round(number, 3))
41 
42  whole, num, den = result
43 
44  if not speech:
45  if num == 0:
46  # TODO: Number grouping? E.g. "1,000,000"
47  return str(whole)
48  else:
49  return '{} {}/{}'.format(whole, num, den)
50 
51  if num == 0:
52  return str(whole)
53  # denominador
54  den_str = _FRACTION_STRING_PT[den]
55  # fracções
56  if whole == 0:
57  if num == 1:
58  # um décimo
59  return_string = 'um {}'.format(den_str)
60  else:
61  # três meio
62  return_string = '{} {}'.format(num, den_str)
63  # inteiros >10
64  elif num == 1:
65  # trinta e um
66  return_string = '{} e {}'.format(whole, den_str)
67  # inteiros >10 com fracções
68  else:
69  # vinte e 3 décimo
70  return_string = '{} e {} {}'.format(whole, num, den_str)
71  # plural
72  if num > 1:
73  return_string += 's'
74  return return_string
75 
76 
77 def pronounce_number_pt(num, places=2):
78  """
79  Convert a number to it's spoken equivalent
80  For example, '5.2' would return 'cinco virgula dois'
81  Args:
82  num(float or int): the number to pronounce (under 100)
83  places(int): maximum decimal places to speak
84  Returns:
85  (str): The pronounced number
86  """
87  if abs(num) >= 100:
88  # TODO: Support n > 100
89  return str(num)
90 
91  result = ""
92  if num < 0:
93  result = "menos "
94  num = abs(num)
95 
96  if num >= 20:
97  tens = int(num - int(num) % 10)
98  ones = int(num - tens)
99  result += _NUM_STRING_PT[tens]
100  if ones > 0:
101  result += " e " + _NUM_STRING_PT[ones]
102  else:
103  result += _NUM_STRING_PT[int(num)]
104 
105  # Deal with decimal part, in portuguese is commonly used the comma
106  # instead the dot. Decimal part can be written both with comma
107  # and dot, but when pronounced, its pronounced "virgula"
108  if not num == int(num) and places > 0:
109  result += " vírgula"
110  place = 10
111  while int(num * place) % 10 > 0 and places > 0:
112  result += " " + _NUM_STRING_PT[int(num * place) % 10]
113  place *= 10
114  places -= 1
115  return result
116 
117 
118 def nice_time_pt(dt, speech=True, use_24hour=False, use_ampm=False):
119  """
120  Format a time to a comfortable human format
121  For example, generate 'cinco treinta' for speech or '5:30' for
122  text display.
123  Args:
124  dt (datetime): date to format (assumes already in local timezone)
125  speech (bool): format for speech (default/True) or display (False)=Fal
126  use_24hour (bool): output in 24-hour/military or 12-hour format
127  use_ampm (bool): include the am/pm for 12-hour format
128  Returns:
129  (str): The formatted time string
130  """
131  if use_24hour:
132  # e.g. "03:01" or "14:22"
133  string = dt.strftime("%H:%M")
134  else:
135  if use_ampm:
136  # e.g. "3:01 AM" or "2:22 PM"
137  string = dt.strftime("%I:%M %p")
138  else:
139  # e.g. "3:01" or "2:22"
140  string = dt.strftime("%I:%M")
141  if string[0] == '0':
142  string = string[1:] # strip leading zeros
143 
144  if not speech:
145  return string
146 
147  # Generate a speakable version of the time
148  speak = ""
149  if use_24hour:
150  # simply speak the number
151  if dt.hour == 1:
152  speak += "uma"
153  else:
154  speak += pronounce_number_pt(dt.hour)
155 
156  # equivalent to "quarter past ten"
157  if dt.minute > 0:
158  speak += " e " + pronounce_number_pt(dt.minute)
159 
160  else:
161  # speak number and add daytime identifier
162  # (equivalent to "in the morning")
163  if dt.minute == 35:
164  minute = -25
165  hour = dt.hour + 1
166  elif dt.minute == 40:
167  minute = -20
168  hour = dt.hour + 1
169  elif dt.minute == 45:
170  minute = -15
171  hour = dt.hour + 1
172  elif dt.minute == 50:
173  minute = -10
174  hour = dt.hour + 1
175  elif dt.minute == 55:
176  minute = -5
177  hour = dt.hour + 1
178  else:
179  minute = dt.minute
180  hour = dt.hour
181 
182  if hour == 0:
183  speak += "meia noite"
184  elif hour == 12:
185  speak += "meio dia"
186  # 1 and 2 are pronounced in female form when talking about hours
187  elif hour == 1 or hour == 13:
188  speak += "uma"
189  elif hour == 2 or hour == 14:
190  speak += "duas"
191  elif hour < 13:
192  speak = pronounce_number_pt(hour)
193  else:
194  speak = pronounce_number_pt(hour - 12)
195 
196  if minute != 0:
197  if minute == 15:
198  speak += " e um quarto"
199  elif minute == 30:
200  speak += " e meia"
201  elif minute == -15:
202  speak += " menos um quarto"
203  else:
204  if minute > 0:
205  speak += " e " + pronounce_number_pt(minute)
206  else:
207  speak += " " + pronounce_number_pt(minute)
208 
209  # exact time
210  if minute == 0 and not use_ampm:
211  # 3:00
212  speak += " em ponto"
213 
214  if use_ampm:
215  if hour > 0 and hour < 6:
216  speak += " da madrugada"
217  elif hour >= 6 and hour < 12:
218  speak += " da manhã"
219  elif hour >= 13 and hour < 21:
220  speak += " da tarde"
221  elif hour != 0 and hour != 12:
222  speak += " da noite"
223  return speak
def pronounce_number_pt(num, places=2)
Definition: format_pt.py:77
def nice_number_pt(number, speech, denominators)
Definition: format_pt.py:23
def convert_to_mixed_fraction(number, denominators)
def nice_time_pt(dt, speech=True, use_24hour=False, use_ampm=False)
Definition: format_pt.py:118


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