Text formatting algorithm for pascal or camel body

Using this question , as a basis there is an example of an alogram or coding for changing any text in Pascal or Camel.

For example:

mynameisfred 

becomes

 Camel: myNameIsFred Pascal: MyNameIsFred 
+7
algorithm coding-style camelcasing pascalcasing
source share
2 answers

I found a thread with a bunch of Perl guys who argued about this question at http://www.perlmonks.org/?node_id=336331 .

I hope this is not too much of an answer to the question, but I would say that you have a small problem in that it will be a very open algorithm that can have many β€œmisses”, as well as hits. For example, let's say you entered: -

 camelCase("hithisisatest"); 

The conclusion could be: -

 "hiThisIsATest" 

Or: -

 "hiThisIsATest" 

In no way did the algorithm know what to prefer. You could add additional code to indicate that you prefer more common words, but misses will happen again (Peter Norwig wrote a very small spelling corrector at http://norvig.com/spell-correct.html , which can help the algorithm, I wrote a C # implementation if C # is your language).

I agree with Mark and say that you would be better off having an algorithm that accepts a split input, i.e. this_is_a_test and converts it. This would be easy to implement, i.e. in the pseudo-code: -

 SetPhraseCase(phrase, CamelOrPascal): if no delimiters if camelCase return lowerFirstLetter(phrase) else return capitaliseFirstLetter(phrase) words = splitOnDelimiter(phrase) if camelCase ret = lowerFirstLetter(first word) else ret = capitaliseFirstLetter(first word) for i in 2 to len(words): ret += capitaliseFirstLetter(words[i]) return ret capitaliseFirstLetter(word): if len(word) <= 1 return upper(word) return upper(word[0]) + word[1..len(word)] lowerFirstLetter(word): if len(word) <= 1 return lower(word) return lower(word[0]) + word[1..len(word)] 

You can also replace my capitaliseFirstLetter () function with the correct case algorithm if you wish.

The C # implementation of the above algorithm is as follows (full console program with a test harness): -

 using System; class Program { static void Main(string[] args) { var caseAlgorithm = new CaseAlgorithm('_'); while (true) { string input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) return; Console.WriteLine("Input '{0}' in camel case: '{1}', pascal case: '{2}'", input, caseAlgorithm.SetPhraseCase(input, CaseAlgorithm.CaseMode.CamelCase), caseAlgorithm.SetPhraseCase(input, CaseAlgorithm.CaseMode.PascalCase)); } } } public class CaseAlgorithm { public enum CaseMode { PascalCase, CamelCase } private char delimiterChar; public CaseAlgorithm(char inDelimiterChar) { delimiterChar = inDelimiterChar; } public string SetPhraseCase(string phrase, CaseMode caseMode) { // You might want to do some sanity checks here like making sure // there no invalid characters, etc. if (string.IsNullOrEmpty(phrase)) return phrase; // .Split() will simply return a string[] of size 1 if no delimiter present so // no need to explicitly check this. var words = phrase.Split(delimiterChar); // Set first word accordingly. string ret = setWordCase(words[0], caseMode); // If there are other words, set them all to pascal case. if (words.Length > 1) { for (int i = 1; i < words.Length; ++i) ret += setWordCase(words[i], CaseMode.PascalCase); } return ret; } private string setWordCase(string word, CaseMode caseMode) { switch (caseMode) { case CaseMode.CamelCase: return lowerFirstLetter(word); case CaseMode.PascalCase: return capitaliseFirstLetter(word); default: throw new NotImplementedException( string.Format("Case mode '{0}' is not recognised.", caseMode.ToString())); } } private string lowerFirstLetter(string word) { return char.ToLower(word[0]) + word.Substring(1); } private string capitaliseFirstLetter(string word) { return char.ToUpper(word[0]) + word.Substring(1); } } 
+3
source share

The only way to do this is to run every part of the word through a dictionary.

"mynameisfred" is just an array of characters dividing it into my name. Fred means understanding what the combination of each of these characters means.

You can do this easily if your input was somehow shared, for example. "my name is fred" or "my_name_is_fred".

0
source share

All Articles