oop-labs-collection/labs/5/Text.java

97 lines
2.6 KiB
Java

/*
* %W% %E% Dymik739
* Email: dymik739@109.86.70.81
*
* Copyright (C) 2023 FIOT Dev Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Class representing the text being processed.
*
* @author Dymik739
* @since 0.2
*/
public class Text {
/** contains all the Sentence objects combining into the original text */
private Sentence[] sentences;
/**
* Constructor which allows to create sentence from a string
*
* @since 0.2
* @param input string to be parsed as sentence
*/
public Text(String input) {
String[] rawSentences = input.split("(?<=[.?!])( |$)");
Sentence[] newSentences = new Sentence[rawSentences.length];
for (int i = 0; i < rawSentences.length; i++) {
newSentences[i] = new Sentence(rawSentences[i]);
}
this.sentences = newSentences;
}
/**
* Method that picks a sentence by it's array index
*
* @since 0.2
* @param index sentence id
* @return selected Sentence object
*/
public Sentence getSentenceByIndex(int index) {
return sentences[index];
}
/**
* Method for cleaning first sentence from words that are present
* in other sentences.
*
* @since 0.2
*/
public void cleanFirstSentence() {
Word processedWord;
for (int i = 1; i < sentences.length; i++) {
for (int j = 0; j < sentences[i].getLength(); j++) {
processedWord = sentences[i].getWordByIndex(j);
while (sentences[0].index(processedWord) != -1) {
sentences[0].removeWord(processedWord);
}
}
}
}
/**
* Method used to get a proper String representation of the text.
*
* @since 0.2
* @return String representation of the text
*/
@Override
public String toString() {
String result = "";
for (Sentence s : this.sentences) {
result += s.toString() + " ";
}
return result;
}
}