Skip to content

Commit

Permalink
Anomaly with Title case and Sentence case JabRef#9112
Browse files Browse the repository at this point in the history
  • Loading branch information
sreenath-tm committed Sep 6, 2022
1 parent ce08d0d commit edcb848
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- 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 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)

We fixed an issue where title case was capitalizing Conjunctions after en-dash characters and the Capitalization fix made as part of #9012 has been restricted only to Title case. #9112
### Removed


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.strings.StringUtil;


public class TitleCaseFormatter extends Formatter {

@Override
Expand Down Expand Up @@ -33,8 +34,8 @@ public String format(String input) {
title.getWords().stream().filter(Word::isSmallerWord).forEach(Word::toLowerCase);
title.getWords().stream().filter(Word::isLargerWord).forEach(Word::toUpperFirst);

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()) {
Expand Down
41 changes: 39 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,22 @@ 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<>();

// 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"));

conjunctions.addAll(Arrays.asList("and", "but", "for", "nor", "or", "so", "yet"));
// Dashes
dashes.addAll(Arrays.asList(
'-', '~', '⸗', '〰', '᐀', '֊', '־', '‐', '‑', '‒',
Expand All @@ -42,6 +44,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 @@ -89,13 +94,45 @@ public void toLowerCase() {
public void toUpperFirst() {
for (int i = 0; i < chars.length; i++) {
if (!protectedChars[i]) {
chars[i] = (i == 0 || DASHES.contains(chars[i - 1])) ?
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])) ?
Character.toUpperCase(chars[i]) :
Character.toLowerCase(chars[i]);
}
}
}
public boolean isSmallerWord() {
// "word:" is still a small "word"
return SMALLER_WORDS.contains(this.toString().replace(":", "").toLowerCase(Locale.ROOT));
Expand Down

0 comments on commit edcb848

Please sign in to comment.