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

Anomaly with Title case and Sentence case #9112 #9142

Merged
merged 4 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed several bugs regarding the manual and the autosave of library files that sometimes lead to exceptions or data loss. [#9067](https://github.com/JabRef/jabref/pull/9067), [#8448](https://github.com/JabRef/jabref/issues/8484), [#8746](https://github.com/JabRef/jabref/issues/8746), [#6684](https://github.com/JabRef/jabref/issues/6684), [#6644](https://github.com/JabRef/jabref/issues/6644), [#6102](https://github.com/JabRef/jabref/issues/6102), [#6002](https://github.com/JabRef/jabref/issues/6000)
- We fixed an issue where applied save actions on saving the library file would lead to the dialog "The library has been modified by another program" popping up [#4877](https://github.com/JabRef/jabref/issues/4877)
- We fixed issues with save actions not correctly loaded when opening the library. [#9122](https://github.com/JabRef/jabref/pull/9122)
- We fixed an issue where title case didn't capitalize words after en-dash characters. [#9068](https://github.com/JabRef/jabref/pull/9068)
- We fixed an issue where title case didn't capitalize words after en-dash characters and skip capitalization of conjunctions that comes after en-dash characters. [#9068](https://github.com/JabRef/jabref/pull/9068),[#9142](https://github.com/JabRef/jabref/pull/9142)
- We fixed an issue where JabRef would not exit when a connection to a LibreOffice document was established previously and the document is still open. [#9075](https://github.com/JabRef/jabref/issues/9075)

### Removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public String format(String input) {
Title title = new Title(sentence);

title.getWords().stream().filter(Word::isSmallerWord).forEach(Word::toLowerCase);
title.getWords().stream().filter(Word::isLargerWord).forEach(Word::toUpperFirst);
title.getWords().stream().filter(Word::isLargerWord).forEach(Word::toUpperFirstTitle);

title.getFirstWord().ifPresent(Word::toUpperFirst);
title.getLastWord().ifPresent(Word::toUpperFirst);
title.getFirstWord().ifPresent(Word::toUpperFirstTitle);
title.getLastWord().ifPresent(Word::toUpperFirstTitle);

for (int i = 0; i < (title.getWords().size() - 2); i++) {
if (title.getWords().get(i).endsWithColon()) {
title.getWords().get(i + 1).toUpperFirst();
title.getWords().get(i + 1).toUpperFirstTitle();
}
}

Expand Down
43 changes: 41 additions & 2 deletions src/main/java/org/jabref/logic/formatter/casechanger/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ public final class Word {
*/
public static final Set<String> SMALLER_WORDS;
public static final Set<Character> DASHES;
public static final Set<String> CONJUNCTIONS;
private final char[] chars;
private final boolean[] protectedChars;

static {
Set<String> smallerWords = new HashSet<>();
Set<Character> dashes = new HashSet<>();
Set<String> conjunctions = new HashSet<>();

// Conjunctions
conjunctions.addAll(Arrays.asList("and", "but", "for", "nor", "or", "so", "yet"));
// Articles
smallerWords.addAll(Arrays.asList("a", "an", "the"));
// Prepositions
smallerWords.addAll(Arrays.asList("above", "about", "across", "against", "along", "among", "around", "at", "before", "behind", "below", "beneath", "beside", "between", "beyond", "by", "down", "during", "except", "for", "from", "in", "inside", "into", "like", "near", "of", "off", "on", "onto", "since", "to", "toward", "through", "under", "until", "up", "upon", "with", "within", "without"));
// Conjunctions
smallerWords.addAll(Arrays.asList("and", "but", "for", "nor", "or", "so", "yet"));

smallerWords.addAll(conjunctions);
Copy link
Member

Choose a reason for hiding this comment

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

In both comments it says 'Conjunctions'. Maybe change the comment add write one line, what difference it makes to conjuctions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Putting something like Conjunctions used as part of Title case capitalisation to specifically check if word is conjunction or not and Conjunctions used as part of all case capitalisation to check if it is a small word or not respectively will be fine right?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. Would just be fine just to avoid misunderstanding.

Copy link
Member

Choose a reason for hiding this comment

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

And don't forget the test case. Then we can merge

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure will add the test cases also

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added test cases and modified the corresponding comments.

// Dashes
dashes.addAll(Arrays.asList(
'-', '~', '⸗', '〰', '᐀', '֊', '־', '‐', '‑', '‒',
Expand All @@ -42,6 +45,9 @@ public final class Word {
// unmodifiable for thread safety
DASHES = dashes;

// unmodifiable for thread safety
CONJUNCTIONS = conjunctions;

// unmodifiable for thread safety
SMALLER_WORDS = smallerWords.stream()
.map(word -> word.toLowerCase(Locale.ROOT))
Expand Down Expand Up @@ -87,6 +93,39 @@ public void toLowerCase() {
}

public void toUpperFirst() {
for (int i = 0; i < chars.length; i++) {
if (!protectedChars[i]) {
chars[i] = (i == 0) ?
Character.toUpperCase(chars[i]) :
Character.toLowerCase(chars[i]);
}
}
}

public void toUpperFirstTitle() {
for (int i = 0; i < chars.length; i++) {
if (!protectedChars[i]) {
chars[i] = (i == 0 || (DASHES.contains(chars[i - 1]) && isConjunction(chars, i))) ?
Character.toUpperCase(chars[i]) :
Character.toLowerCase(chars[i]);
}
}
}

private boolean isConjunction(char[] chars, int i) {
String word = "";
while (i < chars.length && !DASHES.contains(chars[i])) {
word += chars[i];
i++;
}
if (CONJUNCTIONS.contains(word)) {
return false;
} else {
return true;
}
}

public void stripConsonants() {
for (int i = 0; i < chars.length; i++) {
if (!protectedChars[i]) {
chars[i] = (i == 0 || DASHES.contains(chars[i - 1])) ?
Expand Down