-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhitespaceAndPunctuations.java
134 lines (108 loc) · 4.96 KB
/
WhitespaceAndPunctuations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.Arrays;
import java.util.List;
public class WhitespaceAndPunctuations {
private static final List<Character> Punctuations = Arrays.asList('(');
private static final List<Character> FullStopPunctuations = Arrays.asList('.', ':', ',', '!', ')', '?');
public static String normalizeString(String input) {
StringBuilder builder = new StringBuilder();
String normalizedInput = normalizeInput(input);
boolean doubleQuoteEven = false;
char prevChar, nextChar;
int inputLength = normalizedInput.length();
for (int index = 0; index < inputLength; index++) {
char currentChar = normalizedInput.charAt(index);
// If currentChar is a full stop punctuation, remove preceding whitespace and add following whitespace
if (FullStopPunctuations.contains(currentChar)) {
// Remove preceding whitespace
if (index > 0) {
prevChar = normalizedInput.charAt(index - 1);
if (Character.isWhitespace(prevChar)) {
builder.deleteCharAt(builder.length() - 1);
}
}
// Append character
builder.append(currentChar);
// Add following whitespace if needed
if (index < inputLength - 1) {
nextChar = normalizedInput.charAt(index + 1);
if (!FullStopPunctuations.contains(nextChar)) {
builder.append(" ");
}
}
}
// Else if currentChar is a normal punctuation, add preceding whitespace
else if (Punctuations.contains(currentChar)) {
// Add preceding whitespace if needed
if (index > 0) {
prevChar = normalizedInput.charAt(index - 1);
if (!Character.isWhitespace(prevChar)) {
builder.append(" ");
}
}
// Append character
builder.append(currentChar);
}
// Whitespace character, don't append it if prev is a punctuation character
else if (Character.isWhitespace(currentChar)) {
if (index > 0) {
prevChar = normalizedInput.charAt(index - 1);
if (!Punctuations.contains(prevChar) && !FullStopPunctuations.contains(prevChar)) {
builder.append(currentChar);
}
}
}
// If currentChar is a double quote
else if (currentChar == '"') {
// If it is the first quote (opening quote), add preceding whitespace and remove following whitespace
if (!doubleQuoteEven) {
// Add preceding whitespace if needed
if (index > 0) {
prevChar = normalizedInput.charAt(index - 1);
if (!Character.isWhitespace(prevChar)) {
builder.append(" ");
}
}
// Append currentChar
builder.append(currentChar);
// Remove following whitespace
if (index < inputLength - 1) {
nextChar = normalizedInput.charAt(index + 1);
if (Character.isWhitespace(nextChar)) {
index += 1;
}
}
doubleQuoteEven = true;
}
// If it is the second quote (closing quote), remove preceding whitespace and add following whitespace
else {
// Remove preceding whitespace if needed
if (index > 0) {
prevChar = normalizedInput.charAt(index - 1);
if (Character.isWhitespace(prevChar)) {
builder.deleteCharAt(builder.length() - 1);
}
}
// Append currentChar
builder.append(currentChar);
// Add following character
if (index < inputLength - 1) {
nextChar = normalizedInput.charAt(index + 1);
if (!Character.isWhitespace(nextChar) && !Punctuations.contains(nextChar) && !FullStopPunctuations.contains(nextChar)) {
builder.append(" ");
}
}
doubleQuoteEven = false;
}
}
// All others, just append it
else {
builder.append(currentChar);
}
}
return normalizeInput(builder.toString());
}
private static String normalizeInput(String input) {
String newInput = input.trim();
return newInput.replaceAll("\\s+", " ");
}
}