/* * %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 representing a single word in the sentence. * * @author Dymik739 * @since 0.2 */ public class Word { /** contains the symbols that the word is made up of */ private Symbol[] symbols; /** 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) { Symbol p; // punctuation mark Symbol[] newWordArray; // symbols that make up the word Matcher punctuationMatcher = Pattern.compile("[,;:]$").matcher(rawWord); String foundPunctuation; Matcher wordMatcher = Pattern.compile("^[A-Za-z\\-']*").matcher(rawWord); String foundWord; if (punctuationMatcher.find()) { p = new Symbol(rawWord.charAt(rawWord.length()-1)); } else { p = new Symbol(' '); } if (wordMatcher.find()) { foundWord = wordMatcher.group(); } else { foundWord = ""; } 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; } /** * 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++) { if (!this.symbols[i].equals(o.symbols[i])) { return false; } } return true; } /** * 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; } /** * 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; } }