Skip to content

Commit

Permalink
Improvement on the filter-text tools
Browse files Browse the repository at this point in the history
  • Loading branch information
impallari committed Apr 22, 2015
1 parent 89ba260 commit 106890b
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 109 deletions.
88 changes: 68 additions & 20 deletions includes/playground/better-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

$made_from = $_POST['betterglyphs'];
$max_words = $_POST['bettermax'];
$bettersize = $_POST['bettersize'];
$text_size = 30;

# Limpio Lista de Glyphs
Expand All @@ -22,12 +23,7 @@ function mb_str_split( $string ) {
$made_from_as_array[$key] = 0;
}

// Adding Dictionary
include ("../dictionaries/english.php");
$text .= ' '.$english;
include ("../dictionaries/spanish.php");
$text .= ' '.$spanish;

# Funcion para buscar values que contengan una determinada string dentro de un array
function my_array_find($needle, $haystack)
{
foreach ($haystack as $item) {
Expand All @@ -38,12 +34,13 @@ function my_array_find($needle, $haystack)
}
}

# Classe que hace toda la magia
class textFilter
{
private $max_words = 300;
private $full_text = array();
public $madefrom_text = array();
private $madefrom_text = array();
private $resulting_text = array();
public $max_words = 300;
private $length;
private $occurences = array();

Expand Down Expand Up @@ -117,12 +114,46 @@ function analize_frequency()
return $this;
}

function get_results()
function get_words()
{
$this->add_initial_words_to_results();
for ($i = 5; $i < $this->max_words; $i++) {
$this->add_less_frequent_words_to_results();
}
return $this;
}

function uppercase()
{
foreach ($this->resulting_text as $key => $resulting_text) {
$this->resulting_text[$key] = strtoupper($this->resulting_text[$key]);
}
return $this;
}

function sentencecase()
{
foreach ($this->resulting_text as $key => $value) {
$this->resulting_text[$key] = ucwords($this->resulting_text[$key]);
}
return $this;
}

function somesentence()
{
$sentence_count = 1;
foreach ($this->resulting_text as $key => $value) {
if ($sentence_count % 6 == 1 ) {
$this->resulting_text[$key] = ucwords($this->resulting_text[$key]);
}
$sentence_count ++;
}
return $this;
}

function get_results()
{
shuffle($this->resulting_text);
return $this->resulting_text;
}

Expand All @@ -133,22 +164,39 @@ function get_occurences()

}

$a = new textFilter();
$a->add_dictionary($text)->made_from( $made_from_as_array )->set_max_words($max_words);
// Start Benchmark
$timer_start = microtime(true);

// Adding Dictionaries
$text ='';
if ( $_POST['better_eng_dict'] == "yes" ) {
include ("../dictionaries/english.php");
$text .= ' '.$english;
}
if ( $_POST['better_spa_dict'] == "yes" ) {
include ("../dictionaries/spanish.php");
$text .= ' '.$spanish;
}

$a = new textFilter();
$a->add_dictionary($text)->made_from( $made_from_as_array )->set_max_words($max_words)->get_words();
if ( $_POST['better_sentence'] == "yes" ) $a->sentencecase();
if ( $_POST['better_somesentence'] == "yes" ) $a->somesentence();
if ( $_POST['better_uppercase'] == "yes" ) $a->uppercase();
$resultados = $a->get_results();

echo '<p class="sizelabel">'.count($resultados). ' Results</p>';
echo '<p style="font-size: 30px;">'.implode(' ', $resultados).'</p>';
// End Benchmark
$timer_end = microtime(true) - $timer_start;

echo '<p class="sizelabel">'.count($resultados). ' Results ('.number_format($timer_end, 2).' seconds)</p>';
echo '<p style="font-size: '.$bettersize.'px;';
if ( isset( $_POST['betterline'] ) && !empty( $_POST['betterline'] ) ) echo ' line-height: '.$_POST['betterline'].';';
echo '">'.implode(' ', $resultados).'</p>';

echo '<hr>';
echo '<div class="dontprint">';
echo '<pre>';
print_r( $a->get_occurences() );
echo '</pre>';

//echo '<hr>';
//echo '<pre>';
//print_r( $a->madefrom_text );
//echo '</pre>';

echo '</div>';
;
?>
127 changes: 89 additions & 38 deletions includes/playground/better.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

<div id="formContainer">
<form method="post" action="includes/playground/better-ajax.php" id="betterform">

<?php
// Defino un tamaño por defecto
if ( !isset($_POST['betterglyphs']) && empty($_POST['betterglyphs']) ) $_POST['betterglyphs'] = 'abcdefghijklmnopqrstuvwxyz';
if ( !isset($_POST['bettersize']) && empty($_POST['bettersize']) ) $_POST['bettersize'] = 60;
?>

<table width="920" border="0">

<tr>
<td valign="top">

<td valign="top" width="420">
<p class="sizelabel">Made From:</p>
<textarea rows="2" style="width: 500px;" id="betterglyphs"><?php if (isset($_POST['betterglyphs']) && !empty($_POST['betterglyphs']) ) echo $_POST['betterglyphs']; ?></textarea>
<textarea rows="2" style="width: 400px;" id="betterglyphs"><?php if (isset($_POST['betterglyphs']) && !empty($_POST['betterglyphs']) ) echo $_POST['betterglyphs']; ?></textarea>
</td>
<td width="90" valign="top">

<td valign="top">
<p class="sizelabel">Max Results:</p>
<select id="bettermax">
<!-- <option value="">Unlimited</option> -->
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="75">75</option>
<option value="100">100</option>
Expand All @@ -27,51 +31,98 @@
<option value="400">400</option>
<option value="450">450</option>
<option value="500">500</option>
<option value="600">600</option>
<option value="700">700</option>
<option value="800">800</option>
<option value="900">900</option>
<option value="1000">1000</option>
</select>
</td>
</td>

<td valign="top">
<p class="sizelabel"><input type="checkbox" id="better_eng_dict" checked="checked"> Eng<br />
<input type="checkbox" id="better_spa_dict" checked="checked"> Spa</p>
</td>

<td valign="top">
<p class="sizelabel">Size:</p>
<input type="text" id="bettersize" size="3" value="<?php if (isset($_POST['bettersize']) && !empty($_POST['bettersize']) ) echo $_POST['bettersize']; ?>" />
</td>

<td valign="top">
<p class="sizelabel">Line-height:</p>
<select id="betterline">
<option value="">Default</option>
<option value="1">1</option>
<option value="1.1">1.1</option>
<option value="1.2">1.2</option>
<option value="1.3">1.3</option>
<option value="1.4">1.4</option>
<option value="1.5">1.5</option>
<option value="1.6">1.6</option>
<option value="1.7">1.7</option>
<option value="1.8">1.8</option>
</select>
</td>

<td valign="top">
<p class="sizelabel"><input type="checkbox" id="better_addsomesentence"> Some<br />
<input type="checkbox" id="better_addsentence"> Initial<br />
<input type="checkbox" id="better_adduppercase"> AllCaps</p>
</td>

<td align="left" valign="middle">
<button type="submit">Better Adhesion!</button>
</td>
<button type="submit">Find Words</button>
</td>

</tr>
</table>

</form>
</div>
</div>

<div class="better_loading" style="padding: 20px 80px 20px 0px;display: none;"><p class="sizelabel">Searching... go for a coffee, this can take a long time.</p></div>

<!-- ajax Results -->
<div id="betterresults" contenteditable="true" style="padding: 20px 80px 20px 0px;">
<p>Better Adhesion!</p>
<div class="better_results" id="betterresults" contenteditable="true" style="padding: 20px 80px 20px 0px;">
<p>Improved Adhesion text having a high-frequency of low-frequency letters.</p>
</div>

</div>

<!-- Javascript for the Filter Tool -->
<script>
$("#betterform").submit(function(event) {

event.preventDefault();

$.ajax({
url: "includes/playground/better-ajax.php",
data: {
betterglyphs: $( "#betterglyphs" ).val(),
bettermax: $( "#bettermax" ).val(),
},
type: "POST",
dataType : "html",
success: function( msg ) {
$( "#betterresults" ).html( msg );
},
error: function( xhr, status ) {
alert( "Sorry. Try Again, there was a problem!" );
},
});

event.preventDefault();
var better_eng_dict = $("#better_eng_dict").is(':checked') ? "yes" : "no";
var better_spa_dict = $("#better_spa_dict").is(':checked') ? "yes" : "no";
var better_adduppercase = $("#better_adduppercase").is(':checked') ? "yes" : "no";
var better_addsentence = $("#better_addsentence").is(':checked') ? "yes" : "no";
var better_addsomesentence = $("#better_addsomesentence").is(':checked') ? "yes" : "no";
$.ajax({
url: "includes/playground/better-ajax.php",
data: {
betterglyphs: $( "#betterglyphs" ).val(),
bettermax: $( "#bettermax" ).val(),
bettersize: $( "#bettersize" ).val(),
betterline: $( "#betterline" ).val(),
better_uppercase: better_adduppercase,
better_sentence: better_addsentence,
better_somesentence: better_addsomesentence,
better_eng_dict: better_eng_dict,
better_spa_dict: better_spa_dict,
},
type: "POST",
dataType : "html",
success: function( msg ) {
$( "#betterresults" ).html( msg );
},
error: function( xhr, status ) {
alert( "Try Again, there was a problem!" );
},
beforeSend:function(){
$(".better_loading").show();
$(".better_results").hide();
},
complete:function(){
$(".better_loading").hide();
$(".better_results").show();
},
});
});
</script>
<!-- End Javascript for the Filter Tool -->
Loading

0 comments on commit 106890b

Please sign in to comment.