From d1d9ada490866aa040e10ba7224458e7af244935 Mon Sep 17 00:00:00 2001 From: ashleyfrieze Date: Sat, 3 Jul 2021 17:43:55 +0100 Subject: [PATCH] BAEL-5025 Example code for camel case to words (#10964) --- .../camelcasetowords/CamelCaseToWords.java | 27 ++++++++++++ .../regex/camelcasetowords/Recapitalize.java | 43 +++++++++++++++++++ .../CamelCaseToWordsUnitTest.java | 39 +++++++++++++++++ .../RecapitalizeUnitTest.java | 35 +++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 core-java-modules/core-java-regex-2/src/main/java/com/baeldung/regex/camelcasetowords/CamelCaseToWords.java create mode 100644 core-java-modules/core-java-regex-2/src/main/java/com/baeldung/regex/camelcasetowords/Recapitalize.java create mode 100644 core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/camelcasetowords/CamelCaseToWordsUnitTest.java create mode 100644 core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/camelcasetowords/RecapitalizeUnitTest.java diff --git a/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/regex/camelcasetowords/CamelCaseToWords.java b/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/regex/camelcasetowords/CamelCaseToWords.java new file mode 100644 index 0000000000..ea6593495f --- /dev/null +++ b/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/regex/camelcasetowords/CamelCaseToWords.java @@ -0,0 +1,27 @@ +package com.baeldung.regex.camelcasetowords; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Convert a string in camelCase or TitleCase into a list of words + */ +public class CamelCaseToWords { + private static final Pattern WORD_FINDER = Pattern.compile("(([A-Z]?[a-z]+)|([A-Z]))"); + + /** + * Find the words in mixed case string like ThisIsText or HereIsSomeText + * @param text the text to parse + * @return the list of words to process + */ + public static List findWordsInMixedCase(String text) { + Matcher matcher = WORD_FINDER.matcher(text); + List words = new ArrayList<>(); + while (matcher.find()) { + words.add(matcher.group(0)); + } + return words; + } +} diff --git a/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/regex/camelcasetowords/Recapitalize.java b/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/regex/camelcasetowords/Recapitalize.java new file mode 100644 index 0000000000..f87e59c6cc --- /dev/null +++ b/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/regex/camelcasetowords/Recapitalize.java @@ -0,0 +1,43 @@ +package com.baeldung.regex.camelcasetowords; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Recapitalize { + private static final Set STOP_WORDS = Stream.of("a", "an", "the", "and", + "but", "for", "at", "by", "to", "or") + .collect(Collectors.toSet()); + + public static String sentenceCase(List words) { + List capitalized = new ArrayList<>(); + for (int i = 0; i < words.size(); i++) { + String currentWord = words.get(i); + if (i == 0) { + capitalized.add(capitalizeFirst(currentWord)); + } else { + capitalized.add(currentWord.toLowerCase()); + } + } + return String.join(" ", capitalized) + "."; + } + + public static String capitalizeMyTitle(List words) { + List capitalized = new ArrayList<>(); + for (int i = 0; i < words.size(); i++) { + String currentWord = words.get(i); + if (i == 0 || !STOP_WORDS.contains(currentWord.toLowerCase())) { + capitalized.add(capitalizeFirst(currentWord)); + } else { + capitalized.add(currentWord.toLowerCase()); + } + } + return String.join(" ", capitalized); + } + + private static String capitalizeFirst(String word) { + return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase(); + } +} diff --git a/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/camelcasetowords/CamelCaseToWordsUnitTest.java b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/camelcasetowords/CamelCaseToWordsUnitTest.java new file mode 100644 index 0000000000..a843945bba --- /dev/null +++ b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/camelcasetowords/CamelCaseToWordsUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.regex.camelcasetowords; + +import org.junit.jupiter.api.Test; + +import static com.baeldung.regex.camelcasetowords.CamelCaseToWords.findWordsInMixedCase; +import static org.assertj.core.api.Assertions.assertThat; + +class CamelCaseToWordsUnitTest { + + @Test + void givenPlainStringWithNonLetters_thenFindsWords() { + assertThat(findWordsInMixedCase("some words")) + .containsExactly("some", "words"); + } + + @Test + void givenWordsInCamelCase_thenFindsWords() { + assertThat(findWordsInMixedCase("thisIsCamelCaseText")) + .containsExactly("this", "Is", "Camel", "Case", "Text"); + } + + @Test + void givenWordsInTitleCase_thenFindsWords() { + assertThat(findWordsInMixedCase("ThisIsTitleCaseText")) + .containsExactly("This", "Is", "Title", "Case", "Text"); + } + + @Test + void givenWordsAcrossMultipleTexts_thenFindsWords() { + assertThat(findWordsInMixedCase("ThisIsTitleCaseText --- andSoIsThis")) + .containsExactly("This", "Is", "Title", "Case", "Text", "and", "So", "Is", "This"); + } + + @Test + void givenCamelCaseHasASingleLetterWord_thenItCanBeSplit() { + assertThat(findWordsInMixedCase("thisHasASingleLetterWord")) + .containsExactly("this", "Has", "A", "Single", "Letter", "Word"); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/camelcasetowords/RecapitalizeUnitTest.java b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/camelcasetowords/RecapitalizeUnitTest.java new file mode 100644 index 0000000000..32c8f39372 --- /dev/null +++ b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/camelcasetowords/RecapitalizeUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.regex.camelcasetowords; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static com.baeldung.regex.camelcasetowords.Recapitalize.*; +import static org.assertj.core.api.Assertions.assertThat; + +class RecapitalizeUnitTest { + + @Test + void givenWords_thenCanComposeSentence() { + assertThat(sentenceCase(Arrays.asList("these", "Words", "Form", "A", "Sentence"))) + .isEqualTo("These words form a sentence."); + } + + @Test + void givenNonStopWords_thenTitleIsComposed() { + assertThat(capitalizeMyTitle(Arrays.asList("title", "words", "capitalize"))) + .isEqualTo("Title Words Capitalize"); + } + + @Test + void givenStopWords_thenTitleHasThemInLowerCase() { + assertThat(capitalizeMyTitle(Arrays.asList("this", "is", "A", "title", "with", "a", "stop", "word", "or", "two"))) + .isEqualTo("This Is a Title With a Stop Word or Two"); + } + + @Test + void givenStopWordIsFirstWord_thenTitleHasItCapitalized() { + assertThat(capitalizeMyTitle(Arrays.asList("a", "stop", "word", "first"))) + .isEqualTo("A Stop Word First"); + } +} \ No newline at end of file