-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReduction.java
More file actions
173 lines (157 loc) · 3.89 KB
/
Reduction.java
File metadata and controls
173 lines (157 loc) · 3.89 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Class designed to take a string and reduce it to
* canonical form by stripping it of its common suffixes
*
* This class follows Paice's conflation rules for reducing
* a family of words to a common root
*
* USAGE: "java Reduction <WordToReduce>"
*
* @author Samuel Barr
* @version 11.30.2018
*/
public class Reduction {
public static String reduce(String input) {
//Covert to lowercase & trim extra spaces
input = input.toLowerCase();
input = input.trim();
String output = SS(input);
return output;
}
private static String SS(String s) {
if (s.endsWith("ably")) {
s = cut(s, "ably");
s = IS(s);
}
else if (s.endsWith("ibly")) {
s = cut(s, "ibly");
}
else if (s.endsWith("ily")) {
s = cut(s, "ily");
s = SS(s);
}
else if (s.endsWith("ous")) {
s = cut(s, "ous");
}
else if (s.endsWith("ies")) {
s = cut(s, "ies") + "y";
s = ARY(s);
}
else if (s.endsWith("s")) {
s = cut(s, "s");
s = E(s);
}
else if (s.endsWith("ied")) {
s = cut(s, "ied") + "y";
s = ARY(s);
}
else if (s.endsWith("ed")) {
s = cut(s, "ed");
s = ABL(s);
}
else if (s.endsWith("ing")) {
s = cut(s, "ing");
s = ABL(s);
}
else
s = E(s);
return s;
}
private static String E(String s) {
if (s.endsWith("e")) {
s = cut(s, "e");
s = ABL(s);
}
else if (s.endsWith("al")) {
s = cut(s, "al");
s = ION(s);
}
else
s = ION(s);
return s;
}
private static String ION(String s) {
if (s.endsWith("ion")) {
s = cut(s, "ion");
s = AT(s);
}
else
ARY(s);
return s;
}
private static String ARY(String s) {
if (s.endsWith("ary")) {
s = cut(s, "ary");
}
else if (s.endsWith("ability")) {
s = cut(s, "ability");
s = IS(s);
}
else if (s.endsWith("ibility")) {
s = cut(s, "ibility");
}
else if (s.endsWith("ity")) {
s = cut(s, "ity");
s = IV(s);
}
else if (s.endsWith("ify")) {
s = cut(s, "ify");
}
else
s = ABL(s);
return s;
}
private static String ABL(String s) {
if (s.endsWith("abl")) {
s = cut(s, "abl");
s = IS(s);
}
else if (s.endsWith("ibl")) {
s = cut(s, "ibl");
}
else
s = IV(s);
return s;
}
private static String IV(String s) {
if (s.endsWith("iv")) {
s = cut(s, "iv");
s = AT(s);
}
else
s = AT(s);
return s;
}
private static String AT(String s) {
if (s.endsWith("at")) {
s = cut(s, "at");
s = IS(s);
}
else
s = IS(s);
return s;
}
private static String IS(String s) {
if (s.endsWith("is")) {
s = cut(s, "is");
}
else if (s.endsWith("ific")) {
s = cut(s, "ific");
}
else if (s.endsWith("olv")) {
s = cut(s, "olv") + "olut";
}
return s;
}
/**
* Cuts the suffix off of a given string
* @param input
* @param suffix to cut
*/
private static String cut(String input, String suffix) {
return input.substring(0, input.length() - suffix.length());
}
public static void main(String[] args) {
System.out.println("Reduced: " + reduce(args[0]));
}
}