Skip to content

Commit

Permalink
fix and ignore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lfoppiano committed Apr 5, 2024
1 parent b0b1bd7 commit ed3f73f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ dependencies {
implementation 'com.sun.xml.bind:jaxb-core:4.0.1'
implementation 'com.sun.xml.bind:jaxb-impl:4.0.1'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-guava:2.13.4'

//Grobid-quantities
implementation 'tech.units:indriya:2.1.3'
implementation group: 'si.uom', name: 'si-units', version: '2.1'
Expand Down Expand Up @@ -326,7 +326,7 @@ publishing {
}
}

def conf = new org.yaml.snakeyaml.Yaml().load( new File("resources/config/config.yml").newInputStream() )
def conf = new org.yaml.snakeyaml.Yaml().load(new File("resources/config/config.yml").newInputStream())
def grobidHome = conf.grobidHome.replace("\$", "").replace('{', "").replace("GROBID_HOME:- ", "").replace("}", "")
if (grobidHome.startsWith("../")) {
grobidHome = "${rootProject.rootDir}/${grobidHome}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@
import org.grobid.core.data.document.RawPassage;
import org.grobid.core.data.document.TextPassage;
import org.grobid.core.data.document.Span;
import org.grobid.core.data.normalization.UnitNormalizer;
import org.grobid.core.engines.linking.CRFBasedLinker;
import org.grobid.core.layout.LayoutToken;
import org.grobid.core.main.LibraryLoader;
import org.grobid.core.utilities.GrobidConfig;
import org.grobid.core.utilities.client.ChemDataExtractorClient;
import org.grobid.core.utilities.GrobidProperties;
import org.grobid.core.utilities.client.StructureIdentificationModuleClient;
import org.grobid.service.configuration.GrobidQuantitiesConfiguration;
import org.grobid.service.configuration.GrobidSuperconductorsConfiguration;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -29,24 +36,38 @@
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.MatcherAssert.assertThat;

@Ignore("This has been decommissioned")
public class CRFBasedLinkerIntegrationTest {

private CRFBasedLinker target;
private ModuleEngine moduleEngine;
private ChemDataExtractorClient mockChemdataExtractorClient;
private StructureIdentificationModuleClient mockSpaceGroupsClient;


@Before
public void setUp() throws Exception {
public static void initEngineForTests() throws IOException, IllegalAccessException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

// https://stackoverflow.com/questions/14853324/can-not-find-deserialize-for-non-concrete-collection-type
mapper.registerModule(new GuavaModule());
GrobidSuperconductorsConfiguration configuration = mapper.readValue(this.getClass().getResourceAsStream("config-test.yml"), GrobidSuperconductorsConfiguration.class);
GrobidSuperconductorsConfiguration configuration = mapper.readValue(RuleBasedLinker.class.getResourceAsStream("config-test.yml"), GrobidSuperconductorsConfiguration.class);
configuration.getModels().stream().forEach(GrobidProperties::addModel);
GrobidProperties.getInstance();
Field modelMap = Whitebox.getField(GrobidProperties.class, "modelMap");

Map<String, GrobidConfig.ModelParameters> newModelMap = (Map<String, GrobidConfig.ModelParameters>) modelMap.get(new HashMap<>());
newModelMap.entrySet().stream()
.forEach(entry -> {
entry.getValue().engine = "wapiti";
});
Whitebox.setInternalState(GrobidProperties.class, "modelMap", newModelMap);
// GrobidProperties.getDistinctModels().stream().forEach(model -> model);
LibraryLoader.load();

}

@Before
public void setUp() throws Exception {
initEngineForTests();

target = new CRFBasedLinker();
mockChemdataExtractorClient = EasyMock.createMock(ChemDataExtractorClient.class);
mockSpaceGroupsClient = EasyMock.createMock(StructureIdentificationModuleClient.class);
Expand All @@ -55,37 +76,36 @@ public void setUp() throws Exception {
}

@Test
@Ignore()
public void testRealCase_shouldExtract1Link() throws Exception {
String input = "MgB 2 was discovered to be a superconductor in 2001, and it has a remarkably high critical temperature (T c ) around 40 K with a simple hexagonal structure.";
List<LayoutToken> layoutTokens = DeepAnalyzer.getInstance().tokenizeWithLayoutToken(input);

EasyMock.expect(mockChemdataExtractorClient.processBulk(EasyMock.anyObject())).andReturn(Arrays.asList(new ArrayList<>()));
EasyMock.expect(mockSpaceGroupsClient.extractStructuresMulti(EasyMock.anyObject())).andReturn(new ArrayList<>());
EasyMock.replay(mockChemdataExtractorClient, mockSpaceGroupsClient);

List<TextPassage> passages = moduleEngine.process(Arrays.asList(new RawPassage(layoutTokens)), true);

assertThat(passages, hasSize(1));

TextPassage passage = passages.get(0);
List<Span> annotations = passage.getSpans();
assertThat(annotations, hasSize(4));

//set the annotations linkable
annotations
.stream()
.filter(l -> l.getType().equals(SUPERCONDUCTORS_TC_VALUE_LABEL) || l.getType().equals(SUPERCONDUCTORS_MATERIAL_LABEL))
.forEach(l -> l.setLinkable(true));

target.process(layoutTokens, annotations, CRFBasedLinker.getInstance().MATERIAL_TCVALUE_ID);

List<Span> linkedEntities = annotations.stream().filter(l -> isNotEmpty(l.getLinks())).collect(Collectors.toList());
assertThat(linkedEntities, hasSize(2));

assertThat(linkedEntities.get(0).getText(), is("MgB 2"));
assertThat(linkedEntities.get(0).getLinks().get(0).getTargetText(), is("40 K"));

EasyMock.verify(mockChemdataExtractorClient, mockSpaceGroupsClient);
}

Expand All @@ -102,7 +122,7 @@ public void testRealCase_shouldRecogniseOneLink() throws Exception {

List<TextPassage> paragraphs = moduleEngine.process(Arrays.asList(new RawPassage(layoutTokens)), true);
assertThat(paragraphs, hasSize(1));

TextPassage paragraph = paragraphs.get(0);
List<Span> annotations = paragraph.getSpans();
assertThat(annotations, hasSize(annotations.size()));
Expand Down Expand Up @@ -133,14 +153,14 @@ public void testRealCase_shouldNotLink() throws Exception {
EasyMock.expect(mockChemdataExtractorClient.processBulk(EasyMock.anyObject())).andReturn(Arrays.asList(new ArrayList<>()));
EasyMock.expect(mockSpaceGroupsClient.extractStructuresMulti(EasyMock.anyObject())).andReturn(new ArrayList<>());
EasyMock.replay(mockChemdataExtractorClient, mockSpaceGroupsClient);

List<TextPassage> paragraphs = moduleEngine.process(Arrays.asList(new RawPassage(layoutTokens)), true);
assertThat(paragraphs, hasSize(1));

TextPassage paragraph = paragraphs.get(0);
List<Span> annotations = paragraph.getSpans();


target.process(layoutTokens, annotations, CRFBasedLinker.MATERIAL_TCVALUE_ID);

List<Span> linkedEntities = annotations.stream().filter(l -> isNotEmpty(l.getLinks())).collect(Collectors.toList());
Expand Down Expand Up @@ -204,7 +224,7 @@ public void testRealCase_shouldExtract1Links_2() throws Exception {
List<Span> linkedEntities = processedSpans.stream()
.filter(l -> isNotEmpty(l.getLinks()) && l.getType().equals(SUPERCONDUCTORS_MATERIAL_LABEL))
.collect(Collectors.toList());

assertThat(linkedEntities, hasSize(1));

EasyMock.verify(mockChemdataExtractorClient, mockSpaceGroupsClient);
Expand All @@ -218,7 +238,7 @@ public void testRealCase_shouldExtract0Links_3() throws Exception {
EasyMock.expect(mockChemdataExtractorClient.processBulk(EasyMock.anyObject())).andReturn(Arrays.asList(new ArrayList<>()));
EasyMock.expect(mockSpaceGroupsClient.extractStructuresMulti(EasyMock.anyObject())).andReturn(new ArrayList<>());
EasyMock.replay(mockChemdataExtractorClient, mockSpaceGroupsClient);

List<TextPassage> paragraphs = moduleEngine.process(Collections.singletonList(new RawPassage(layoutTokens)), true);
assertThat(paragraphs, hasSize(1));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.MatcherAssert.assertThat;

@Ignore("Decommissioned")
@RunWith(PowerMockRunner.class)
@PrepareForTest(Lexicon.class)
public class CRFBasedLinkerTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;

import static org.easymock.EasyMock.*;
import static org.grobid.core.engines.CRFBasedLinkerIntegrationTest.initEngineForTests;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
Expand All @@ -32,6 +33,7 @@ public class GrobidPDFEngineStaticMockTest {

@BeforeClass
public static void before() throws Exception {
initEngineForTests();
GrobidConfig.ModelParameters modelParameters = new GrobidConfig.ModelParameters();
modelParameters.name = "bao";
GrobidProperties.addModel(modelParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.stream.Collectors;

import static org.grobid.core.engines.CRFBasedLinkerIntegrationTest.initEngineForTests;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.core.Is.is;
Expand All @@ -22,7 +23,7 @@ public class SentenceSegmenterIntegrationTest {

@Before
public void setUp() throws Exception {
LibraryLoader.load();
initEngineForTests();
target = new SentenceSegmenter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.grobid.core.engines.CRFBasedLinkerIntegrationTest.initEngineForTests;
import static org.grobid.core.engines.SuperconductorsParser.NONE_CHEMSPOT_TYPE;
import static org.grobid.core.utilities.GrobidTestUtils.getWapitiResult;
import static org.hamcrest.CoreMatchers.is;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class AnnotationValuesTEIStaxHandlerIntegrationTest {

@Before
public void setUp() throws Exception {
LibraryLoader.load();
// LibraryLoader.load();
}


Expand Down

0 comments on commit ed3f73f

Please sign in to comment.