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:
Doug Gregor
2015-09-03 05:44:20 +00:00
parent 271fc2057e
commit c3497fb378
4 changed files with 39 additions and 1 deletions

View File

@@ -40,7 +40,8 @@ namespace swift {
enum class PartOfSpeech { enum class PartOfSpeech {
Unknown, Unknown,
Preposition, Preposition,
Verb Verb,
Gerund,
}; };
/// Determine the part of speech for the given word. /// Determine the part of speech for the given word.

View File

@@ -48,6 +48,31 @@ PartOfSpeech swift::getPartOfSpeech(StringRef word) {
return PartOfSpeech::Verb; return PartOfSpeech::Verb;
#include "PartsOfSpeech.def" #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; return PartOfSpeech::Unknown;
} }
@@ -362,6 +387,7 @@ StringRef swift::omitNeedlessWords(StringRef name, StringRef typeName,
switch (getPartOfSpeech(*nameWordRevIter)) { switch (getPartOfSpeech(*nameWordRevIter)) {
case PartOfSpeech::Preposition: case PartOfSpeech::Preposition:
case PartOfSpeech::Verb: case PartOfSpeech::Verb:
case PartOfSpeech::Gerund:
// Okay to strip off the part of the name that is redundant with // Okay to strip off the part of the name that is redundant with
// type information. // type information.
break; break;

View File

@@ -61,6 +61,11 @@
// Note: don't map base name down to a keyword. // Note: don't map base name down to a keyword.
// CHECK-FOUNDATION: func doSelector(selector: Selector) // CHECK-FOUNDATION: func doSelector(selector: Selector)
// Note: Strip names preceded by a gerund.
// CHECK-FOUNDATION: func startSquashing(bee: Bee)
// CHECK-FOUNDATION: func startSoothing(bee: Bee)
// CHECK-FOUNDATION: func startShopping(bee: Bee)
// Note: class method name stripping result type. // Note: class method name stripping result type.
// CHECK-APPKIT: class func red() -> NSColor // CHECK-APPKIT: class func red() -> NSColor

View File

@@ -868,3 +868,9 @@ __attribute__((availability(macosx,introduced=10.11)))
@interface NSObject (Silly) @interface NSObject (Silly)
-(void)doSelector:(SEL)selector; -(void)doSelector:(SEL)selector;
@end @end
@interface Bee (Gerunds)
- (void)startSquashingBee:(nonnull Bee *)bee;
- (void)startSoothingBee:(nonnull Bee *)bee;
- (void)startShoppingBee:(nonnull Bee *)bee;
@end