Skip to content

Commit

Permalink
more tests, adding kotlin mock engine
Browse files Browse the repository at this point in the history
  • Loading branch information
lfoppiano committed Feb 26, 2024
1 parent 3ee8c5b commit 8a896fe
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ dependencies {
testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
testImplementation 'org.powermock:powermock-api-easymock:2.0.9'
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation "io.mockk:mockk:1.13.9"

//GROBID
implementation 'org.grobid:grobid-core:0.8.0'
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/grobid/core/engines/QuantityParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ public List<Measurement> extractMeasurement(List<LayoutToken> tokens, String res
return measurements;
}

private static void populateRawOffsetsAndText(Measurement m, List<LayoutToken> tokens) {
protected static void populateRawOffsetsAndText(Measurement m, List<LayoutToken> tokens) {
final Pair<OffsetPosition, String> measurementRawOffsetsAndText = QuantityOperations.getMeasurementRawOffsetsAndText(m, tokens);
m.setRawOffsets(measurementRawOffsetsAndText.getLeft());
m.setRawString(measurementRawOffsetsAndText.getRight().replace("\n", " "));
Expand All @@ -803,7 +803,7 @@ protected OffsetPosition findSentenceOffset(List<OffsetPosition> sentences, Offs
List<OffsetPosition> sentencesCurrentMeasure = sentences.stream()
.filter(sop -> sop.start <= currentMeasureOffset.start - firstTokenOffset
&& sop.end > currentMeasureOffset.end - firstTokenOffset)
.collect(Collectors.toList());
.toList();

if (sentencesCurrentMeasure.size() == 1) {
return sentencesCurrentMeasure.get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
package org.grobid.core.engines;

import com.googlecode.clearnlp.engine.EngineGetter
import com.googlecode.clearnlp.tokenization.AbstractTokenizer
import org.grobid.core.data.SentenceParse
import org.grobid.core.utilities.GrobidConfig
import org.grobid.core.utilities.GrobidProperties
import io.mockk.every
import io.mockk.mockkStatic
import org.easymock.EasyMock
import org.easymock.EasyMock.anyObject
import org.grobid.core.analyzers.QuantityAnalyzer
import org.grobid.core.data.*
import org.grobid.core.utilities.*
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import java.util.*
import kotlin.test.Test


class DefaultQuantifiedObjectParserTest {

private var target: DefaultQuantifiedObjectParser? = null
lateinit var target: DefaultQuantifiedObjectParser
lateinit var mockTextParser: TextParser

@BeforeEach
fun setUp() {
target = DefaultQuantifiedObjectParser()
// val tokenizer: AbstractTokenizer? = EngineGetter.getTokenizer("en", );
mockTextParser = EasyMock.createMock(TextParser::class.java);
// PowerMock.mockStatic(TextParser::class.java)
mockkStatic(TextParser::class)
every { TextParser.getInstance() } returns mockTextParser
}

@Test
fun addTokenIndex() {
fun testAddTokenIndex() {
val result: List<String> = ArrayList()

val tree = """1 However however RB _ 12 advmod 12:AM-DIS
Expand Down Expand Up @@ -112,6 +118,56 @@ class DefaultQuantifiedObjectParserTest {
assertThat(indexes.get(1), `is`("4"))
}

@Test
fun testProcess() {
val text = "A 20kg ingot is made in a high frequency induction melting furnace"
val tokens = QuantityAnalyzer.getInstance().tokenizeWithLayoutToken(text)

val measurement = Measurement(UnitUtilities.Measurement_Type.VALUE)
val unit = Unit("Kg", OffsetPosition(4, 6))
val atomicQuantity = Quantity("20", unit, OffsetPosition(2, 4))
atomicQuantity.layoutTokens = listOf(tokens[2])
unit.layoutTokens = listOf(tokens[3])
measurement.setAtomicQuantity(atomicQuantity)
QuantityParser.populateRawOffsetsAndText(measurement, tokens)
val measurements = listOf(measurement)



var parseRepresentation = """1 A a DT _ 4 det _
2 20 0 CD _ 3 num _
3 kg kg NN _ 4 nn _
4 ingot ingot NN _ 6 nsubjpass 6:AM-PRR
5 is be VBZ pb=be.03 6 auxpass _
6 made make VBN pb=make.LV 0 root _
7 in in IN _ 6 prep _
8 a a DT _ 12 det _
9 high high JJ _ 10 amod _
10 frequency frequency NN _ 12 nn _
11 induction induction NN _ 12 nn _
12 melting melting NN _ 7 pobj _
13 furnace furnace JJ _ 12 amod _"""

val parse = SentenceParse()
parse.setParseRepresentation(parseRepresentation)
parse.createMap(text)

val sentence = OffsetPosition(0, 66)
val sentences = Sentence(text, listOf(parse), sentence)

EasyMock.expect(mockTextParser.parseText(anyObject(), anyObject())).andReturn(listOf(sentences)).anyTimes()
EasyMock.replay(mockTextParser)

val output = target.process(tokens, measurements);

assertThat(output, hasSize(1))
assertThat(output[0].quantifiedObject, notNullValue())
assertThat(output[0].quantifiedObject.rawName, `is`("ingot"))
assertThat(output[0].quantifiedObject.normalizedName, `is`("ingot"))
assertThat(output[0].quantifiedObject.offsetStart, `is`(7))
assertThat(output[0].quantifiedObject.offsetEnd, `is`(12))
}

companion object {
@JvmStatic
@BeforeAll
Expand Down

0 comments on commit 8a896fe

Please sign in to comment.