To do this absolutely correctly, you'd need a dictionary.
If you are willing to "come close", you could probably figure out some algorithm. But good luck finding an algorithm that can correctly parse "algorithm". (Well, maybe you could...the ending "thm" is unusual enough that you could probably special case it.)
Interesting that there aren't tons of hits from doing