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

Implemented branch coverage tool for addDotIfAbbrevation() #5951

Closed
Closed
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
79 changes: 78 additions & 1 deletion src/main/java/org/jabref/model/entry/Author.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.jabref.model.entry;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Objects;
import java.util.Optional;

Expand All @@ -15,6 +18,7 @@
* all other methods are provided for completeness.
*/
public class Author {
static boolean[] visited = new boolean[32];

private final String firstPart;

Expand Down Expand Up @@ -55,90 +59,163 @@ public Author(String first, String firstabbr, String von, String last, String jr
jrPart = removeStartAndEndBraces(jr);
}

@SuppressWarnings("checkstyle:WhitespaceAround")
public static String addDotIfAbbreviation(String name) {
if ((name == null) || name.isEmpty()) {
visited[0] = true;
getBranchCoverage(visited);
return name;
} else {
visited[1] = true;
}
// If only one character (uppercase letter), add a dot and return immediately:
if ((name.length() == 1) && Character.isLetter(name.charAt(0)) &&
Character.isUpperCase(name.charAt(0))) {
visited[2] = true;
getBranchCoverage(visited);
return name + ".";
} else {
visited[3] = true;
}

StringBuilder sb = new StringBuilder();
char lastChar = name.charAt(0);
for (int i = 0; i < name.length(); i++) {
visited[4] = true;
if (i > 0) {
visited[5] = true;
lastChar = name.charAt(i - 1);
} else {
visited[6] = true;
}
char currentChar = name.charAt(i);
sb.append(currentChar);

if (currentChar == '.') {
visited[7] = true;
// A.A. -> A. A.
if (((i + 1) < name.length()) && Character.isUpperCase(name.charAt(i + 1))) {
visited[8] = true;
sb.append(' ');
}
else {
visited[9] = true;
}
} else {
visited[10] = true;
}

boolean currentIsUppercaseLetter = Character.isLetter(currentChar) && Character.isUpperCase(currentChar);
if (!currentIsUppercaseLetter) {
visited[11] = true;
// No uppercase letter, hence nothing to do
continue;
} else {
visited[12] = true;
}

boolean lastIsLowercaseLetter = Character.isLetter(lastChar) && Character.isLowerCase(lastChar);
if (lastIsLowercaseLetter) {
visited[13] = true;
// previous character was lowercase (probably an acronym like JabRef) -> don't change anything
continue;
} else {
visited[14] = true;
}

if ((i + 1) >= name.length()) {
visited[15] = true;
// Current character is last character in input, so append dot
sb.append('.');
continue;
} else {
visited[16] = true;
}

char nextChar = name.charAt(i + 1);
if ('-' == nextChar) {
visited[17] = true;
// A-A -> A.-A.
sb.append(".");
continue;
} else {
visited[18] = true;
}
if ('.' == nextChar) {
visited[19] = true;
// Dot already there, so nothing to do
continue;
} else {
visited[20] = true;
}

// AA -> A. A.
// Only append ". " if the rest of the 'word' is uppercase
boolean nextWordIsUppercase = true;
char furtherChar = Character.MIN_VALUE;
for (int j = i + 1; j < name.length(); j++) {
visited[21] = true;
furtherChar = name.charAt(j);
if (Character.isWhitespace(furtherChar) || (furtherChar == '-') || (furtherChar == '~') || (furtherChar == '.')) {
visited[22] = true;
// end of word
break;
} else {
visited[23] = true;
}

boolean furtherIsUppercaseLetter = Character.isLetter(furtherChar) && Character.isUpperCase(furtherChar);
if (!furtherIsUppercaseLetter) {
visited[24] = true;
nextWordIsUppercase = false;
break;
} else {
visited[25] = true;
}

}
if (!(name.length() > i + 1)) {
visited[31] = true;
} else {
visited[30] = true;
}
if (nextWordIsUppercase) {
visited[26] = true;
if (Character.isWhitespace(furtherChar)) {
visited[27] = true;
sb.append(".");
} else {
visited[28] = true;
sb.append(". ");
}
} else {
visited[29] = true;
}
}

getBranchCoverage(visited);
return sb.toString().trim();
}

private static void getBranchCoverage(boolean[] visited) {
try {
File directory = new File("/Temp");
if (!directory.exists()) {
directory.mkdir();
}
File f = new File(directory + "/addDotIfAbbreviation.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
double frac = 0;
for (int i = 0; i < visited.length; ++i) {
frac += (visited[i] ? 1 : 0);
bw.write("branch " + i + " was" + (visited[i] ? " visited." : " not visited.") + "\n");
}
bw.write("" + frac / visited.length);
bw.close();
} catch (Exception e) {
System.err.println("ye");
}
}

@Override
public int hashCode() {
return Objects.hash(firstAbbr, firstPart, jrPart, lastPart, vonPart);
Expand Down