Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def unit_to_spoken(unit, count=1):
"""
Convert a given unit to the unit in words, correctly inflected.
:param unit: The unit to be converted
:param count: The value of the quantity (i.e. 1 for one watt, 2 for two
seconds)
:return: A string with the correctly inflected spoken version of the unit
"""
if unit.surfaces:
unit_string = unit.surfaces[0]
unit_string = load.pluralize(unit_string, count)
else:
# derived unit
denominator_dimensions = [i for i in unit.dimensions if i["power"] > 0]
denominator_string = parser.name_from_dimensions(denominator_dimensions, lang)
plural_denominator_string = load.pluralize(denominator_string)
unit_string = unit.name.replace(denominator_string, plural_denominator_string)
return unit_string
def build_common_words():
# Read raw 4 letter file
path = os.path.join(TOPDIR, "common-words.txt")
words = defaultdict(list) # Collect words based on length
with open(path, "r", encoding="utf-8") as file:
for line in file:
if line.startswith("#"):
continue
line = line.rstrip()
if (
line not in load.units(lang).surfaces_all
and line not in load.units(lang).symbols
):
words[len(line)].append(line)
plural = load.pluralize(line)
if (
plural not in load.units(lang).surfaces_all
and plural not in load.units(lang).symbols
):
words[len(plural)].append(plural)
return words
Convert number words to integers in a given text.
"""
numwords = {}
numwords.update(miscnum(lang))
for idx, word in enumerate(units(lang)):
numwords[word] = (1, idx)
for idx, word in enumerate(tens(lang)):
numwords[word] = (1, idx * 10)
for idx, word in enumerate(scales(lang)):
numwords[word] = (10 ** (idx * 3 or 2), 0)
for word, factor in decimals(lang).items():
numwords[word] = (factor, 0)
numwords[load.pluralize(word, lang=lang)] = (factor, 0)
return numwords
def unit_to_spoken(unit, count=1):
"""
Convert a given unit to the unit in words, correctly inflected.
:param unit: The unit to be converted
:param count: The value of the quantity (i.e. 1 for one watt, 2 for two
seconds)
:return: A string with the correctly inflected spoken version of the unit
"""
if unit.surfaces:
unit_string = unit.surfaces[0]
unit_string = load.pluralize(unit_string, count)
else:
# derived unit
denominator_dimensions = [i for i in unit.dimensions if i["power"] > 0]
denominator_string = parser.name_from_dimensions(denominator_dimensions, lang)
plural_denominator_string = load.pluralize(denominator_string)
unit_string = unit.name.replace(denominator_string, plural_denominator_string)
return unit_string