Created
December 4, 2023 00:56
-
-
Save ojulianos/17c92a38cd5b3ce52a45332db1e97bfb to your computer and use it in GitHub Desktop.
Adicionada validação com a https://api.dicionario-aberto.net/word/palavra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.Scanner; | |
import org.json.JSONArray; | |
public class TrabProg { | |
public static boolean isInDictionary(String word) { | |
try { | |
// URL da API do Dicionário Aberto | |
String apiUrl = "https://api.dicionario-aberto.net/word/" + word; | |
// Criação de uma conexão HTTP | |
URL url = new URL(apiUrl); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
// Verificação do código de resposta HTTP | |
int responseCode = conn.getResponseCode(); | |
if (responseCode != HttpURLConnection.HTTP_OK) { | |
System.out.println("Erro na conexão à API. Código de resposta: " + responseCode); | |
return false; | |
} | |
// Leitura da resposta da API | |
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
StringBuilder response = new StringBuilder(); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
response.append(line); | |
} | |
reader.close(); | |
// Analisar a resposta JSON da API | |
JSONArray jsonResponse = new JSONArray(response.toString()); | |
// Verificar se a palavra está presente na resposta JSON | |
for (int i = 0; i < jsonResponse.length(); i++) { | |
String wordResponse = jsonResponse.getJSONObject(i).optString("word"); | |
if (word.equalsIgnoreCase(wordResponse)) { | |
return true; | |
} | |
} | |
return false; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
public static boolean endsWithOx(String word) { | |
// Verificação se a palavra é oxítona usando a API do Dicionário Aberto | |
return isInDictionary(word); | |
} | |
public static boolean hasDit(String[] syllables) { | |
// Método para verificar se a sílaba tem ditongo | |
char[] vowels = {'a', 'e', 'i', 'o', 'u'}; | |
String lastSyllable = syllables[syllables.length - 1]; | |
int secondLastCharIndex = lastSyllable.length() - 2; | |
for (char vowel : vowels) { | |
if (lastSyllable.charAt(secondLastCharIndex) == vowel) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public static void checkOxOrParox(String word, int tonic) { | |
String[] syllables = word.split("-"); | |
boolean isOx = endsWithOx(word); | |
switch (tonic - 1) { | |
case 0: | |
System.out.println("A palavra é oxítona."); | |
if (isOx) { | |
System.out.println("Tem acento."); | |
} else { | |
System.out.println("Não tem acento."); | |
} | |
System.out.println("Todas as oxítonas terminadas em A(s), E(s), O(s), EM(ns) são acentuadas."); | |
System.out.println("Oxítonas não terminadas com esses prefixos não são acentuadas."); | |
break; | |
case 1: | |
System.out.println("A palavra é paroxítona."); | |
if (isOx) { | |
if (hasDit(syllables)) { | |
System.out.println("Tem acento."); | |
System.out.println("Todas as paroxítonas que possuem ditongos são acentuadas."); | |
} else { | |
System.out.println("Não tem acento."); | |
System.out.println("Exceção: O acento não ocorre quando o I ou U tônico formarem sílaba com as letras L, M, N, R ou z (EX: ruim) ou forem seguidos de nh (EX: rainha)."); | |
} | |
} else { | |
System.out.println("Tem acento."); | |
System.out.println("Todas as paroxítonas não terminadas em A(s), E(s), O(s), EM(ns) são acentuadas."); | |
} | |
break; | |
default: | |
System.out.println("Palavra sem acento."); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner kBoard = new Scanner(System.in); | |
String keepGoing = "sim"; | |
while (keepGoing.equals("sim")) { | |
System.out.println("Digite uma palavra separada em sílabas: "); | |
String word = kBoard.next().toLowerCase(); | |
// Verifica se a palavra está no dicionário antes de prosseguir | |
if (!isInDictionary(word)) { | |
System.out.println("A palavra não está no dicionário. Tente outra."); | |
continue; // Volta ao início do loop | |
} | |
System.out.println("Numere a sílaba tônica da direita para a esquerda. Exemplo: Ca-fe tônica: Fe=1"); | |
int tonic = kBoard.nextInt(); | |
checkOxOrParox(word, tonic); | |
System.out.print("Digite 'sim' para utilizar novamente: "); | |
keepGoing = kBoard.next().toLowerCase(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment