mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Omit needless words: allow removal of words following a gerund.
Identify gerunds by stripping off the "ing" and looking for a verb. This lets us transform, e.g., "stringByAppendingString" to "stringByAppending", since "append" is a verb. Swift SVN r31660
This commit is contained in:
@@ -48,6 +48,31 @@ PartOfSpeech swift::getPartOfSpeech(StringRef word) {
|
||||
return PartOfSpeech::Verb;
|
||||
#include "PartsOfSpeech.def"
|
||||
|
||||
// Identify gerunds, which always end in "ing".
|
||||
if (word.endswith("ing") && word.size() > 4) {
|
||||
StringRef possibleVerb = word.substr(0, word.size()-3);
|
||||
|
||||
// If what remains is a verb, we have a gerund.
|
||||
if (getPartOfSpeech(possibleVerb) == PartOfSpeech::Verb)
|
||||
return PartOfSpeech::Gerund;
|
||||
|
||||
// Try adding an "e" and look for that as a verb.
|
||||
if (possibleVerb.back() != 'e') {
|
||||
SmallString<16> possibleVerbWithE;
|
||||
possibleVerbWithE += possibleVerb;
|
||||
possibleVerbWithE += 'e';
|
||||
if (getPartOfSpeech(possibleVerbWithE) == PartOfSpeech::Verb)
|
||||
return PartOfSpeech::Gerund;
|
||||
}
|
||||
|
||||
// If there is a repeated letter at the back, drop that second
|
||||
// instance of that letter and try again.
|
||||
unsigned count = possibleVerb.size();
|
||||
if (possibleVerb[count-1] == possibleVerb[count-2] &&
|
||||
getPartOfSpeech(possibleVerb.substr(0, count-1)) == PartOfSpeech::Verb)
|
||||
return PartOfSpeech::Gerund;
|
||||
}
|
||||
|
||||
return PartOfSpeech::Unknown;
|
||||
}
|
||||
|
||||
@@ -362,6 +387,7 @@ StringRef swift::omitNeedlessWords(StringRef name, StringRef typeName,
|
||||
switch (getPartOfSpeech(*nameWordRevIter)) {
|
||||
case PartOfSpeech::Preposition:
|
||||
case PartOfSpeech::Verb:
|
||||
case PartOfSpeech::Gerund:
|
||||
// Okay to strip off the part of the name that is redundant with
|
||||
// type information.
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user