All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.cmu.sv.spoken_language_understanding.Utils Maven / Gradle / Ivy

Go to download

A library that allows rapid prototyping of dialog systems (language understanding, discourse modelling, dialog management, language generation).

There is a newer version: 0.7.0
Show newest version
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);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy