upload lab5
This commit is contained in:
parent
a39494a9c8
commit
e1258c2542
|
@ -0,0 +1,12 @@
|
|||
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Lab5 {
|
||||
public static void main(String[] args) {
|
||||
Text text = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Mauris a diam maecenas sed enim ut sem viverra. Amet est placerat in egestas erat imperdiet. Sed turpis tincidunt id aliquet risus. Amet porttitor eget dolor morbi non arcu risus quis. Elementum nibh tellus molestie nunc non blandit massa. Feugiat scelerisque varius morbi enim nunc faucibus. Ipsum faucibus vitae aliquet nec ullamcorper sit amet risus nullam. Quis enim lobortis scelerisque fermentum dui faucibus in. Sem viverra aliquet eget sit amet tellus cras adipiscing enim. Sed ullamcorper morbi tincidunt ornare. Sodales ut eu sem integer vitae justo eget magna. Mi ipsum faucibus vitae aliquet nec ullamcorper.");
|
||||
System.out.println(text);
|
||||
text.getSortedWordsByFirstLetter();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
|
||||
|
||||
public class Letter {
|
||||
private final char character;
|
||||
|
||||
public Letter(char character) {
|
||||
this.character = character;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(character);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
|
||||
|
||||
public class Punctuation implements SentenceElement {
|
||||
private final String character;
|
||||
|
||||
public Punctuation(String character) {
|
||||
this.character = character;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return character;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class Sentence {
|
||||
private final SentenceElement[] sentenceElements;
|
||||
private static final String PUNCTUATION = "\\p{Punct}";
|
||||
|
||||
public Sentence(String sentences) {
|
||||
String[] sentenceElement = sentences.split("(?=" + PUNCTUATION + ")| ");
|
||||
sentenceElements = new SentenceElement[sentenceElement.length];
|
||||
for (int i = 0; i < sentenceElement.length; i++) {
|
||||
if (sentenceElement[i].matches(PUNCTUATION)) {
|
||||
sentenceElements[i] = new Punctuation(sentenceElement[i]);
|
||||
} else {
|
||||
sentenceElements[i] = new Word(sentenceElement[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void getSortedWordsByFirstLetter(Sentence[] sentences) {
|
||||
StringBuilder allWords = new StringBuilder();
|
||||
for (Sentence sentence : sentences) {
|
||||
allWords.append(sentence).append(" ");
|
||||
}
|
||||
|
||||
String[] words = allWords.toString().split("\\W+"); // Remove punctuation marks
|
||||
|
||||
HashSet<String> uniqueWords = new HashSet<>();
|
||||
|
||||
for (String word : words) {
|
||||
if (!word.isEmpty()) { // Ignore empty words
|
||||
uniqueWords.add(word.toLowerCase()); // Convert word to lowercase and add to HashSet
|
||||
}
|
||||
}
|
||||
|
||||
String[] uniqueSortedWords = uniqueWords.toArray(new String[0]);
|
||||
|
||||
Arrays.sort(uniqueSortedWords); // Sort unique words by the first letter
|
||||
|
||||
for (String word : uniqueSortedWords) {
|
||||
System.out.println(word);
|
||||
}
|
||||
}
|
||||
|
||||
// public static String[] getSortedWordsByFirstLetter() {
|
||||
//
|
||||
// return Sentence.getSortedWordsByFirstLetter(sentences);
|
||||
// }
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (SentenceElement se : sentenceElements) {
|
||||
if (se.getClass().isAssignableFrom(Word.class)) {
|
||||
out.append(" ");
|
||||
}
|
||||
out.append(se);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
|
||||
|
||||
public interface SentenceElement {
|
||||
// Об'єднуєм два типа
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
|
||||
|
||||
public class Text {
|
||||
private final Sentence[] sentences;
|
||||
|
||||
public Text(String text) {
|
||||
String[] splitText = text.split("(?=[.!?]+)| ");
|
||||
sentences = new Sentence[splitText.length];
|
||||
for (int i = 0; i < splitText.length; i++) {
|
||||
sentences[i] = new Sentence(splitText[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void getSortedWordsByFirstLetter() {
|
||||
Sentence.getSortedWordsByFirstLetter(sentences);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (Sentence sentence : sentences) {
|
||||
out.append(sentence.toString());
|
||||
}
|
||||
return out.toString().strip();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
|
||||
|
||||
public class Word implements SentenceElement {
|
||||
private final Letter[] letters;
|
||||
|
||||
public Word(String word) {
|
||||
letters = new Letter[word.length()];
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
letters[i] = new Letter(word.charAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (Letter let : letters) {
|
||||
out.append(let.toString());
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue