Correct the article in generated documents

For example:

  "a Ingress" > "an Ingress"
This commit is contained in:
Angus Salkeld
2016-10-26 09:54:02 +10:00
parent 8ca348a7a0
commit 3a08cf0619
11 changed files with 135 additions and 32 deletions

View File

@@ -17,8 +17,10 @@ limitations under the License.
package strings
import (
"fmt"
"path"
"strings"
"unicode"
)
// Splits a fully qualified name and returns its namespace and name.
@@ -45,3 +47,30 @@ func ShortenString(str string, n int) string {
return str[:n]
}
}
// GetArticleForNoun returns the article needed for the given noun.
func GetArticleForNoun(noun string, padding string) string {
if noun[len(noun)-2:] != "ss" && noun[len(noun)-1:] == "s" {
// Plurals don't have an article.
// Don't catch words like class
return fmt.Sprintf("%v", padding)
}
article := "a"
if isVowel(rune(noun[0])) {
article = "an"
}
return fmt.Sprintf("%s%s%s", padding, article, padding)
}
// isVowel returns true if the rune is a vowel (case insensitive).
func isVowel(c rune) bool {
vowels := []rune{'a', 'e', 'i', 'o', 'u'}
for _, value := range vowels {
if value == unicode.ToLower(c) {
return true
}
}
return false
}