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

135 lines
3.5 KiB
Java
Raw Normal View History

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/>.
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2023-06-10 15:50:10 +03:00
/**
* Class representing a single word in the sentence.
*
* @author Dymik739
* @since 0.2
*/
public class Word {
2023-06-10 15:50:10 +03:00
/** contains the symbols that the word is made up of */
private Symbol[] symbols;
2023-06-10 15:50:10 +03:00
/** punctuation mark that follows the word */
private Symbol ending;
/**
* Constructor that allows to create a word by having just a string.
*
* @since 0.2
* @param rawWord string that contains a parseable word.
*/
public Word(String rawWord) {
2023-06-10 15:50:10 +03:00
Symbol p; // punctuation mark
Symbol[] newWordArray; // symbols that make up the word
2023-06-10 15:50:10 +03:00
Matcher punctuationMatcher = Pattern.compile("[,;:]$").matcher(rawWord);
String foundPunctuation;
2023-06-10 15:50:10 +03:00
Matcher wordMatcher = Pattern.compile("^[A-Za-z\\-']*").matcher(rawWord);
String foundWord;
if (punctuationMatcher.find()) {
2023-06-10 15:50:10 +03:00
p = new Symbol(rawWord.charAt(rawWord.length()-1));
} else {
p = new Symbol(' ');
}
2023-06-10 15:50:10 +03:00
if (wordMatcher.find()) {
foundWord = wordMatcher.group();
} else {
foundWord = "";
}
2023-06-10 15:50:10 +03:00
newWordArray = new Symbol[foundWord.length()];
for (int i = 0; i < foundWord.length(); i++) {
newWordArray[i] = new Symbol(foundWord.charAt(i));
}
this.symbols = newWordArray;
this.ending = p;
}
2023-06-10 15:50:10 +03:00
/**
* Method for comparing two words.
*
* @since 0.2
* @param o Word to compare to
* @return true if words are the same, false otherwise
*/
public boolean equals(Word o) {
if (o.symbols.length != this.symbols.length) {
return false;
}
for (int i = 0; i < this.symbols.length; i++) {
2023-06-10 15:50:10 +03:00
if (!this.symbols[i].equals(o.symbols[i])) {
return false;
}
}
return true;
}
2023-06-10 15:50:10 +03:00
/**
* Method used to get a proper String representation of this word.
*
* @since 0.2
* @return String representation of this word
*/
@Override
public String toString() {
String result = "";
for (Symbol i : this.symbols) {
result += i.toString();
}
if (!this.ending.equals(new Symbol(' '))) {
result += this.ending.toString();
}
return result;
}
2023-06-10 15:50:10 +03:00
/**
* Method used to get a clean String representation of just this word.
*
* @since 0.2
* @return String representation of this word (without punctuation)
*/
public String toStringClean() {
String result = "";
for (Symbol i : this.symbols) {
result += i.toString();
}
return result;
}
}