def handleExponents(input): m = re.search(r'\bsquare (\w+)', input) if m and self.isValidUnit(m.group(1)): input = re.sub(r'\bsquare (\w+)', r'\g<1>^2', input) m = re.search(r'\bsquared (\w+)', input) if m and self.isValidUnit(m.group(1)): input = re.sub(r'\bsquared (\w+)', r'\g<1>^2', input) m = re.search(r'\b(\w+) squared', input) if m and self.isValidUnit(m.group(1)): input = re.sub(r'\b(\w+) squared', r'\g<1>^2', input) m = re.search(r'\bsq (\w+)', input) if m and self.isValidUnit(m.group(1)): input = re.sub(r'\bsq (\w+)', r'\g<1>^2', input) m = re.search(r'\b(\w+) cubed', input) if m and self.isValidUnit(m.group(1)): input = re.sub(r'\b(\w+) cubed', r'\g<1>^3', input) m = re.search(r'\bcubic (\w+)', input) if m and self.isValidUnit(m.group(1)): input = re.sub(r'\bcubic (\w+)', r'\g<1>^3', input) service = NumberService() m = re.search(r'\b(\w+) to the (\w+)( power)?', input) if m and self.isValidUnit(m.group(1)): if m.group(2) in service.__ordinals__: exp = service.parseMagnitude(m.group(2)) input = re.sub(r'\b(\w+) to the (\w+)( power)?', r'\g<1>^' + str(exp), input) return input
def parseUnits(self, input): """Carries out a conversion (represented as a string) and returns the result as a human-readable string. Args: input (str): Text representing a unit conversion, which should include a magnitude, a description of the initial units, and a description of the target units to which the quantity should be converted. Returns: A quantities object representing the converted quantity and its new units. """ quantity = self.convert(input) units = ' '.join(str(quantity.units).split(' ')[1:]) return NumberService.parseMagnitude(quantity.item()) + " " + units