Ladies_Man

recog number

Jul 17th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. enum multipliers {
  2.         thousand("thousand", 1000), hundred("hundred", 100);
  3.  
  4.         private String str;
  5.         private int value;
  6.  
  7.         multipliers(String str, int value) {
  8.             this.str = str;
  9.             this.value = value;
  10.         }
  11.     }
  12.  
  13.     Map<String, Integer> nums = new LinkedHashMap<String, Integer>(){{
  14.         put("ten", 10);
  15.         put("eleven", 11);
  16.         put("twelve", 12);
  17.         put("one", 1);
  18.         put("tw", 2);
  19.         put("th", 3);
  20.         put("fo", 4);
  21.         put("fi", 5);
  22.         put("si", 6);
  23.         put("seven", 7);
  24.         put("eight", 8);
  25.         put("nine", 9);
  26.     }};
  27.  
  28.     @Test
  29.     public void testTestRecognition() {
  30.         List<String> text_8599 = new ArrayList<>(Arrays.asList("eight", "thousand", "five", "hundred", "ninety", "nine"));
  31.         List<String> text_1912 = new ArrayList<>(Arrays.asList("one", "thousand", "nine", "hundred", "twelve"));
  32.         List<String> text_1911a = new ArrayList<>(Arrays.asList("one", "thousand", "nine", "hundred", "and", "eleven"));
  33.         List<String> text_1337 = new ArrayList<>(Arrays.asList("one", "thousand", "three", "hundred", "thirty", "seven"));
  34.         List<String> text_1500 = new ArrayList<>(Arrays.asList("one", "thousand", "five", "hundred"));
  35.         List<String> text_1012 = new ArrayList<>(Arrays.asList("one", "thousand", "twelve"));
  36.         List<String> text_1011 = new ArrayList<>(Arrays.asList("one", "thousand", "eleven"));
  37.         List<String> text_1010 = new ArrayList<>(Arrays.asList("one", "thousand", "ten"));
  38.         List<String> text_1001 = new ArrayList<>(Arrays.asList("one", "thousand", "one"));
  39.         List<String> text_1001a = new ArrayList<>(Arrays.asList("one", "thousand", "and", "one"));
  40.         List<String> text_300 = new ArrayList<>(Arrays.asList("three", "hundred"));
  41.         List<String> text_228 = new ArrayList<>(Arrays.asList("two", "hundred", "twenty", "eight"));
  42.         List<String> text_42 = new ArrayList<>(Arrays.asList("forty", "two"));
  43.         List<String> text_12 = new ArrayList<>(Arrays.asList("twelve"));
  44.         List<String> text_11 = new ArrayList<>(Arrays.asList("eleven"));
  45.         List<String> text_10 = new ArrayList<>(Arrays.asList("ten"));
  46.  
  47.         Assert.assertEquals(8599, recogAll(text_8599));
  48.         Assert.assertEquals(1912, recogAll(text_1912));
  49.         Assert.assertEquals(1911, recogAll(text_1911a));
  50.         Assert.assertEquals(1337, recogAll(text_1337));
  51.         Assert.assertEquals(1500, recogAll(text_1500));
  52.         Assert.assertEquals(1012, recogAll(text_1012));
  53.         Assert.assertEquals(1011, recogAll(text_1011));
  54.         Assert.assertEquals(1010, recogAll(text_1010));
  55.         Assert.assertEquals(1001, recogAll(text_1001));
  56.         Assert.assertEquals(1001, recogAll(text_1001a));
  57.         Assert.assertEquals(300, recogAll(text_300));
  58.         Assert.assertEquals(228, recogAll(text_228));
  59.         Assert.assertEquals(42, recogAll(text_42));
  60.         Assert.assertEquals(12, recogAll(text_12));
  61.         Assert.assertEquals(11, recogAll(text_11));
  62.         Assert.assertEquals(10, recogAll(text_10));
  63.     }
  64.  
  65.  
  66.     private int recogAll(List<String> text) {
  67.  
  68.         List<String> queue = new ArrayList<>();
  69.         int ret = 0;
  70.         for (String t : text) {
  71.  
  72.             if (t.equals(multipliers.thousand.str)) {
  73.                 //accumulate digits to the left from "thousand". convert to number
  74.                 ret += recognizeNumber(queue) * multipliers.thousand.value;
  75.                 //process and clear
  76.                 queue.clear();
  77.  
  78.             } else if (t.equals(multipliers.hundred.str)) {
  79.  
  80.                 ret += recognizeNumber(queue) * multipliers.hundred.value;
  81.                 queue.clear();
  82.  
  83.             } else {
  84.                 queue.add(t);
  85.             }
  86.         }
  87.         ret += recognizeNumber(queue);
  88.  
  89.         return ret;
  90.     }
  91.  
  92.     private int recognizeNumber(List<String> queue) {
  93.         int res = 0;
  94.         List<String> tmp = new ArrayList<String>(){{
  95.             addAll(queue);
  96.         }};
  97.  
  98.         Collections.reverse(tmp);
  99.  
  100.         int multiplier = 1;     //increases as we move to the higher order
  101.         for (String digit : tmp) {
  102.  
  103.             if (digit.endsWith("teen")) {
  104.                 res += 10 + recognizeDigit(digit) ;
  105.             } else {
  106.  
  107.                 //else first digit represents decades and second is remainder
  108.                 res += multiplier*recognizeDigit(digit);
  109.                 multiplier *= 10;
  110.             }
  111.         }
  112.         return res;
  113.     }
  114.  
  115.     private int recognizeDigit(String digit) {
  116.         for (String v : nums.keySet()) {
  117.             if (digit.startsWith(v)) {
  118.                 return nums.get(v);
  119.             }
  120.         }
  121.         return 0;
  122.     }
Advertisement
Add Comment
Please, Sign In to add comment