grobid-quantities icon indicating copy to clipboard operation
grobid-quantities copied to clipboard

How to extract quantities via the Java API

Open navraj28 opened this issue 4 years ago • 3 comments

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

navraj28 avatar Sep 08 '20 08:09 navraj28

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

lfoppiano avatar Sep 09 '20 02:09 lfoppiano

Thanks @lfoppiano ! I tried the above. But getting an empty list for Measurements.

navraj28 avatar Sep 09 '20 07:09 navraj28

Does the demo work? https://grobid-quantities.readthedocs.io/en/latest/gettingStarted.html#start-and-use-the-service

lfoppiano avatar Sep 10 '20 09:09 lfoppiano

@navraj28 I'm closing this issue. Feel free to reopen if you need me to look into it

lfoppiano avatar Dec 15 '23 02:12 lfoppiano