1. ubershmekel
  2. metricbot

Commits

ubershmekel  committed 1d7a93c

Disabled ounce conversion because of the volume/weight context.

Added foot-inch support (eg 5'2").

  • Participants
  • Parent commits 51e8d91
  • Branches default

Comments (0)

Files changed (3)

File TODO.txt

View file
 
 When someone is translating the imperial units to metric units - don't be redundant.
 http://www.reddit.com/r/science/comments/pptts/worlds_tiniest_chameleon_discovered/c3rfi4k?context=3
+http://www.reddit.com/r/cars/comments/pq7fj/please_justify_the_use_of_radar_detectors_to_me/c3rhvpt?context=3
+
 
 A number after a "foot" is probably inches:
 http://www.reddit.com/r/nba/comments/pq3vw/jeremy_lin_hits_game_winner_against_raptors_right/c3rfp7x?context=3
 
+avoid a subreddit
+http://www.reddit.com/r/weightroom/comments/pqqmo/back_roundingcaving_on_high_bar_squats_what_do/c3rhkvx?context=3

File metricbot.py

View file
 import math
 import decimal
 import time
+import itertools
 
 import quantities
 from redditbot import RedditBot
     'miles': quantities.mile,
     'mile': quantities.mile,
     'mph': MPH,
-    'oz': quantities.oz,
-    'ounce': quantities.oz,
-    'ounces': quantities.oz,
+    #'oz': quantities.oz,
+    #'ounce': quantities.oz,
+    #'ounces': quantities.oz, # How do I know if it's ml or mg?
 }
 
 SI_UNITS = {
     else:
         return unit.name + 's'
 
-def find_imperials(text):
+def number_unit_conversions(text):
     number_word_pairs = re.findall(r'([/\d,\.]+)[ ]?([a-zA-Z\-/]+)', text)
-    logging.info
-    conversions = []
-    seen = set()
     for number, word in number_word_pairs:
         if word in IMP_UNITS:
             no_commas_number = number.replace(',', '')
             converted = (amount * unit).rescale(si_unit)
             converted = float(converted)
             num_str = nice_number_str(converted)
-            original_pair = (amount, unit)
-            if original_pair in seen:
-                continue
-            else:
-                seen.add(original_pair)
-            was = '%s %s' % (number, word)
-            now = '%s %s' % (num_str, nice_unit_name(si_unit))
-            line = '%s = %s' % (was, now)
-            conversions.append(line)
+            
+            src = '%s %s' % (number, word)
+            dst = '%s %s' % (num_str, nice_unit_name(si_unit))
+            yield src, dst
+
+def foot_inch_apostrophe_conversions(text):
+    for feet, inches in re.findall(r'''(\d+)'(\d+)"''', text):
+        try:
+            feet = int(feet)
+            inches = int(inches)
+            amount = quantities.ft * feet + quantities.inch * inches
+            result = amount.rescale(quantities.m)
+            result = float(result)
+            num_str = nice_number_str(result)
+            
+            src = '''%d'%d"''' % (feet, inches)
+            dst = '%s %s' % (num_str, 'meters')
+            
+            yield src, dst
+            
+        except ValueError:
+            # bad number eg '0.,0'
+            continue
+            
+
+def find_imperials(text):
+    conversions = []
+    seen = set()
+    for src, dst in itertools.chain(number_unit_conversions(text), foot_inch_apostrophe_conversions(text)):
+        if dst in seen:
+            continue
+        else:
+            seen.add(dst)
+        line = '%s = %s' % (src, dst)
+        conversions.append(line)
     
     return conversions
 

File test_metricbot.py

View file
            'my 1 gallon friend': '1 gallon = 3.79 liters',
            '-0.1 mph': '0.1 mph = 0.16 km/h',
            '1 feet 1 ft 1foot': '1 feet = 0.3 meters',
+           '''I'm 6'3" 223lbs, so I avoid a lot of confrontations''': '''223 lbs = 101.15 kilograms\n\n6\'3" = 1.91 meters''',
            }
         
         for text, expected in pairs.items():
-            res = metricbot.find_imperials(text)[0]
+            res = metricbot.metric_response(text)
             self.assertEqual(expected, res)        
     
     def test_no_units(self):
             '',
             'laksjdflkajdsfjalsdf32482342j34lkjkjlad43243',
             ]
-        expected = []
+        expected = ''
         for text in dont_detect:
-            res = metricbot.find_imperials(text)
+            res = metricbot.metric_response(text)
             self.assertEqual(expected, res)
         
 if __name__ == '__main__':