Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strings: fix Title function #21442

Closed
GwynethLlewelyn opened this issue Aug 14, 2017 · 5 comments
Closed

strings: fix Title function #21442

GwynethLlewelyn opened this issue Aug 14, 2017 · 5 comments

Comments

@GwynethLlewelyn
Copy link

GwynethLlewelyn commented Aug 14, 2017

First of all: no, this is not #6801!

Instead, I just wanted to point out that the current strings.Title() implementation is a 'quick & dirty' way of dealing with titles; it does not respect proper English rules for title naming (although it might be appropriate for some languages, I don't know).

A suggestion was made on the Go Cookbook to implement it thusly:

func properTitle(input string) string {
	words := strings.Fields(input)
	smallwords := " a an on the to "

	for index, word := range words {
		if strings.Contains(smallwords, " "+word+" ") {
			words[index] = word
		} else {
			words[index] = strings.Title(word)
		}
	}
	return strings.Join(words, " ")
}

This is a clever and neat trick, and could be used for other languages as well (ex. in Portuguese the smallwords would have to include " de do da dos das " but possibly exclude "a" since that is by coincidence also a definite article, etc.).
The authors of the Go Cookbook did not seem to add a suggestion here, so I'm doing it 🙂

@ianlancetaylor ianlancetaylor changed the title Suggestion for pkg strings: implement proper Title function? (note: NO, this is not about word boundaries!) strings: fix Title function Aug 14, 2017
@ianlancetaylor
Copy link
Contributor

Can you give an example of an input that would change under your proposal?

CC @mpvl

@GwynethLlewelyn
Copy link
Author

Sure, the best example is on the Go Cookbook itself:

strings.Title("welcome to the dollhouse!") gives Welcome To The Dollhouse!

while

properTitle("welcome to the dollhouse!") gives Welcome to the Dollhouse!

which is grammatically correct (while the result from strings.Title() is not)

@bep
Copy link
Contributor

bep commented Aug 14, 2017

As I suspect changing the existing strings.Title would be a hard sell, I wanted to point to a great third-party Go lib with both AP style and Chicago style titles:

https://godoc.org/github.com/jdkato/prose/transform

@ianlancetaylor
Copy link
Contributor

Ah, I understand now. No, I don't think we can or should change strings.Title in that way. strings.Title acts as documented. Changing it would make it a different function, and would break backward compatibility. I agree that the sort of function you describe is useful, but it's not strings.Title. Also the function you describe is language-specific, and there are no such functions in the strings package.

Closing.

@GwynethLlewelyn
Copy link
Author

I agree that strings.Title works exactly as described, it's just that the name of the function that is misleading! On the other hand, you're totally right that this is language-specific, and none of the other functions in strings have such a limitation (one thing is handling with runes — which are language-specific but easy to handle — the other thing is handling with grammar, so I get your point!).
Aye, there are plenty of ways of doing it 'the right way' and while I hadn't come across the prose series of packages, implementing 'proper' title capitalisation is not exactly rocket science. Also, I now understand that 'Title Case' (which is what strings.Title implements...) may mean something significantly different in the software development environment compared to the grammatical definition. So I should have done my research before making a suggestion here! Sorry about that.

@golang golang locked and limited conversation to collaborators Aug 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants