diff --git a/lab5/lab5.iml b/lab5/lab5.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/lab5/lab5.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/lab5/out/production/lab5/lab5/Letter.class b/lab5/out/production/lab5/lab5/Letter.class new file mode 100644 index 0000000..7fe3e5a Binary files /dev/null and b/lab5/out/production/lab5/lab5/Letter.class differ diff --git a/lab5/out/production/lab5/lab5/Main.class b/lab5/out/production/lab5/lab5/Main.class new file mode 100644 index 0000000..b80468f Binary files /dev/null and b/lab5/out/production/lab5/lab5/Main.class differ diff --git a/lab5/out/production/lab5/lab5/Punctuation.class b/lab5/out/production/lab5/lab5/Punctuation.class new file mode 100644 index 0000000..aac5b33 Binary files /dev/null and b/lab5/out/production/lab5/lab5/Punctuation.class differ diff --git a/lab5/out/production/lab5/lab5/Sentence.class b/lab5/out/production/lab5/lab5/Sentence.class new file mode 100644 index 0000000..d168417 Binary files /dev/null and b/lab5/out/production/lab5/lab5/Sentence.class differ diff --git a/lab5/out/production/lab5/lab5/Text.class b/lab5/out/production/lab5/lab5/Text.class new file mode 100644 index 0000000..1227004 Binary files /dev/null and b/lab5/out/production/lab5/lab5/Text.class differ diff --git a/lab5/out/production/lab5/lab5/Word.class b/lab5/out/production/lab5/lab5/Word.class new file mode 100644 index 0000000..1ee4a26 Binary files /dev/null and b/lab5/out/production/lab5/lab5/Word.class differ diff --git a/lab5/src/lab5/Letter.java b/lab5/src/lab5/Letter.java new file mode 100644 index 0000000..cb78fdc --- /dev/null +++ b/lab5/src/lab5/Letter.java @@ -0,0 +1,5 @@ +package lab5; + +public record Letter(char value) { + +} diff --git a/lab5/src/lab5/Main.java b/lab5/src/lab5/Main.java new file mode 100644 index 0000000..10ffc25 --- /dev/null +++ b/lab5/src/lab5/Main.java @@ -0,0 +1,38 @@ +package lab5; + +import java.util.*; + +public class Main { + + public static void main(String[] args) { + String inputText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tempor dictum velit, vitae sollicitudin mauris facilisis et. Praesent mi odio, pretium eget sem ac, ultricies eleifend elit. Maecenas vulputate efficitur risus, a ornare lorem sollicitudin vitae. Pellentesque rhoncus posuere risus eu placerat. Maecenas luctus felis ac interdum sodales. Quisque nec est lectus."; + + // Створений Text об'єкт + Text text = new Text(inputText); + + // Пошук у першому реченні слів, яких немає в наступних реченнях + List result = findUniqueWords(text); + System.out.println("Unique words in the first sentence: " + result); + } + + public static List findUniqueWords(Text text) { + if (text.getSentences().isEmpty()) { + throw new IllegalArgumentException("Text must contain at least one sentence."); + } + + Sentence firstSentence = text.getSentences().get(0); + List wordsInFirstSentence = firstSentence.getStringWords(); + + Set uniqueWords = new HashSet<>(wordsInFirstSentence); + + for (int i = 1; i < text.getSentences().size(); i++) { + Sentence sentence = text.getSentences().get(i); + List wordsInCurrentSentence = sentence.getStringWords(); + + wordsInCurrentSentence.forEach(uniqueWords::remove); + } + + return new ArrayList<>(uniqueWords); + } + +} diff --git a/lab5/src/lab5/Punctuation.java b/lab5/src/lab5/Punctuation.java new file mode 100644 index 0000000..037612a --- /dev/null +++ b/lab5/src/lab5/Punctuation.java @@ -0,0 +1,5 @@ +package lab5; + +public record Punctuation(char value) { + +} diff --git a/lab5/src/lab5/Sentence.java b/lab5/src/lab5/Sentence.java new file mode 100644 index 0000000..b1638e4 --- /dev/null +++ b/lab5/src/lab5/Sentence.java @@ -0,0 +1,62 @@ +package lab5; + +import java.util.ArrayList; +import java.util.List; + +public class Sentence { + private final List wordsAndPunctuations; + + public Sentence(String stringSentence) { + List wordsAndPunctuations = new ArrayList<>(); + StringBuilder word = new StringBuilder(); + + for (int i = 0; i < stringSentence.length(); i++) { + char ch = stringSentence.charAt(i); + + if (Character.isLetterOrDigit(ch)) { + word.append(ch); + } else if (Character.isWhitespace(ch)) { + if (word.length() > 0) { + wordsAndPunctuations.add(new Word(word.toString())); + word.setLength(0); + } + } else if (String.valueOf(ch).matches(".*\\p{Punct}.*")) { + if (word.length() > 0) { + wordsAndPunctuations.add(new Word(word.toString())); + word.setLength(0); + } + wordsAndPunctuations.add(new Punctuation(ch)); + } + } + + if (word.length() > 0) { + wordsAndPunctuations.add(new Word(word.toString())); + } + + this.wordsAndPunctuations = wordsAndPunctuations; + } + + public List getWordsAndPunctuations() { + return wordsAndPunctuations; + } + + + public List extractWords() { + List words = new ArrayList<>(); + for (Object element : getWordsAndPunctuations()) { + if (element instanceof Word) { + words.add((Word) element); + } + } + return words; + } + + public List getStringWords() { + List wordStrings = new ArrayList<>(); + for (Word word : extractWords()) { + wordStrings.add(word.getStringWord()); + } + return wordStrings; + } + +} diff --git a/lab5/src/lab5/Text.java b/lab5/src/lab5/Text.java new file mode 100644 index 0000000..f765e7c --- /dev/null +++ b/lab5/src/lab5/Text.java @@ -0,0 +1,21 @@ +package lab5; + +import java.util.ArrayList; +import java.util.List; + +public class Text { + private final List sentences; + + public Text(String stringText) { + this.sentences = new ArrayList<>(); + String[] splitText = stringText.split("(?<=[!.?])\\s*"); + for (String sentence : splitText) { + getSentences().add(new Sentence(sentence)); + } + } + + public List getSentences() { + return sentences; + } + +} diff --git a/lab5/src/lab5/Word.java b/lab5/src/lab5/Word.java new file mode 100644 index 0000000..7f3638e --- /dev/null +++ b/lab5/src/lab5/Word.java @@ -0,0 +1,28 @@ +package lab5; + +import java.util.ArrayList; +import java.util.List; + +public class Word { + private final String stringWord; + private final List letters; + + public Word(String stringWord) { + List letters = new ArrayList<>(); + char[] chars = stringWord.toCharArray(); + for (char c : chars) { + letters.add(new Letter(c)); + } + this.letters = letters; + this.stringWord = stringWord; + } + + public List getLetters() { + return letters; + } + + public String getStringWord() { + return stringWord; + } + +}