format_fr.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 """ Format functions for french (fr)
18 
19 """
20 
21 from mycroft.util.lang.format_common import convert_to_mixed_fraction
22 
23 NUM_STRING_FR = {
24  0: 'zéro',
25  1: 'un',
26  2: 'deux',
27  3: 'trois',
28  4: 'quatre',
29  5: 'cinq',
30  6: 'six',
31  7: 'sept',
32  8: 'huit',
33  9: 'neuf',
34  10: 'dix',
35  11: 'onze',
36  12: 'douze',
37  13: 'treize',
38  14: 'quatorze',
39  15: 'quinze',
40  16: 'seize',
41  20: 'vingt',
42  30: 'trente',
43  40: 'quarante',
44  50: 'cinquante',
45  60: 'soixante',
46  70: 'soixante-dix',
47  80: 'quatre-vingt',
48  90: 'quatre-vingt-dix'
49 }
50 
51 FRACTION_STRING_FR = {
52  2: 'demi',
53  3: 'tiers',
54  4: 'quart',
55  5: 'cinquième',
56  6: 'sixième',
57  7: 'septième',
58  8: 'huitième',
59  9: 'neuvième',
60  10: 'dixième',
61  11: 'onzième',
62  12: 'douzième',
63  13: 'treizième',
64  14: 'quatorzième',
65  15: 'quinzième',
66  16: 'seizième',
67  17: 'dix-septième',
68  18: 'dix-huitième',
69  19: 'dix-neuvième',
70  20: 'vingtième'
71 }
72 
73 
74 def nice_number_fr(number, speech, denominators):
75  """ French helper for nice_number
76 
77  This function formats a float to human understandable functions. Like
78  4.5 becomes "4 et demi" for speech and "4 1/2" for text
79 
80  Args:
81  number (int or float): the float to format
82  speech (bool): format for speech (True) or display (False)
83  denominators (iter of ints): denominators to use, default [1 .. 20]
84  Returns:
85  (str): The formatted string.
86  """
87  strNumber = ""
88  whole = 0
89  num = 0
90  den = 0
91 
92  result = convert_to_mixed_fraction(number, denominators)
93 
94  if not result:
95  # Give up, just represent as a 3 decimal number
96  whole = round(number, 3)
97  else:
98  whole, num, den = result
99 
100  if not speech:
101  if num == 0:
102  strNumber = '{:,}'.format(whole)
103  strNumber = strNumber.replace(",", " ")
104  strNumber = strNumber.replace(".", ",")
105  return strNumber
106  else:
107  return '{} {}/{}'.format(whole, num, den)
108  else:
109  if num == 0:
110  # if the number is not a fraction, nothing to do
111  strNumber = str(whole)
112  strNumber = strNumber.replace(".", ",")
113  return strNumber
114  den_str = FRACTION_STRING_FR[den]
115  # if it is not an integer
116  if whole == 0:
117  # if there is no whole number
118  if num == 1:
119  # if numerator is 1, return "un demi", for example
120  strNumber = 'un {}'.format(den_str)
121  else:
122  # else return "quatre tiers", for example
123  strNumber = '{} {}'.format(num, den_str)
124  elif num == 1:
125  # if there is a whole number and numerator is 1
126  if den == 2:
127  # if denominator is 2, return "1 et demi", for example
128  strNumber = '{} et {}'.format(whole, den_str)
129  else:
130  # else return "1 et 1 tiers", for example
131  strNumber = '{} et 1 {}'.format(whole, den_str)
132  else:
133  # else return "2 et 3 quart", for example
134  strNumber = '{} et {} {}'.format(whole, num, den_str)
135  if num > 1 and den != 3:
136  # if the numerator is greater than 1 and the denominator
137  # is not 3 ("tiers"), add an s for plural
138  strNumber += 's'
139 
140  return strNumber
141 
142 
143 def pronounce_number_fr(num, places=2):
144  """
145  Convert a number to it's spoken equivalent
146 
147  For example, '5.2' would return 'cinq virgule deux'
148 
149  Args:
150  num(float or int): the number to pronounce (under 100)
151  places(int): maximum decimal places to speak
152  Returns:
153  (str): The pronounced number
154  """
155  if abs(num) >= 100:
156  # TODO: Support for numbers over 100
157  return str(num)
158 
159  result = ""
160  if num < 0:
161  result = "moins "
162  num = abs(num)
163 
164  if num > 16:
165  tens = int(num-int(num) % 10)
166  ones = int(num-tens)
167  if ones != 0:
168  if tens > 10 and tens <= 60 and int(num-tens) == 1:
169  result += NUM_STRING_FR[tens] + "-et-" + NUM_STRING_FR[ones]
170  elif num == 71:
171  result += "soixante-et-onze"
172  elif tens == 70:
173  result += NUM_STRING_FR[60] + "-"
174  if ones < 7:
175  result += NUM_STRING_FR[10 + ones]
176  else:
177  result += NUM_STRING_FR[10] + "-" + NUM_STRING_FR[ones]
178  elif tens == 90:
179  result += NUM_STRING_FR[80] + "-"
180  if ones < 7:
181  result += NUM_STRING_FR[10 + ones]
182  else:
183  result += NUM_STRING_FR[10] + "-" + NUM_STRING_FR[ones]
184  else:
185  result += NUM_STRING_FR[tens] + "-" + NUM_STRING_FR[ones]
186  else:
187  if num == 80:
188  result += "quatre-vingts"
189  else:
190  result += NUM_STRING_FR[tens]
191  else:
192  result += NUM_STRING_FR[int(num)]
193 
194  # Deal with decimal part
195  if not num == int(num) and places > 0:
196  result += " virgule"
197  place = 10
198  while int(num*place) % 10 > 0 and places > 0:
199  result += " " + NUM_STRING_FR[int(num*place) % 10]
200  place *= 10
201  places -= 1
202  return result
203 
204 
205 def nice_time_fr(dt, speech=True, use_24hour=False, use_ampm=False):
206  """
207  Format a time to a comfortable human format
208 
209  For example, generate 'cinq heures trente' for speech or '5:30' for
210  text display.
211 
212  Args:
213  dt (datetime): date to format (assumes already in local timezone)
214  speech (bool): format for speech (default/True) or display (False)=Fal
215  use_24hour (bool): output in 24-hour/military or 12-hour format
216  use_ampm (bool): include the am/pm for 12-hour format
217  Returns:
218  (str): The formatted time string
219  """
220  if use_24hour:
221  # e.g. "03:01" or "14:22"
222  string = dt.strftime("%H:%M")
223  else:
224  if use_ampm:
225  # e.g. "3:01 AM" or "2:22 PM"
226  string = dt.strftime("%I:%M %p")
227  else:
228  # e.g. "3:01" or "2:22"
229  string = dt.strftime("%I:%M")
230  if string[0] == '0':
231  string = string[1:] # strip leading zeros
232 
233  if not speech:
234  return string
235 
236  # Generate a speakable version of the time
237  speak = ""
238  if use_24hour:
239 
240  # "13 heures trente"
241  if dt.hour == 0:
242  speak += "minuit"
243  elif dt.hour == 12:
244  speak += "midi"
245  elif dt.hour == 1:
246  speak += "une heure"
247  else:
248  speak += pronounce_number_fr(dt.hour) + " heures"
249 
250  if dt.minute != 0:
251  speak += " " + pronounce_number_fr(dt.minute)
252 
253  else:
254  # Prepare for "trois heures moins le quart"
255  if dt.minute == 35:
256  minute = -25
257  hour = dt.hour + 1
258  elif dt.minute == 40:
259  minute = -20
260  hour = dt.hour + 1
261  elif dt.minute == 45:
262  minute = -15
263  hour = dt.hour + 1
264  elif dt.minute == 50:
265  minute = -10
266  hour = dt.hour + 1
267  elif dt.minute == 55:
268  minute = -5
269  hour = dt.hour + 1
270  else:
271  minute = dt.minute
272  hour = dt.hour
273 
274  if hour == 0:
275  speak += "minuit"
276  elif hour == 12:
277  speak += "midi"
278  elif hour == 1 or hour == 13:
279  speak += "une heure"
280  elif hour < 13:
281  speak = pronounce_number_fr(hour) + " heures"
282  else:
283  speak = pronounce_number_fr(hour-12) + " heures"
284 
285  if minute != 0:
286  if minute == 15:
287  speak += " et quart"
288  elif minute == 30:
289  speak += " et demi"
290  elif minute == -15:
291  speak += " moins le quart"
292  else:
293  speak += " " + pronounce_number_fr(minute)
294 
295  if use_ampm:
296  if hour > 17:
297  speak += " du soir"
298  elif hour > 12:
299  speak += " de l'après-midi"
300  elif hour > 0 and hour < 12:
301  speak += " du matin"
302 
303  return speak
def nice_time_fr(dt, speech=True, use_24hour=False, use_ampm=False)
Definition: format_fr.py:205
def pronounce_number_fr(num, places=2)
Definition: format_fr.py:143
def convert_to_mixed_fraction(number, denominators)
def nice_number_fr(number, speech, denominators)
Definition: format_fr.py:74


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