Skip to content

Commit

Permalink
Merge pull request #89 from messai-engineering/active
Browse files Browse the repository at this point in the history
YugaV1.60 || Introduced Rates || trailing curr to amt
  • Loading branch information
saxena-saumittra authored Dec 20, 2023
2 parents b331db8 + 0d229cc commit 077c264
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>in.messai</groupId>
<artifactId>yuga</artifactId>
<version>1.0.59</version>
<version>1.0.60</version>

<name>Yuga Date Parser</name>
<description>
Expand Down Expand Up @@ -188,7 +188,7 @@
<developerConnection>scm:git:[email protected]:messai-engineering/Yuga.git</developerConnection>
<url>https://github.com/messai-engineering/Yuga</url>

<tag>yuga-1.0.59</tag>
<tag>yuga-1.0.60</tag>

</scm>
</project>
57 changes: 41 additions & 16 deletions src/main/java/com/twelfthmile/yuga/Yuga.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ public static Response parse(String str) {
private static Pair<String, Object> prepareResult(String str, Pair<Integer, FsaContextMap> p, Map<String, String> 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)) {
Expand Down Expand Up @@ -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();

}
}
}
}
}
Expand All @@ -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+3<str.length() && (str.substring(k,k+2).equalsIgnoreCase("km") || str.substring(k,k+3).equalsIgnoreCase("/km"))){
map.setType(Constants.TY_DIST, Constants.TY_DIST);
map.put("type","km");
i = k+3;
}
else if (i < str.length() && str.charAt(i-1)!=' ' && Character.isAlphabetic(str.charAt(i)) && (!config.containsKey(Constants.YUGA_SOURCE_CONTEXT)||(!Constants.YUGA_SC_CURR.equals(config.get(Constants.YUGA_SOURCE_CONTEXT))&&!Constants.YUGA_SC_TRANSID.equals(config.get(Constants.YUGA_SOURCE_CONTEXT))))) {
int j = i;
Expand Down Expand Up @@ -1304,7 +1337,7 @@ else if(config.containsKey(Constants.YUGA_SOURCE_CONTEXT) && config.get(Constant
map.getValMap().remove("to_num");
map.setType(Constants.TY_TMS);
} else if(sub.length()>3 && (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);
Expand Down Expand Up @@ -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);
Expand All @@ -1477,8 +1500,8 @@ private static int accAmtNumPct(String str, int i, FsaContextMap map, Map<String
map.setType(Constants.TY_AMT, Constants.TY_AMT);
map.append(c);
return 10;
} else if(c == Constants.CH_STAR && subStr.length()>10 && 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);
Expand Down Expand Up @@ -1544,10 +1567,12 @@ private static int getPrevState(ArrayList<Integer> prevStates) {

public static String getYugaResponseOutput(String str,Map<String, String> 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 "";
}


Expand Down
29 changes: 26 additions & 3 deletions src/main/java/com/twelfthmile/yuga/YugaMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

5 changes: 4 additions & 1 deletion src/main/java/com/twelfthmile/yuga/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions src/test/resources/yuga_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]

0 comments on commit 077c264

Please sign in to comment.