Add files via upload
This commit is contained in:
parent
154398a013
commit
a81f01103f
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,5 @@
|
|||
package lab5;
|
||||
|
||||
public record Letter(char value) {
|
||||
|
||||
}
|
|
@ -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<String> result = findUniqueWords(text);
|
||||
System.out.println("Unique words in the first sentence: " + result);
|
||||
}
|
||||
|
||||
public static List<String> findUniqueWords(Text text) {
|
||||
if (text.getSentences().isEmpty()) {
|
||||
throw new IllegalArgumentException("Text must contain at least one sentence.");
|
||||
}
|
||||
|
||||
Sentence firstSentence = text.getSentences().get(0);
|
||||
List<String> wordsInFirstSentence = firstSentence.getStringWords();
|
||||
|
||||
Set<String> uniqueWords = new HashSet<>(wordsInFirstSentence);
|
||||
|
||||
for (int i = 1; i < text.getSentences().size(); i++) {
|
||||
Sentence sentence = text.getSentences().get(i);
|
||||
List<String> wordsInCurrentSentence = sentence.getStringWords();
|
||||
|
||||
wordsInCurrentSentence.forEach(uniqueWords::remove);
|
||||
}
|
||||
|
||||
return new ArrayList<>(uniqueWords);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package lab5;
|
||||
|
||||
public record Punctuation(char value) {
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package lab5;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Sentence {
|
||||
private final List<Object> wordsAndPunctuations;
|
||||
|
||||
public Sentence(String stringSentence) {
|
||||
List<Object> 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<Object> getWordsAndPunctuations() {
|
||||
return wordsAndPunctuations;
|
||||
}
|
||||
|
||||
|
||||
public List<Word> extractWords() {
|
||||
List<Word> words = new ArrayList<>();
|
||||
for (Object element : getWordsAndPunctuations()) {
|
||||
if (element instanceof Word) {
|
||||
words.add((Word) element);
|
||||
}
|
||||
}
|
||||
return words;
|
||||
}
|
||||
|
||||
public List<String> getStringWords() {
|
||||
List<String> wordStrings = new ArrayList<>();
|
||||
for (Word word : extractWords()) {
|
||||
wordStrings.add(word.getStringWord());
|
||||
}
|
||||
return wordStrings;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package lab5;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Text {
|
||||
private final List<Sentence> 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<Sentence> getSentences() {
|
||||
return sentences;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package lab5;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Word {
|
||||
private final String stringWord;
|
||||
private final List<Letter> letters;
|
||||
|
||||
public Word(String stringWord) {
|
||||
List<Letter> letters = new ArrayList<>();
|
||||
char[] chars = stringWord.toCharArray();
|
||||
for (char c : chars) {
|
||||
letters.add(new Letter(c));
|
||||
}
|
||||
this.letters = letters;
|
||||
this.stringWord = stringWord;
|
||||
}
|
||||
|
||||
public List<Letter> getLetters() {
|
||||
return letters;
|
||||
}
|
||||
|
||||
public String getStringWord() {
|
||||
return stringWord;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue