From 0d229cc43a7e29bdd52cfadfebf2f502bb523d86 Mon Sep 17 00:00:00 2001 From: saumittra saxena Date: Wed, 20 Dec 2023 16:37:52 +0530 Subject: [PATCH] YugaV1.60 || Introduced Rates || trailing curr to amt --- pom.xml | 4 +- src/main/java/com/twelfthmile/yuga/Yuga.java | 57 +++++++++++++------ .../com/twelfthmile/yuga/YugaMethods.java | 29 +++++++++- .../com/twelfthmile/yuga/utils/Constants.java | 5 +- src/test/resources/yuga_tests.json | 20 +++++++ 5 files changed, 93 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 3c23e3c..0abc14e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 in.messai yuga - 1.0.59 + 1.0.60 Yuga Date Parser @@ -188,7 +188,7 @@ scm:git:git@github.com:messai-engineering/Yuga.git https://github.com/messai-engineering/Yuga - yuga-1.0.59 + yuga-1.0.60 diff --git a/src/main/java/com/twelfthmile/yuga/Yuga.java b/src/main/java/com/twelfthmile/yuga/Yuga.java index 7322280..b3b1ad9 100644 --- a/src/main/java/com/twelfthmile/yuga/Yuga.java +++ b/src/main/java/com/twelfthmile/yuga/Yuga.java @@ -153,7 +153,9 @@ public static Response parse(String str) { private static Pair prepareResult(String str, Pair p, Map config) { int index = p.getA(); FsaContextMap map = p.getB(); - if (map.getType().equals(Constants.TY_DTE)) { + if (map.getType().equals(Constants.TY_RATE)){ + return new Pair<>(Constants.TY_RATE, str.substring(0, index)); + } else if (map.getType().equals(Constants.TY_DTE)) { if (map.contains(Constants.DT_MMM) && map.size() < 3)//may fix return new Pair<>(Constants.TY_STR, str.substring(0, index)); if (map.contains(Constants.DT_HH) && map.contains(Constants.DT_mm) && !map.contains(Constants.DT_D) && !map.contains(Constants.DT_DD) && !map.contains(Constants.DT_MM) && !map.contains(Constants.DT_MMM) && !map.contains(Constants.DT_YY) && !map.contains(Constants.DT_YYYY)) { @@ -1151,6 +1153,25 @@ else if (str.charAt(j) == 'x' && ((j + 1) == str.length() || ((j + 1) < str.leng map.setType(Constants.TY_MLT, Constants.TY_MLT); map.append(str.substring(i,j+1)); i = j; + } else if(str.charAt(j) == '/') { + j++; + String ahead = str.substring(j); + if(ahead.length() >= 2 && ahead.substring(0,2).equalsIgnoreCase("km")){ + map.setType(Constants.TY_RATE, Constants.TY_RATE); + map.getValMap().put("per",Constants.TY_DIST); + i = j + 2; + } + Response r = getResponse(ahead, config); + if(r != null) { + String type = r.getType(); + if (type.equals(Constants.TY_NUM_MINS) || type.equals(Constants.TY_TME) || type.equals(Constants.TY_WGT) || type.equals(Constants.TY_DTA)) { + map.setType(Constants.TY_RATE, Constants.TY_RATE); + map.getValMap().putAll(r.getValMap()); + map.getValMap().put("per", r.getType()); + i = j + r.getStr().length(); + + } + } } } } @@ -1162,6 +1183,18 @@ else if (str.charAt(j) == 'x' && ((j + 1) == str.length() || ((j + 1) < str.leng if(k < str.length() && ((str.charAt(k) == 'k' || str.charAt(k) == 'm' || str.charAt(k) == 'g') && (k + 1) < str.length() && str.charAt(k + 1) == 'b')) { checkIfData(str, k, map); i = k + 2; + } else if(map.get("NUM").length()<3 && k+3 < str.length() && str.substring(k,k+3).equalsIgnoreCase("min")){ + i = k + 4; + map.setType(Constants.TY_NUM_MINS); + map.getValMap().put("minutes_num",map.get("NUM")); + } else if(!configContextIsCURR(config) && YugaMethods.isCurrencyAhead(str.substring(k))) { + map.setType(Constants.TY_AMT, Constants.TY_AMT); + map.getValMap().put("currency",YugaMethods.getPotentialCurrString(str.substring(k))); + i = k + 3; + } else if(k+33 && (sub.charAt(0)=='-' || sub.charAt(0)=='x')){ - int del = nextSpace(sub); + int del = YugaMethods.nextSpace(sub); if(Util.isNumber(sub.substring(1))){ map.append(sub); map.setType(Constants.TY_NUM, Constants.TY_NUM); @@ -1445,16 +1478,6 @@ private static int skip(String str) { return i; } - private static int nextSpace(String str) { - int i = 0; - while (i < str.length()) { - if (str.charAt(i) == ' ') - return i; - else - i++; - } - return i; - } private static boolean checkForAlphaAfterComma(String str, int i) { while(i < str.length()) { char c = str.charAt(i); @@ -1477,8 +1500,8 @@ private static int accAmtNumPct(String str, int i, FsaContextMap map, Map10 && str.charAt(i+1)!= Constants.CH_STAR && nextSpace(subStr)-i>12){ - int nextSpace = nextSpace(subStr); + } else if(c == Constants.CH_STAR && subStr.length()>10 && str.charAt(i+1)!= Constants.CH_STAR && YugaMethods.nextSpace(subStr)-i>12){ + int nextSpace = YugaMethods.nextSpace(subStr); String code = Util.getcallFrwrdCode(str, i); if(Constants.callForwardCode.contains(code)){ map.setType(Constants.TY_CALLFORWARD); @@ -1544,10 +1567,12 @@ private static int getPrevState(ArrayList prevStates) { public static String getYugaResponseOutput(String str,Map config,boolean isTime) { Response r = getResponse(str, config); - if(isTime) + if(isTime && r!=null) return r.getValMap().get("time"); - else + else if(r!=null) return r.getStr(); + else + return ""; } diff --git a/src/main/java/com/twelfthmile/yuga/YugaMethods.java b/src/main/java/com/twelfthmile/yuga/YugaMethods.java index c1d51fb..62ab162 100644 --- a/src/main/java/com/twelfthmile/yuga/YugaMethods.java +++ b/src/main/java/com/twelfthmile/yuga/YugaMethods.java @@ -3,9 +3,8 @@ import com.twelfthmile.yuga.utils.Constants; import com.twelfthmile.yuga.utils.Util; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.Arrays; + public class YugaMethods { // for cases like 18th Jun, 12 pm @@ -59,5 +58,29 @@ static String getAmt(String type) { return ""; } } + + static boolean isCurrencyAhead(String type) { + type = getPotentialCurrString(type); + if(Arrays.asList(Constants.currAct).contains(type.toLowerCase())) + return true; + return false; + } + + static String getPotentialCurrString(String type) { + int next = nextSpace(type); + type = type.substring(0,next); + return type; + } + + static int nextSpace(String str) { + int i = 0; + while (i < str.length()) { + if (str.charAt(i) == ' ') + return i; + else + i++; + } + return i; + } } diff --git a/src/main/java/com/twelfthmile/yuga/utils/Constants.java b/src/main/java/com/twelfthmile/yuga/utils/Constants.java index 903ca3d..96e579e 100644 --- a/src/main/java/com/twelfthmile/yuga/utils/Constants.java +++ b/src/main/java/com/twelfthmile/yuga/utils/Constants.java @@ -42,9 +42,12 @@ public class Constants { public static final String TY_WGT = "WGT"; public static final String TY_ACC = "INSTRNO"; public static final String TY_TYP = "TYP"; + public static final String TY_RATE = "RATE"; public static final String TY_DTE = "DATE"; public static final String TY_DTERANGE = "DATERANGE"; public static final String TY_TME = "TIME"; + public static final String TY_NUM_MINS = "NUM_MINS"; + public static final String TY_DIST = "DISTANCE"; public static final String TY_TMERANGE = "TIMERANGE"; public static final String TY_TMS = "TIMES"; public static final String TY_STR = "STR"; @@ -70,7 +73,7 @@ public class Constants { public static final String FSA_FLTID = "6E,AI,I5,SG,G8,UK,IX,2T,9W"; public static final String[] curr = {"rs", "inr", "cny", "amt", "amount", "ngn", "usd", "cad", "eur", "gbp", "aed", "jpy", "aud", "s$", "lkr", "ksh", "egp"}; - + public static final String[] currAct = {"rs", "inr", "cny", "ngn", "usd", "cad", "eur", "gbp", "aed", "jpy", "aud", "s$", "lkr", "ksh", "egp"}; public static final String[] instr = {"card", "no", "a/c"}; public static final String[] fltid = {"6e", "indigo", "ai", "airindia", "sg", "spicejet", "g8", "goair", "uk", "vistara", "ix", diff --git a/src/test/resources/yuga_tests.json b/src/test/resources/yuga_tests.json index c77aa5c..e430251 100644 --- a/src/test/resources/yuga_tests.json +++ b/src/test/resources/yuga_tests.json @@ -1516,5 +1516,25 @@ "index": 5 }, "accepted": true + }, + { + "input": "100.0/30 mins", + "response": { + "type": "RATE", + "valMap":{"per":"TIME", "time":"00:30"}, + "str":"100.0/30 mins", + "index": 13 + }, + "accepted": true + }, + { + "input": "15.9/Km", + "response": { + "type": "RATE", + "valMap":{"per":"DISTANCE"}, + "str":"15.9/Km", + "index": 7 + }, + "accepted": true } ]