Created
June 11, 2011 22:40
-
-
Save vmlemon/1021046 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include "multitool.h" | |
| #include "ui_multitool.h" | |
| MultiTool::MultiTool(QWidget *parent) : | |
| QMainWindow(parent), | |
| ui(new Ui::MultiTool) | |
| { | |
| ui->setupUi(this); | |
| } | |
| MultiTool::~MultiTool() | |
| { | |
| delete ui; | |
| } | |
| /* | |
| TODO: Figure out an optimal method of working with forenames/surnames contextually. | |
| */ | |
| QRegExp iProperNouns("Thames|Cannes|Swiss", Qt::CaseInsensitive); | |
| QRegExp iWordTerminators("es|s", Qt::CaseInsensitive); | |
| enum TMode { | |
| EPlural, //Word is plural | |
| ESingular, //Word is singular | |
| EAntonym, //Word is an antonym | |
| ESynonym //Word is a synonym | |
| }; | |
| /* | |
| Return either the plural form of a singluar word, | |
| or the singular form of a plural word. | |
| */ | |
| QString MultiTool::LocatePlural(QString aWord, int aMode) { | |
| /* This lexicon is for nouns */ | |
| QMap<QString, QString> lexicon; | |
| lexicon.insert("dog", "dogs"); | |
| lexicon.insert("basket", "baskets"); | |
| lexicon.insert("car", "cars"); | |
| lexicon.insert("child", "children"); | |
| lexicon.insert("deer", "deer"); | |
| lexicon.insert("berry", "berries"); | |
| lexicon.insert("pony", "ponies"); | |
| lexicon.insert("orange", "oranges"); | |
| lexicon.insert("source", "sources"); | |
| lexicon.insert("sauce", "sauces"); | |
| lexicon.insert("foe", "foes"); | |
| lexicon.insert("shell", "shells"); | |
| /* | |
| In certain contexts, the plural of "fish" can be either | |
| "fish" or "fishes". | |
| */ | |
| lexicon.insert("fish", "fish"); | |
| lexicon.insert("sheep", "sheep"); | |
| lexicon.insert("tweet", "tweets"); | |
| lexicon.insert("vertex", "vertices"); | |
| lexicon.insert("potato", "potatoes"); | |
| lexicon.insert("mouse", "mice"); | |
| if (aMode == ESingular) { | |
| return(lexicon.value(aWord.toLower())); | |
| } | |
| if (aMode == EPlural) | |
| { | |
| return(lexicon.key(aWord.toLower())); | |
| } | |
| else { | |
| return(""); | |
| qDebug() << "No mode"; | |
| } | |
| } | |
| /* Convert a plural word into a singular word */ | |
| QString MultiTool::Depluralise(QString aPluralWord) | |
| { | |
| /* Attempt to handle "echoes", "tomatoes" and "potatoes" */ | |
| if (aPluralWord.endsWith("oes")) { | |
| /* Hacky - but "foes" is another special case */ | |
| if (aPluralWord.endsWith("foes")) { | |
| return aPluralWord.replace("oes", "oe"); | |
| } | |
| else | |
| { | |
| return aPluralWord.replace("oes", "o"); | |
| } | |
| } | |
| /* "Boxes", "foxes" etc. */ | |
| if (aPluralWord.endsWith("es") || aPluralWord.endsWith("s")) | |
| { | |
| /* "Ponies", "Berries", "Batteries" etc. */ | |
| if (aPluralWord.endsWith("ies")) { | |
| return aPluralWord.replace("ies","y"); | |
| } | |
| /* "Sources", "Sauces", "Races" etc. */ | |
| if (aPluralWord.endsWith("ces")) { | |
| return aPluralWord.replace("ces","ce"); | |
| } | |
| /* "Buns", "Suns", "Runs" etc. */ | |
| if (aPluralWord.endsWith("uns")) { | |
| return aPluralWord.replace(QRegExp("uns"),"un"); | |
| } | |
| /* "Datsons", "Davidsons" etc. */ | |
| if (aPluralWord.endsWith("ons")) { | |
| return aPluralWord.replace(QRegExp("ons"),"on"); | |
| } | |
| /* "Waffles" etc. */ | |
| if (aPluralWord.endsWith("les")) { | |
| return aPluralWord.replace(QRegExp("les"),"le"); | |
| } | |
| /* "Bills", "Hells", "Shells" etc. */ | |
| if (aPluralWord.endsWith("lls")) { | |
| return aPluralWord.replace(QRegExp("lls"),"ll"); | |
| } | |
| /* "Sausages", "Mortgages" etc. */ | |
| if (aPluralWord.endsWith("ges")) { | |
| return aPluralWord.replace(QRegExp("ges"),"ge"); | |
| } | |
| /* | |
| Proper nouns shouldn't be treated as plurals. | |
| */ | |
| if (aPluralWord.toLower().contains(iProperNouns)){ | |
| return aPluralWord; | |
| } | |
| else | |
| { | |
| return aPluralWord.remove(iWordTerminators); | |
| } | |
| } | |
| else | |
| { | |
| return LocatePlural(aPluralWord, EPlural); | |
| } | |
| } | |
| /* Return the "opposite" of a given word */ | |
| QString MultiTool::LocateAntonym(QString aWord) { | |
| QMap <QString, QString> lexicon; | |
| /* Lexicon of basic antonyms */ | |
| lexicon.insert("antonym", "synonym"); | |
| lexicon.insert("slow", "fast"); | |
| lexicon.insert("off", "on"); | |
| lexicon.insert("small", "large"); | |
| lexicon.insert("input", "output"); | |
| return lexicon.value(aWord); | |
| } | |
| void MultiTool::on_DepluraliseButton_clicked() | |
| { | |
| ui->Result->setText(Depluralise(ui->GivenWord->text())); | |
| } | |
| void MultiTool::on_LocatePluralButton_clicked() | |
| { | |
| ui->Result->setText(LocatePlural(ui->GivenWord->text(), ESingular)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment