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

Add parser for German months (#3536) #3734

Merged
merged 15 commits into from
Mar 9, 2018
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The new default removes the linked file from the entry instead of deleting the f
- The group editing window can now also be called by double-clicking the group to be edited. [koppor#277](https://github.com/koppor/jabref/issues/277)
- The magnifier icon at the search shows the [search mode](https://help.jabref.org/en/Search#search-modes) again. [#3535](https://github.com/JabRef/jabref/issues/3535)
- We added a new cleanup operation that replaces ligatures with their expanded form. [#3613](https://github.com/JabRef/jabref/issues/3613)
- We added the function to parse German month names. [#3536](https://github.com/JabRef/jabref/pull/3536)

### Fixed
- We fixed several performance problems with the management of journal abbreviations [#3323](https://github.com/JabRef/jabref/issues/3323)
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/jabref/model/entry/Month.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.jabref.model.entry;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;
import java.util.Optional;

import org.jabref.model.strings.StringUtil;
Expand Down Expand Up @@ -85,9 +89,13 @@ public static Optional<Month> parse(String value) {
testString = testString.substring(0, 3);
}
Optional<Month> month = Month.getMonthByShortName(testString);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indent is wrong here. I have no clue, what is going wrong on your side. Do you use tabs instead of spaces?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what went wrong. I used the auto formatter of Eclipse, so it could be the tabs. Now it looks better after replacing the tabs with spaces by hand. I should definetly change the IDE if this is a common problem.

Optional<Month> monthGerman = Month.parseGermanShortMonth(testString);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move that after line 95. the German month only has to be determined if there is no English month.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (month.isPresent()) {
return month;
}
if (monthGerman.isPresent()) {
return monthGerman;
}

try {
int number = Integer.parseInt(value);
Expand All @@ -97,6 +105,29 @@ public static Optional<Month> parse(String value) {
}
}

/**
* Parses a month having the string in German standard form such as
* "Oktober" or in German short form such as "Okt"
*
* @param value,
* a String that represents a month in German form
* @return the corresponding month instance, empty if input is not in German
* form
*/
private static Optional<Month> parseGermanShortMonth(String value) {
if ("Mae".equals(value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it is "mae" in lowercase?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation should use equalsIgnoreCase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix indent.

return Month.getMonthByNumber(3);
}

try {
YearMonth yearMonth = YearMonth.parse("1969-" + value,
DateTimeFormatter.ofPattern("yyyy-MMM", Locale.GERMAN));
return Month.getMonthByNumber(yearMonth.getMonthValue());
} catch (DateTimeParseException e) {
return Optional.empty();
}
}

/**
* Returns the name of a Month in a short (3-letter) format. (jan, feb, mar, ...)
*
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/jabref/model/entry/MonthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,38 @@ public void parseReturnsEmptyOptionalForInvalidInput() {
public void parseReturnsEmptyOptionalForEmptyInput() {
assertEquals(Optional.empty(), Month.parse(""));
}

@Test
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adjusted it.

public void parseCorrectlyByShortNameGerman() {
assertEquals(Optional.of(Month.JANUARY), Month.parse("Jan"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add tests for lowercase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The German short forms are in uppercase. So what should be tested?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, then just copy the test cases to a new method and then just call month. Parse with the lowercase name
And execute the tests then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests were added.

assertEquals(Optional.of(Month.FEBRUARY), Month.parse("Feb"));
assertEquals(Optional.of(Month.MARCH), Month.parse("M�r"));
assertEquals(Optional.of(Month.MARCH), Month.parse("Mae"));
assertEquals(Optional.of(Month.APRIL), Month.parse("Apr"));
assertEquals(Optional.of(Month.MAY), Month.parse("Mai"));
assertEquals(Optional.of(Month.JUNE), Month.parse("Jun"));
assertEquals(Optional.of(Month.JULY), Month.parse("Jul"));
assertEquals(Optional.of(Month.AUGUST), Month.parse("Aug"));
assertEquals(Optional.of(Month.SEPTEMBER), Month.parse("Sep"));
assertEquals(Optional.of(Month.OCTOBER), Month.parse("Okt"));
assertEquals(Optional.of(Month.NOVEMBER), Month.parse("Nov"));
assertEquals(Optional.of(Month.DECEMBER), Month.parse("Dez"));
}

@Test
public void parseCorrectlyByFullNameGerman() {
assertEquals(Optional.of(Month.JANUARY), Month.parse("Januar"));
assertEquals(Optional.of(Month.FEBRUARY), Month.parse("Februar"));
assertEquals(Optional.of(Month.MARCH), Month.parse("M�rz"));
assertEquals(Optional.of(Month.MARCH), Month.parse("Maerz"));
assertEquals(Optional.of(Month.APRIL), Month.parse("April"));
assertEquals(Optional.of(Month.MAY), Month.parse("Mai"));
assertEquals(Optional.of(Month.JUNE), Month.parse("Juni"));
assertEquals(Optional.of(Month.JULY), Month.parse("Juli"));
assertEquals(Optional.of(Month.AUGUST), Month.parse("August"));
assertEquals(Optional.of(Month.SEPTEMBER), Month.parse("September"));
assertEquals(Optional.of(Month.OCTOBER), Month.parse("Oktober"));
assertEquals(Optional.of(Month.NOVEMBER), Month.parse("November"));
assertEquals(Optional.of(Month.DECEMBER), Month.parse("Dezember"));
}
}