1
0

unit_converter.py 5.3 KB

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