forked from lintool/Ivory
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:nasadi/Ivory into nasadi-master
- Loading branch information
Showing
14 changed files
with
496 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ivory.ltr.operator; | ||
|
||
public class BooleanCount extends Operator { | ||
@Override public double getFinalScore() { | ||
int s = 0; | ||
for(double f: scores) { | ||
if(f > 0) { | ||
s++; | ||
} | ||
} | ||
return ((double) s); | ||
} | ||
|
||
@Override public Operator newInstance() { | ||
return new BooleanCount(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ivory.ltr.operator; | ||
|
||
public class BooleanRatio extends Operator { | ||
@Override public double getFinalScore() { | ||
if(scores.size() == 0) { | ||
return 0; | ||
} | ||
|
||
int s = 0; | ||
for(double f: scores) { | ||
if(f > 0) { | ||
s++; | ||
} | ||
} | ||
return ((double) s) / scores.size(); | ||
} | ||
|
||
@Override public Operator newInstance() { | ||
return new BooleanRatio(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ivory.ltr.operator; | ||
|
||
public class Max extends Operator { | ||
@Override public double getFinalScore() { | ||
double s = Double.NEGATIVE_INFINITY; | ||
for(double f: scores) { | ||
if(f > s) { | ||
s = f; | ||
} | ||
} | ||
return s; | ||
} | ||
|
||
@Override public Operator newInstance() { | ||
return new Max(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ivory.ltr.operator; | ||
|
||
public class Mean extends Operator { | ||
@Override public double getFinalScore() { | ||
if(scores.size() == 0) { | ||
return 0; | ||
} | ||
|
||
double s = 0; | ||
for(double f: scores) { | ||
s += f; | ||
} | ||
|
||
return (s / scores.size()); | ||
} | ||
|
||
@Override public Operator newInstance() { | ||
return new Mean(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ivory.ltr.operator; | ||
|
||
public class Min extends Operator { | ||
@Override public double getFinalScore() { | ||
double s = Double.POSITIVE_INFINITY; | ||
for(double f: scores) { | ||
if(f < s) { | ||
s = f; | ||
} | ||
} | ||
return s; | ||
} | ||
|
||
@Override public Operator newInstance() { | ||
return new Min(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ivory.ltr.operator; | ||
|
||
import java.util.List; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
/** | ||
* Holds and computes the final value of a feature | ||
* | ||
* @author Nima Asadi | ||
*/ | ||
public abstract class Operator { | ||
protected List<Double> scores; | ||
|
||
protected Operator() { | ||
scores = Lists.newArrayList(); | ||
} | ||
|
||
/** | ||
* Adds a new score | ||
* | ||
* @param score Score | ||
*/ | ||
public void addScore(double score) { | ||
scores.add(score); | ||
} | ||
|
||
/** | ||
* Clears the scores | ||
*/ | ||
public void clear() { | ||
scores.clear(); | ||
} | ||
|
||
/** | ||
* Computes the final feature value | ||
* | ||
* @return Feature value | ||
*/ | ||
public abstract double getFinalScore(); | ||
|
||
/** | ||
* @return New instance | ||
*/ | ||
public abstract Operator newInstance(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ivory.ltr.operator; | ||
|
||
import java.io.InputStream; | ||
import java.io.IOException; | ||
import java.io.File; | ||
import java.util.Map; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.Maps; | ||
import com.google.common.io.InputSupplier; | ||
import com.google.common.io.Files; | ||
|
||
import org.xml.sax.SAXException; | ||
|
||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.NodeList; | ||
|
||
/** | ||
* Provides auxiliary functions to parse operators. | ||
* | ||
* @author Nima Asadi | ||
*/ | ||
public class OperatorUtil { | ||
/** | ||
* Loads and tokenizes a set of features | ||
* | ||
* @param featurePath Path to the file containing the feature descriptions | ||
* @return Map of feature id to operator | ||
*/ | ||
public static Map<String, Operator> parseOperators(String featurePath) | ||
throws Exception { | ||
return OperatorUtil.loadOperators(Files.newInputStreamSupplier(new File(featurePath))); | ||
} | ||
|
||
/** | ||
* Reads a feature set in an XML format as follows: | ||
* | ||
* @param featureInputSupplier An input supplier that provides the feature descriptions | ||
* @return A map of feature id to Operator | ||
*/ | ||
public static Map<String, Operator> loadOperators(InputSupplier<? extends InputStream> featureInputSupplier) | ||
throws ParserConfigurationException, SAXException, IOException, Exception { | ||
Preconditions.checkNotNull(featureInputSupplier); | ||
|
||
Map<String, Operator> operators = Maps.newHashMap(); | ||
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(featureInputSupplier.getInput()); | ||
NodeList nodeList = dom.getDocumentElement().getElementsByTagName("model"); | ||
|
||
if(nodeList == null) { | ||
return null; | ||
} | ||
|
||
for(int i = 0; i < nodeList.getLength(); i++) { | ||
Element element = (Element) nodeList.item(i); | ||
String modelName = element.getAttribute("id"); | ||
NodeList featureList = element.getElementsByTagName("feature"); | ||
for(int j = 0; j < featureList.getLength(); j++) { | ||
Element felement = (Element) featureList.item(j); | ||
String fid = modelName + "-" + felement.getAttribute("id"); | ||
if(felement.hasAttribute("operator")) { | ||
String className = felement.getAttribute("operator"); | ||
operators.put(fid, (Operator) Class.forName(className).newInstance()); | ||
} else { | ||
operators.put(fid, new Sum()); | ||
} | ||
} | ||
} | ||
return operators; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ivory.ltr.operator; | ||
|
||
public class Sum extends Operator { | ||
@Override public double getFinalScore() { | ||
double s = 0; | ||
for(double f: scores) { | ||
s += f; | ||
} | ||
return s; | ||
} | ||
|
||
@Override public Operator newInstance() { | ||
return new Sum(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ivory.ltr.operator; | ||
|
||
public class Variance extends Operator { | ||
@Override public double getFinalScore() { | ||
if(scores.size() == 0) { | ||
return 0; | ||
} | ||
|
||
double mean = 0; | ||
for(double f: scores) { | ||
mean += f; | ||
} | ||
mean /= scores.size(); | ||
|
||
double var = 0; | ||
for(double f: scores) { | ||
var += Math.pow((f - mean), 2); | ||
} | ||
|
||
return var / scores.size(); | ||
} | ||
|
||
@Override public Operator newInstance() { | ||
return new Variance(); | ||
} | ||
} |
Oops, something went wrong.