unit_converter.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import re
  2. class UnitConverter:
  3. """
  4. Utility class to convert culinary volumetric units to metric weight (grams)
  5. based on the specific product density.
  6. """
  7. # Common culinary volumetric units and their approximate volume in milliliters (ml)
  8. VOLUME_UNITS_ML = {
  9. 'tsp': 5.0,
  10. 'teaspoon': 5.0,
  11. 'tbsp': 15.0,
  12. 'tablespoon': 15.0,
  13. 'cup': 240.0,
  14. 'fl oz': 30.0,
  15. 'fluid ounce': 30.0,
  16. 'pint': 473.0,
  17. 'quart': 946.0,
  18. 'gallon': 3785.0,
  19. 'cm3': 1.0,
  20. 'cl': 10.0,
  21. 'dl': 100.0,
  22. 'l': 1000.0,
  23. 'liter': 1000.0,
  24. 'pinch': 0.36, # rough estimate
  25. 'dash': 0.72,
  26. 'xl': 64.0,
  27. 'l': 50.0,
  28. 'm': 44.0,
  29. 's': 38.0,
  30. 'extra': 64.0,
  31. 'extra large': 64.0,
  32. 'big': 64.0,
  33. 'b': 64.0,
  34. 'large': 50.0,
  35. 'medium': 44.0,
  36. 'small': 38.0,
  37. }
  38. # Densities in grams per milliliter (g/ml)
  39. PRODUCT_DENSITIES = {
  40. # Baking and flours
  41. 'flour': 0.53,
  42. 'all-purpose flour': 0.53,
  43. 'wheat flour': 0.53,
  44. 'sugar': 0.85,
  45. 'white sugar': 0.85,
  46. 'granulated sugar': 0.85,
  47. 'powdered sugar': 0.50,
  48. 'icing sugar': 0.50,
  49. 'brown sugar': 0.83,
  50. 'salt': 1.20,
  51. 'table salt': 1.20,
  52. 'baking powder': 0.90,
  53. 'baking soda': 1.10,
  54. 'cocoa powder': 0.42,
  55. # Liquids
  56. 'water': 1.0,
  57. 'milk': 1.03,
  58. 'heavy cream': 0.99,
  59. 'vegetable oil': 0.92,
  60. 'olive oil': 0.92,
  61. 'honey': 1.42,
  62. 'maple syrup': 1.32,
  63. 'butter': 0.96, # melted or solid approx
  64. 'melted butter': 0.94,
  65. # Grains and dry goods
  66. 'rice': 0.85,
  67. 'white rice': 0.85,
  68. 'oats': 0.38,
  69. 'rolled oats': 0.38,
  70. 'quinoa': 0.72,
  71. 'couscous': 0.72,
  72. 'lentils': 0.85,
  73. # Condiments
  74. 'ketchup': 1.15,
  75. 'mustard': 1.05,
  76. 'mayonnaise': 0.95,
  77. 'peanut butter': 1.08,
  78. # Default density for unknown items (approximate density of water/mixed food)
  79. 'default': 1.0
  80. }
  81. # Direct weight conversions (already in weight, just need unit conversion)
  82. WEIGHT_UNITS_G = {
  83. 'g': 1.0,
  84. 'gram': 1.0,
  85. 'kg': 1000.0,
  86. 'kilo': 1000.0,
  87. 'kilogram': 1000.0,
  88. 'oz': 28.35,
  89. 'ounce': 28.35,
  90. 'lb': 453.59,
  91. 'pound': 453.59,
  92. 'mg': 0.001
  93. }
  94. @classmethod
  95. def get_density(cls, product_name):
  96. if not product_name:
  97. return cls.PRODUCT_DENSITIES['default']
  98. product_name = str(product_name).lower().strip()
  99. # Exact match
  100. if product_name in cls.PRODUCT_DENSITIES:
  101. return cls.PRODUCT_DENSITIES[product_name]
  102. # Partial match
  103. for key, density in cls.PRODUCT_DENSITIES.items():
  104. if key in product_name:
  105. return density
  106. return cls.PRODUCT_DENSITIES['default']
  107. @classmethod
  108. def convert_to_grams(cls, amount, unit, product_name=None):
  109. """
  110. Converts an amount and unit of a specific product to grams.
  111. """
  112. unit = str(unit).lower().strip()
  113. # If it's already a weight unit, simple scalar conversion
  114. for w_unit, g_factor in cls.WEIGHT_UNITS_G.items():
  115. if unit == w_unit or unit == f"{w_unit}s":
  116. return amount * g_factor
  117. # If it's a volumetric unit, use density
  118. volume_ml = None
  119. for v_unit, ml_factor in cls.VOLUME_UNITS_ML.items():
  120. if unit == v_unit or unit == f"{v_unit}s":
  121. volume_ml = amount * ml_factor
  122. break
  123. if volume_ml is not None:
  124. density = cls.get_density(product_name)
  125. return volume_ml * density
  126. # Unrecognized unit
  127. return None
  128. @classmethod
  129. def parse_and_convert(cls, recipe_string, product_name=None):
  130. """
  131. Parses a string like "1.5 cups" or "2 tbsp" and converts to grams.
  132. """
  133. # Match number (including decimals/fractions roughly) followed by text
  134. match = re.match(r'^([\d\.]+)\s*([a-zA-Z\s]+)$', str(recipe_string).strip())
  135. if match:
  136. try:
  137. amount = float(match.group(1))
  138. unit = match.group(2).strip()
  139. result = cls.convert_to_grams(amount, unit, product_name)
  140. if result is not None:
  141. return round(result, 2)
  142. except ValueError:
  143. pass
  144. return None
  145. if __name__ == '__main__':
  146. # Tests
  147. print("1 cup of all-purpose flour:", UnitConverter.parse_and_convert("1 cup", "all-purpose flour"), "g")
  148. print("1 cup of white sugar:", UnitConverter.parse_and_convert("1 cup", "white sugar"), "g")
  149. print("1 cup of water:", UnitConverter.parse_and_convert("1 cup", "water"), "g")
  150. print("2 tbsp of olive oil:", UnitConverter.parse_and_convert("2 tbsp", "olive oil"), "g")
  151. print("1 pound of generic food:", UnitConverter.parse_and_convert("1 pound", "unknown"), "g")
  152. print("1 pinch of salt:", UnitConverter.parse_and_convert("1 pinch", "salt"), "g")
  153. print("1 xl egg:", UnitConverter.parse_and_convert("1 xl", "egg"), "g")
  154. print("2 large eggs:", UnitConverter.parse_and_convert("2 large", "egg"), "g")