2023-06-10 15:50:10 +03:00
|
|
|
/*
|
|
|
|
* %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/>.
|
|
|
|
*/
|
|
|
|
|
2023-06-08 15:31:50 +03:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2023-06-10 15:50:10 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that represents a sentence containing words.
|
|
|
|
*
|
|
|
|
* @since 0.2
|
|
|
|
* @author Dymik739
|
|
|
|
*/
|
2023-06-08 15:31:50 +03:00
|
|
|
public class Sentence {
|
2023-06-10 15:50:10 +03:00
|
|
|
/** contains Word objects of this sentence */
|
2023-06-08 15:31:50 +03:00
|
|
|
private Word[] words;
|
|
|
|
|
2023-06-10 15:50:10 +03:00
|
|
|
/** contains a single punctuation mark that this sentence ends with */
|
|
|
|
private Symbol ending;
|
2023-06-08 15:31:50 +03:00
|
|
|
|
2023-06-10 15:50:10 +03:00
|
|
|
/**
|
|
|
|
* Constructor which automates the creation of a Sentence with just
|
|
|
|
* a single string.
|
|
|
|
*
|
|
|
|
* @since 0.2
|
|
|
|
* @param sentence string containing a sentence
|
|
|
|
*/
|
2023-06-08 15:31:50 +03:00
|
|
|
public Sentence(String sentence) {
|
|
|
|
int totalSentenceLength = sentence.length();
|
|
|
|
this.ending = new Symbol(sentence.charAt(totalSentenceLength-1));
|
|
|
|
|
|
|
|
String[] rawWords = sentence.substring(0, totalSentenceLength-1)
|
2023-06-10 15:50:10 +03:00
|
|
|
.split(" ");
|
2023-06-08 15:31:50 +03:00
|
|
|
|
|
|
|
Word[] preparedWords = new Word[rawWords.length];
|
|
|
|
|
|
|
|
for (int i = 0; i < rawWords.length; i++) {
|
|
|
|
preparedWords[i] = new Word(rawWords[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.words = preparedWords;
|
|
|
|
}
|
|
|
|
|
2023-06-10 15:50:10 +03:00
|
|
|
/**
|
|
|
|
* 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];
|
2023-06-08 15:31:50 +03:00
|
|
|
}
|
|
|
|
|
2023-06-10 15:50:10 +03:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2023-06-08 15:31:50 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-10 15:50:10 +03:00
|
|
|
/**
|
|
|
|
* Wrapper for removeWord(int) method which looks up the Word first.
|
|
|
|
*
|
|
|
|
* @since 0.2
|
|
|
|
* @param w Word to delete
|
|
|
|
*/
|
2023-06-08 15:31:50 +03:00
|
|
|
public void removeWord(Word w) {
|
|
|
|
int deleteIndex = index(w);
|
|
|
|
removeWord(deleteIndex);
|
|
|
|
}
|
|
|
|
|
2023-06-10 15:50:10 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-06-08 15:31:50 +03:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2023-06-10 15:50:10 +03:00
|
|
|
for (int i = 0; i < this.words.length - deleteIndex - 1; i++) {
|
2023-06-08 15:31:50 +03:00
|
|
|
updatedArray[deleteIndex+i] = this.words[deleteIndex+i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.words = updatedArray;
|
|
|
|
}
|
|
|
|
|
2023-06-10 15:50:10 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2023-06-08 15:31:50 +03:00
|
|
|
public String toString() {
|
|
|
|
String result = "";
|
|
|
|
|
|
|
|
for (Word i : this.words) {
|
|
|
|
result += i.toString() + " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.substring(0, result.length()-1) + this.ending.toString();
|
|
|
|
}
|
|
|
|
}
|