Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum multipliers {
- thousand("thousand", 1000), hundred("hundred", 100);
- private String str;
- private int value;
- multipliers(String str, int value) {
- this.str = str;
- this.value = value;
- }
- }
- Map<String, Integer> nums = new LinkedHashMap<String, Integer>(){{
- put("ten", 10);
- put("eleven", 11);
- put("twelve", 12);
- put("one", 1);
- put("tw", 2);
- put("th", 3);
- put("fo", 4);
- put("fi", 5);
- put("si", 6);
- put("seven", 7);
- put("eight", 8);
- put("nine", 9);
- }};
- @Test
- public void testTestRecognition() {
- List<String> text_8599 = new ArrayList<>(Arrays.asList("eight", "thousand", "five", "hundred", "ninety", "nine"));
- List<String> text_1912 = new ArrayList<>(Arrays.asList("one", "thousand", "nine", "hundred", "twelve"));
- List<String> text_1911a = new ArrayList<>(Arrays.asList("one", "thousand", "nine", "hundred", "and", "eleven"));
- List<String> text_1337 = new ArrayList<>(Arrays.asList("one", "thousand", "three", "hundred", "thirty", "seven"));
- List<String> text_1500 = new ArrayList<>(Arrays.asList("one", "thousand", "five", "hundred"));
- List<String> text_1012 = new ArrayList<>(Arrays.asList("one", "thousand", "twelve"));
- List<String> text_1011 = new ArrayList<>(Arrays.asList("one", "thousand", "eleven"));
- List<String> text_1010 = new ArrayList<>(Arrays.asList("one", "thousand", "ten"));
- List<String> text_1001 = new ArrayList<>(Arrays.asList("one", "thousand", "one"));
- List<String> text_1001a = new ArrayList<>(Arrays.asList("one", "thousand", "and", "one"));
- List<String> text_300 = new ArrayList<>(Arrays.asList("three", "hundred"));
- List<String> text_228 = new ArrayList<>(Arrays.asList("two", "hundred", "twenty", "eight"));
- List<String> text_42 = new ArrayList<>(Arrays.asList("forty", "two"));
- List<String> text_12 = new ArrayList<>(Arrays.asList("twelve"));
- List<String> text_11 = new ArrayList<>(Arrays.asList("eleven"));
- List<String> text_10 = new ArrayList<>(Arrays.asList("ten"));
- Assert.assertEquals(8599, recogAll(text_8599));
- Assert.assertEquals(1912, recogAll(text_1912));
- Assert.assertEquals(1911, recogAll(text_1911a));
- Assert.assertEquals(1337, recogAll(text_1337));
- Assert.assertEquals(1500, recogAll(text_1500));
- Assert.assertEquals(1012, recogAll(text_1012));
- Assert.assertEquals(1011, recogAll(text_1011));
- Assert.assertEquals(1010, recogAll(text_1010));
- Assert.assertEquals(1001, recogAll(text_1001));
- Assert.assertEquals(1001, recogAll(text_1001a));
- Assert.assertEquals(300, recogAll(text_300));
- Assert.assertEquals(228, recogAll(text_228));
- Assert.assertEquals(42, recogAll(text_42));
- Assert.assertEquals(12, recogAll(text_12));
- Assert.assertEquals(11, recogAll(text_11));
- Assert.assertEquals(10, recogAll(text_10));
- }
- private int recogAll(List<String> text) {
- List<String> queue = new ArrayList<>();
- int ret = 0;
- for (String t : text) {
- if (t.equals(multipliers.thousand.str)) {
- //accumulate digits to the left from "thousand". convert to number
- ret += recognizeNumber(queue) * multipliers.thousand.value;
- //process and clear
- queue.clear();
- } else if (t.equals(multipliers.hundred.str)) {
- ret += recognizeNumber(queue) * multipliers.hundred.value;
- queue.clear();
- } else {
- queue.add(t);
- }
- }
- ret += recognizeNumber(queue);
- return ret;
- }
- private int recognizeNumber(List<String> queue) {
- int res = 0;
- List<String> tmp = new ArrayList<String>(){{
- addAll(queue);
- }};
- Collections.reverse(tmp);
- int multiplier = 1; //increases as we move to the higher order
- for (String digit : tmp) {
- if (digit.endsWith("teen")) {
- res += 10 + recognizeDigit(digit) ;
- } else {
- //else first digit represents decades and second is remainder
- res += multiplier*recognizeDigit(digit);
- multiplier *= 10;
- }
- }
- return res;
- }
- private int recognizeDigit(String digit) {
- for (String v : nums.keySet()) {
- if (digit.startsWith(v)) {
- return nums.get(v);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment