Created
June 18, 2014 20:48
-
-
Save siddMahen/048986428ddf580ae2e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function m(word::String) | |
str = split(word, "") | |
seeking = true | |
sawvowel = false | |
sawconsonant = false | |
count = 0 | |
for w in str | |
if seeking && isvowel(w) | |
seeking = false | |
end | |
if !seeking | |
if isvowel(w) | |
sawvowel = true | |
else | |
sawconsonant = true | |
if sawvowel | |
count += 1 | |
sawvowel = false | |
sawconsonant = false | |
end | |
end | |
end | |
end | |
return count | |
end | |
function hassuffix(word::String, suffix::String) | |
str = split(word, "") | |
ending = split(suffix, "") | |
if length(str) < length(ending) | |
return false | |
end | |
if length(ending) == 1 | |
return str[end] == ending[1] | |
else | |
for i = 1:length(ending) | |
if str[end - length(ending) + i] == ending[i] | |
continue | |
else | |
return false | |
end | |
end | |
end | |
return true | |
end | |
function replace_suffix(word::String, from::String, to::String) | |
str = split(word, "") | |
old = split(from, "") | |
if length(old) == 1 | |
pop!(str) | |
else | |
for i = 1:length(old) | |
pop!(str) | |
end | |
end | |
return join(str, "") * to | |
end | |
macro rule(ex) | |
local rules = eval(ex) | |
if length(rules) > 0 | |
(k, v) = shift!(rules) | |
return esc(quote | |
if hassuffix(word, $k) | |
word = replace_suffix(word, $k, $v) | |
else | |
@rule $rules | |
end | |
end) | |
else | |
return :(nothing) | |
end | |
end | |
function isvowel(w::String) | |
const vowels = Set("a","e","i","o","u") | |
return w in vowels | |
end | |
function containsvowel(word::String) | |
str = split(word, "") | |
for l in str | |
if isvowel(l) | |
return true | |
end | |
end | |
return false | |
end | |
function endswith(word::String, letter::String) | |
str = split(word,"") | |
if str[end] == letter | |
return true | |
else | |
return false | |
end | |
end | |
function endsdouble(word::String) | |
const consonants = Set("b","c","d","f","g","h","j", | |
"k","l","m","n","p","q","r", | |
"s","t","v","w","x","y","z") | |
str = split(word,"") | |
for c in consonants | |
if str[end] == c && str[end - 1] == c | |
return true | |
end | |
end | |
return false | |
end | |
function endscvc(word::String) | |
const consonants = Set("b","c","d","f","g","h","j", | |
"k","l","m","n","p","q","r", | |
"s","t","v","w","x","y","z") | |
const vowels = Set("a","e","i","o","u") | |
const special = Set("w","x","y") | |
str = split(word,"") | |
for v in vowels, c in consonants, w in setdiff(consonants, special) | |
if str[end - 2] == v && str[end - 1] == c && str[end] == w | |
return true | |
end | |
end | |
return false | |
end | |
function stem!(word::String) | |
@rule [ | |
("sses", "ss"), | |
("ies", "i"), | |
("ss", "ss"), | |
("s", "") | |
] | |
if m(word) > 0 | |
@rule [ ("eed", "ee") ] | |
end | |
if containsvowel(word) | |
@rule [ | |
("ed",""), | |
("ing","") | |
] | |
@rule [ | |
("at","ate"), | |
("bl","ble"), | |
("iz","ize") | |
] | |
if endsdouble(word) && !(endswith(word,"l") || endswith(word,"s") || endswith(word,"z")) | |
str = split(word,"") | |
pop!(str) | |
word = join(str,"") | |
end | |
if m(word) == 1 && endscvc(word) | |
str = split(word,"") | |
push!(str,"e") | |
word = join(str,"") | |
end | |
end | |
if containsvowel(word) | |
@rule [ ("y","i") ] | |
end | |
if m(word) > 0 | |
@rule [ | |
("ational", "ate"), | |
("tional", "tion"), | |
("enci", "ence"), | |
("anci", "ance"), | |
("izer", "ize"), | |
("abli", "able"), | |
("alli", "al"), | |
("entli", "ent"), | |
("eli", "e"), | |
("ousli", "ous"), | |
("ization", "ize"), | |
("ation", "ate"), | |
("ator", "ate"), | |
("alism", "al"), | |
("iveness", "ive"), | |
("fulness", "ful"), | |
("ousness", "ous"), | |
("aliti", "al"), | |
("iviti", "ive"), | |
("biliti", "ble") | |
] | |
end | |
if m(word) > 0 | |
@rule [ | |
("icate", "ic"), | |
("ative", ""), | |
("alize", "al"), | |
("iciti", "ic"), | |
("ical", "ic"), | |
("ful", ""), | |
("ness", "") | |
] | |
end | |
if m(word) > 1 | |
@rule [ | |
("al", ""), | |
("ance", ""), | |
("ence", ""), | |
("er", ""), | |
("ic", ""), | |
("able", ""), | |
("ible", ""), | |
("ant", ""), | |
("ement", ""), | |
("ment", ""), | |
("ent", ""), | |
("ou", ""), | |
("ism", ""), | |
("ate", ""), | |
("iti", ""), | |
("ous", ""), | |
("ive", ""), | |
("ize", "") | |
] | |
# this is wierd | |
if endswith(word,"s") || endswith(word,"t") | |
@rule [ ("ion", "") ] | |
end | |
end | |
if m(word) > 1 | |
@rule [ ("e", "") ] | |
elseif m(word) == 1 | |
if !endscvc(word) | |
@rule [ ("e", "") ] | |
end | |
end | |
if m(word) > 1 && endsdouble(word) && endswith(word, "l") | |
str = split(word, "") | |
pop!(str) | |
word = join(str, "") | |
end | |
return word | |
end | |
println(stem!("calculations")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment