edu.cmu.sv.spoken_language_understanding.Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yoda Show documentation
Show all versions of yoda Show documentation
A library that allows rapid prototyping of dialog systems (language understanding, discourse modelling, dialog management, language generation).
package edu.cmu.sv.spoken_language_understanding;
import com.google.common.primitives.Doubles;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by David Cohen on 5/11/15.
*/
public class Utils {
public static double stringSetBestCoverage(String phrase, Set matchingStrings){
double ans = 0.0;
int adjustedLength = phrase.replaceAll("\\Aa ","").replaceAll("\\Aan ","").replaceAll("\\Aany ","").
replaceAll("\\Athe ","").replaceAll("\\Asome ","").trim().length();
for (String matchingString : matchingStrings) {
Pattern regexPattern = Pattern.compile("(.+ | |)" + matchingString + "( .+| |)");
Matcher matcher = regexPattern.matcher(phrase);
if (matcher.matches())
ans = Doubles.max(ans, matchingString.length() * 1.0 / adjustedLength);
}
return Doubles.min(ans, 1.0);
}
public static double stringSetTotalCoverage(String phrase, Set matchingStrings){
phrase = phrase.replace("any ","").replace("the ","").replace("some ","").trim();
int adjustedLength = Integer.max(1, phrase.length());
for (String matchingString : matchingStrings) {
phrase = phrase.replaceAll("(\\A| )" + matchingString + "( |\\z)", " ").trim();
}
return 1.0 - ((1.0 * phrase.length()) / adjustedLength);
}
}