unit_converter.py 5.0 KB

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