Created
August 30, 2016 00:29
-
-
Save joffilyfe/92fbd60e22b35ba7f2c77b9767f00fd8 to your computer and use it in GitHub Desktop.
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
<?php | |
function search(array $data) { | |
$query = "http://dicionario-aberto.net/search-json"; | |
/* | |
* Busca por termos (like, prefix, suffix) | |
*/ | |
$first = TRUE; | |
foreach ($data as $type => $term) { | |
if (isset($data['word']) && isset($data['type']) && $data['type'] == 'full') { | |
$query .= "/". $data['word']; | |
break; | |
} elseif (isset($data['word']) && isset($data['type']) && $data['type'] == 'like') { | |
$query .= '?like='. $data['word']; | |
break; | |
} | |
if ($first) | |
$query .= '?'.$type.'='.$term; | |
else | |
$query .= '&'.$type.'='.$term; | |
$first = FALSE; | |
} | |
/* | |
* Faz a query | |
*/ | |
$result = file_get_contents($query); | |
if (isset($result) && !$result) { | |
throw new Exception("Erro na consulta, não há resposta de resultado."); | |
} | |
return json_decode($result); | |
} | |
/* | |
* Recebendo o GET e montando a Query | |
*/ | |
if (isset($_GET) && !empty($_GET)) { | |
$query = $_GET; | |
try { | |
$resultado = search($query); | |
$searchType = $query['type']; | |
/* | |
* Montando resultado estruturado | |
*/ | |
if (isset($query['word']) && $query['type'] == 'full') { | |
if (isset($resultado->superEntry)) { | |
$resultado = $resultado->superEntry[0]->entry->sense; | |
} else { | |
$resultado = $resultado->entry->sense; | |
} | |
} elseif ($query['type'] == 'like') { | |
$resultado = $resultado->list; | |
} | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Buscando por termos no Dicionario-aberto</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<div class="container"> | |
<h1 class="page-header">Buscar no Dicionário Aberto</h1> | |
<form class="col-md-6"> | |
<div class="form-group"> | |
<label for="word">Palavra</label> | |
<input type="text" class="form-control" id="word" placeholder="Gato" name="word"> | |
</div> | |
<div class="form-group"> | |
<label for="type">Tipo</label> | |
<select name="type" id="type" class="form-control" > | |
<option value="full">Palavra inteira</option> | |
<option value="like">Aproximada</option> | |
</select> | |
</div> | |
<button type="submit" class="btn btn-default">Buscar</button> | |
</form> | |
<form class="col-md-6"> | |
<div class="form-group"> | |
<label for="prefix">Prefixo</label> | |
<input type="text" class="form-control" id="prefix" placeholder="c" name="prefix"> | |
</div> | |
<div class="form-group"> | |
<label for="suffix">Sufixo</label> | |
<input type="text" class="form-control" id="suffix" placeholder="er" name="suffix"> | |
</div> | |
<input type="hidden" name="type" value="like"> | |
<button type="submit" class="btn btn-default">Buscar</button> | |
</form> | |
</div> | |
<div class="container"> | |
<div class="col-md-12"> | |
<?php if (isset($resultado)): ?> | |
<h2>Resultado da consulta</h2> | |
<hr> | |
<?php endif; ?> | |
<!-- Resultado da pesquisa para palavras sem aproximação --> | |
<?php if (isset($resultado) && $resultado != null && $searchType == 'full'): ?> | |
<table class="table table-striped table-bordered"> | |
<thead> | |
<th>Detalhe</th> | |
<th>Definição</th> | |
</thead> | |
<tbody> | |
<?php foreach ($resultado as $key => $linha): ?> | |
<tr> | |
<?php foreach ($linha as $coluna): ?> | |
<?php if (!is_object($coluna) && $coluna == 1) continue ?> | |
<?php | |
if (is_object($coluna)) { | |
// echo $coluna->usg; | |
$coluna = (Array)$coluna; | |
} else { | |
} | |
?> | |
<?php if (is_array($coluna)): ?> | |
<td><?php print_r(($coluna['#text'])); ?></td> | |
<?php else: ?> | |
<td><?php echo $coluna; ?></td> | |
<?php endif; ?> | |
<?php endforeach; ?> | |
</tr> | |
<?php endforeach; ?> | |
</tbody> | |
</table> | |
<!-- Resultado das pesquisas para palavras aproximadas --> | |
<?php elseif (isset($resultado) && $resultado != null && $searchType == 'like'): ?> | |
<table class="table table-striped table-bordered"> | |
<thead> | |
<th>#</th> | |
<th>Palavras similares</th> | |
</thead> | |
<tbody> | |
<?php foreach($resultado as $key => $palavra): ?> | |
<tr> | |
<td> | |
<?php echo $key+1; ?> | |
</td> | |
<td> | |
<?php echo $palavra; ?> | |
</td> | |
</tr> | |
<?php endforeach; ?> | |
</tbody> | |
</table> | |
<?php endif; ?> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment