From 31c3b96018de8816d7814333b63425a6fddc0a5f Mon Sep 17 00:00:00 2001 From: Oleksii Aleshchenko Date: Thu, 11 May 2023 15:46:43 +0300 Subject: [PATCH] update Variant0.java --- src/lab3/Variant0.java | 54 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/src/lab3/Variant0.java b/src/lab3/Variant0.java index 647adb7..b11def5 100644 --- a/src/lab3/Variant0.java +++ b/src/lab3/Variant0.java @@ -1,17 +1,65 @@ package lab3; import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; /** * Знайти найбільшу кількість речень заданого тексту, в яких є однакові слова. */ public class Variant0 { public static void main(String[] args) { - String textString = "A, a. B a. B a. B a. C."; - final String[] sentencesStrings = textString.split("\\. ?"); + System.out.println("\\"); + String textString = "Aa, a b. Bb a? B a! B a. Ccc."; +// final String[] sentencesStrings = textString.split("(?<=(\\.|!|\\?)) "); +// final String[] sentencesStrings = textString.split("(?<=[.!?]) "); + final String[] sentencesStrings = textString.split("[.!?] ?"); for (String sentencesString : sentencesStrings) { - System.out.println(sentencesString); + System.out.println("[" + sentencesString + "]"); } + System.out.println("++++++++++++"); + + final String[][] wordsStringsBySentences = new String[sentencesStrings.length][]; + for (int i = 0; i < sentencesStrings.length; i++) { + wordsStringsBySentences[i] = sentencesStrings[i].split(",? "); + } + + for (String[] wordsStrings : wordsStringsBySentences) { + for (String wordsString : wordsStrings) { + System.out.print("[" + wordsString + "] - "); + } + System.out.println(); + } + + Set originalWordsStrings = new HashSet(); + for (String[] wordsStrings : wordsStringsBySentences) { + for (String wordsString : wordsStrings) { + originalWordsStrings.add(wordsString.toLowerCase()); + } + } + final String[] originalWordsArray = originalWordsStrings.toArray(new String[0]); + + System.out.println("++++++++++++"); + for (Object originalWordsString : originalWordsStrings) { + System.out.println(originalWordsString); + } + + int[] entersQuantitiesOriginalWordsInSentences = new int[originalWordsArray.length]; + for (int i = 0; i < originalWordsArray.length; i++) { + String originalWord = originalWordsArray[i]; + for (String[] words : wordsStringsBySentences) { + for (String word : words) { + if (originalWord.equalsIgnoreCase(word)) { + entersQuantitiesOriginalWordsInSentences[i]++; + break; + } + } + } + } + +// todo show greatest value from entersQuantitiesOriginalWordsInSentences + // System.out.println(Arrays.toString(sentencesStrings)); System.out.println("++++++++++++"); }