/* * %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 . */ import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Class that represents a sentence containing words. * * @since 0.2 * @author Dymik739 */ public class Sentence { /** contains Word objects of this sentence */ private Word[] words; /** contains a single punctuation mark that this sentence ends with */ private Symbol ending; /** * Constructor which automates the creation of a Sentence with just * a single string. * * @since 0.2 * @param sentence string containing a sentence */ public Sentence(String sentence) { int totalSentenceLength = sentence.length(); this.ending = new Symbol(sentence.charAt(totalSentenceLength-1)); String[] rawWords = sentence.substring(0, totalSentenceLength-1) .split(" "); Word[] preparedWords = new Word[rawWords.length]; for (int i = 0; i < rawWords.length; i++) { preparedWords[i] = new Word(rawWords[i]); } this.words = preparedWords; } /** * Getter for picking a Word by it's array index. * * @since 0.2 * @param index index of the Word * @return selected Word */ public Word getWordByIndex(int index) { return words[index]; } /** * Getter for the word array length. * * @since 0.2 * @return Word array length */ public int getLength() { return words.length; } /** * Method which allows to get index of a specific Word. * * @since 0.2 * @param w input word * @return index of the Word (if found) or -1 (if not found) */ public int index(Word w) { int wordIndex = -1; for (int i = 0; i < this.words.length; i++) { if (this.words[i].equals(w)) { wordIndex = i; break; } } return wordIndex; } /** * Wrapper for removeWord(int) method which looks up the Word first. * * @since 0.2 * @param w Word to delete */ public void removeWord(Word w) { int deleteIndex = index(w); removeWord(deleteIndex); } /** * Method which allows to remove a Word from the sentence by it's index. * * @since 0.2 * @param deleteIndex index of the word to delete */ public void removeWord(int deleteIndex) { Word[] updatedArray = new Word[this.words.length-1]; for (int i = 0; i < deleteIndex; i++) { updatedArray[i] = this.words[i]; } for (int i = 0; i < this.words.length - deleteIndex - 1; i++) { updatedArray[deleteIndex+i] = this.words[deleteIndex+i+1]; } this.words = updatedArray; } /** * Method which returns a String with simple listout of the words. * * @return String with word list */ public String listWords() { String result = ""; for (Word w : words) { result += w.toStringClean() + ", "; } return result.substring(0, result.length()-2); } /** * Method which picks the unique word found in the words array. * * @return result string */ public String getUniqueWord() { if (words.length == 1) { return "Unique word is " + words[0].toString(); } else if (words.length < 1) { return "No words are in this sentence"; } else { return "There are too many words to choose from (" + listWords() + ")."; } } /** * Method used to get a proper String representation of this sentence. * * @since 0.2 * @return String representation of this sentence */ @Override public String toString() { String result = ""; for (Word i : this.words) { result += i.toString() + " "; } return result.substring(0, result.length()-1) + this.ending.toString(); } }