grobid-quantities
grobid-quantities copied to clipboard
How to extract quantities via the Java API
Hello! Can we access grobid-quantities via a Java API, just like we can access grobid via the Java API. I tried as below, but it did not work.
package org.grobid.service;
import java.util.Arrays;
import org.grobid.core.data.Measurement;
import org.grobid.core.data.MeasurementsResponse;
import org.grobid.core.engines.QuantitiesEngine;
import org.grobid.core.main.GrobidHomeFinder;
import org.grobid.core.main.LibraryLoader;
import org.grobid.core.utilities.GrobidProperties;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String pGrobidHome = "D:\\grobid\\grobid-home";
// The GrobidHomeFinder can be instantiate without parameters to verify the grobid home in the standard
// location (classpath, ../grobid-home, ../../grobid-home)
// If the location is customised:
GrobidHomeFinder grobidHomeFinder = new GrobidHomeFinder(Arrays.asList(pGrobidHome));
//The GrobidProperties needs to be instantiate using the correct grobidHomeFinder or it will use the default
//locations
GrobidProperties.getInstance(grobidHomeFinder);
System.out.println(">>>>>>>> GROBID_HOME="+GrobidProperties.get_GROBID_HOME_PATH());
LibraryLoader.load();
QuantitiesEngine engine = new QuantitiesEngine();
String text = "Add 2Kg of rice to 1/2 a Kg of sugar";
MeasurementsResponse response = engine.processText(text);
for (Measurement m : response.getMeasurements()) {
System.out.println("********************* " + m.getQuantityAtomic());
}
}
catch (Exception e) {
// If an exception is generated, print a stack trace
e.printStackTrace();
}
}
}
Thanks, NR
Yes, it's not documented though... 😅
going by memory, I would use something like this:
//The false disable the quantified object
QuantityParser quantityParser = QuantityParser.getInstance(false);
List<Measurement> measurements = quantityParser.process(text);
But you need to import the grobid-quantities:0.6.0 jar
Thanks @lfoppiano ! I tried the above. But getting an empty list for Measurements.
Does the demo work? https://grobid-quantities.readthedocs.io/en/latest/gettingStarted.html#start-and-use-the-service
@navraj28 I'm closing this issue. Feel free to reopen if you need me to look into it