001/* 002 * SPDX-License-Identifier: Apache-2.0 003 * 004 * Copyright 2025-2026 The Enola <https://enola.dev> Authors 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package dev.enola.common.text; 019 020import static java.util.Objects.requireNonNull; 021 022import java.util.Locale; 023 024/** 025 * PronounceableText is a {@link LangString} plus a phonetic representation. 026 * 027 * <p>For example, the English word "hello" would be represented as "hello" and "həˈləʊ". 028 * 029 * <p>This allows it to be correctly pronounced by a text-to-speech synthesis engine, such as e.g. 030 * the (venerable!) <a href="https://espeak.sourceforge.net">eSpeak</a> (try it e.g. on <a 031 * href="https://itinerarium.github.io/phoneme-synthesis/">itinerarium's phoneme-synthesis</a>), or 032 * <a href="https://cloud.google.com/text-to-speech">Google Cloud Text-to-Speech AI</a> or <a 033 * href="https://aws.amazon.com/polly/">Amazon Polly</a> (try it e.g. on <a 034 * href="https://ipa-reader.com">ipa-reader.com</a>), etc. 035 * 036 * <p>Both the idea and the specific class name <code>PronounceableText</code> were directly 037 * inspired by the <a 038 * href="https://schema.org/PronounceableText">https://schema.org/PronounceableText</a>. 039 */ 040public final class PronounceableText extends LangString { 041 042 private final String phonetic; 043 private final SpeechMarkupForm phoneticForm; 044 045 private PronounceableText( 046 String text, Locale language, String phonetic, SpeechMarkupForm phoneticForm) { 047 super(text, language); 048 this.phoneticForm = requireNonNull(phoneticForm, "phoneticForm"); 049 this.phonetic = requireNonNull(phonetic, "phonetic"); 050 } 051 052 public static PronounceableText of( 053 String text, Locale language, String phonetic, SpeechMarkupForm phoneticForm) { 054 return new PronounceableText(text, language, phonetic, phoneticForm); 055 } 056 057 public String phonetic() { 058 return phonetic; 059 } 060 061 public SpeechMarkupForm phoneticForm() { 062 return phoneticForm; 063 } 064 065 @Override 066 public String toString() { 067 return super.toString() + "/" + phoneticForm + ":" + phonetic; 068 } 069 070 @Override 071 public int hashCode() { 072 int result = super.hashCode(); 073 result = 31 * result + phonetic.hashCode(); 074 result = 31 * result + phoneticForm.hashCode(); 075 return result; 076 } 077 078 @Override 079 public boolean equals(Object o) { 080 if (o == null || getClass() != o.getClass()) return false; 081 if (!super.equals(o)) return false; 082 083 PronounceableText that = (PronounceableText) o; 084 return phonetic.equals(that.phonetic) && phoneticForm == that.phoneticForm; 085 } 086}