Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regex, #867 #868

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ public enum MarkerType {

// simple patterns just to capture the majority callout style
private final static Pattern BRACKET_TEXT_PATTERN = Pattern.compile("\\[(.)+\\]");
private final static Pattern BRACKET_NUMBER_PATTERN = Pattern.compile("\\[((\\d+[a-f]?)|[,-;•])+\\]");
//private final static Pattern BRACKET_NUMBER_PATTERN = Pattern.compile("\\[((\\d{0,4}[a-f]?)|[,-;•])+\\]");
private final static Pattern BRACKET_NUMBER_PATTERN = Pattern.compile("\\[(?>[0-9]{1,4}[a-f]?[\\-;•,]?((and)|&|(et))?)+\\]");
private final static Pattern PARENTHESIS_TEXT_PATTERN = Pattern.compile("\\((.)+\\)");
private final static Pattern PARENTHESIS_NUMBER_PATTERN = Pattern.compile("\\(((\\d+[a-f]?)|[,-;•])+\\)");
private final static Pattern NUMBER_PATTERN = Pattern.compile("(\\d+)[a-f]?");
//private final static Pattern PARENTHESIS_NUMBER_PATTERN = Pattern.compile("\\(((\\d+[a-f]?)|[,-;•])+\\)");
private final static Pattern PARENTHESIS_NUMBER_PATTERN = Pattern.compile("\\((?>[0-9]{1,4}[a-f]?[\\-;•,]?((and)|&|(et))?)+\\)");
private final static Pattern NUMBER_PATTERN = Pattern.compile("(?>\\d+)[a-f]?");
private final static Pattern ROMAN_PATTERN = Pattern.compile("(IX|IV|V?I{0,3})");

public static MarkerType getCalloutType(List<LayoutToken> callout) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.grobid.core.engines.citations;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;

import org.grobid.core.engines.citations.CalloutAnalyzer.MarkerType;
import org.grobid.core.analyzers.GrobidDefaultAnalyzer;
import org.grobid.core.layout.LayoutToken;

import java.util.List;

public class CalloutAnalyzerTest {

@Test
public void testGetCalloutTypeText() throws Exception {
String input = "(Dé&amps, C & Bidule, D., 2010)";
List<LayoutToken> inputCallout = GrobidDefaultAnalyzer.getInstance().tokenizeWithLayoutToken(input);
MarkerType result = MarkerType.PARENTHESIS_TEXT;
assertThat(CalloutAnalyzer.getCalloutType(inputCallout), is(result));
}

@Test
public void testGetCalloutTypeTextFail() throws Exception {
String input = "(1,3)";
List<LayoutToken> inputCallout = GrobidDefaultAnalyzer.getInstance().tokenizeWithLayoutToken(input);
MarkerType result = MarkerType.PARENTHESIS_TEXT;
assertThat(CalloutAnalyzer.getCalloutType(inputCallout), not(result));
}

@Test
public void testGetCalloutTypeNumber() throws Exception {
String input = "[1-5]";
List<LayoutToken> inputCallout = GrobidDefaultAnalyzer.getInstance().tokenizeWithLayoutToken(input);
MarkerType result = MarkerType.BRACKET_NUMBER;
assertThat(CalloutAnalyzer.getCalloutType(inputCallout), is(result));
}

@Test
public void testGetCalloutTypeNumberFail() throws Exception {
String input = "[Foppiano et. al, 2004]";
List<LayoutToken> inputCallout = GrobidDefaultAnalyzer.getInstance().tokenizeWithLayoutToken(input);
MarkerType result = MarkerType.BRACKET_NUMBER;
assertThat(CalloutAnalyzer.getCalloutType(inputCallout), not(result));
}

@Test
public void testGetCalloutCatastrophicBacktracking() throws Exception {
String input = "(1915-1919, 1920-1924, 1925-1929, 1930-1934, and 1935-1939)";
List<LayoutToken> inputCallout = GrobidDefaultAnalyzer.getInstance().tokenizeWithLayoutToken(input);
MarkerType result = MarkerType.PARENTHESIS_NUMBER;
assertThat(CalloutAnalyzer.getCalloutType(inputCallout), is(result));
}

@Test
public void testGetCalloutCatastrophicBacktrackingFail() throws Exception {
String input = "(1915-1919, 1920-1924, 1925-1929, 1930-1934, and 1935-1939)";
List<LayoutToken> inputCallout = GrobidDefaultAnalyzer.getInstance().tokenizeWithLayoutToken(input);
MarkerType result = MarkerType.BRACKET_NUMBER;
assertThat(CalloutAnalyzer.getCalloutType(inputCallout), not(result));
}
}