Word2vec in php - php

I am fairly new to ML and have no idea how things work, i have a webpage with search bar. and i need to use word2vec to complete user input in search bar.
i am using this code from https://github.com/donlinglok/C-word2vec-php
<?php
$txtfilepath = fopen("options.txt", "r") or die("Unable to open file!");
echo fread($txtfilepath, filesize("options.txt"));
//fclose($txtfilepath);
function train($txtfilepath) {
$cbow = 1;
$size = 500;
$window = 10;
$negative = 10;
$hs = 0;
$sample = "1e-5";
$threads = 40;
$binary = 1;
$iter = 3;
$min_count = 10;
exec ( "cd " . dirname ( __FILE__ ) . "/google-word2vec-trunk && ./word2vec " . " -train " . $txtfilepath . " -output " . dirname ( __FILE__ ) . "/vectors.bin " . " -cbow " . $cbow . " -size " . $size . " -window " . $window . " -negative " . $negative . " -hs " . $hs . " -sample " . $sample . " -threads " . $threads . " -binary " . $binary . " -iter " . $iter . " -min-count " . $min_count, $outputArray );
}
function distance($keyword) {
exec ( "cd " . dirname ( __FILE__ ) . " && ./distancecli " . dirname ( __FILE__ ) . "/vectors.bin " . $keyword, $outputArray );
if (isset ( $outputArray[0] )) {
return $outputArray;
} else {
return distance ( $keyword );
}
}
//train ( dirname ( __FILE__ ) . "/google-word2vec-trunk/questions-words.txt" );
echo json_encode ( distance ( "facebook" ), true );
?>
what should i do next ? how to run this code and get result in search bar? i ran it from the web folder http://localhost/trainmodel/word2vec.php and recieved this error Fatal error:
Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\trainmodel\word2vec.php on line 23

You can overcome this problem by increasing the max_execution_time value in the php settings.

Related

How to add a new line in textarea element in php code?

I want to add a newline in a textarea. I tried with < p> tag but it's not working. Can you help me to insert a newline in a textarea? Please find the code above for a generic contact form. Where should I add tags for line breaks here?
public function cs_form_textarea_render($params = '') {
global $post, $pagenow;
extract($params);
if ( $pagenow == 'post.php' ) {
$cs_value = get_post_meta($post->ID, 'cs_' . $id, true);
} else {
$cs_value = $std;
}
if ( isset($cs_value) && $cs_value != '' ) {
$value = $cs_value;
} else {
$value = $std;
}
$cs_rand_id = time();
if ( isset($force_std) && $force_std == true ) {
$value = $std;
}
$html_id = ' id="cs_' . sanitize_html_class($id) . '"';
$html_name = ' name="cs_' . sanitize_html_class($id) . '"';
if ( isset($array) && $array == true ) {
$html_id = ' id="cs_' . sanitize_html_class($id) . $cs_rand_id . '"';
$html_name = ' name="cs_' . sanitize_html_class($id) . '_array[]"';
}
$cs_required = '';
if ( isset($required) && $required == 'yes' ) {
$cs_required = ' required="required"';
}
$cs_output = '<div class="' . $classes . '">';
$cs_output .= ' <textarea' . $cs_required . ' rows="5" cols="30"' . $html_id . $html_name . ' placeholder="' . $name . '">' . sanitize_text_field($value) . '</textarea>';
$cs_output .= $this->cs_form_description($description);
$cs_output .= '</div>';
if ( isset($return) && $return == true ) {
return force_balance_tags($cs_output);
} else {
echo force_balance_tags($cs_output);
}
}
Just add a "\n" char somewhere between your text area tags
$cs_output .= ' <textarea' . $cs_required . ' rows="5" cols="30"' . $html_id . $html_name . ' placeholder="' . $name . '">' . sanitize_text_field($value) . "\n" . "this text is on a new line". '</textarea>';
You will try to add "\n" before your end position of the tag. You need to concatenate Like ."\n".

How does the computer calculate or store Diffie-Hellman values?

So usually the Diffie-Hellman key is 2048 bits as I understand but my computer can barely calculate a 10 digit number. What are some common numbers in Diffie-Hellman?
Here is my code that's suuuper slow:
$gen = 77;
$mod = 517165;
$saltA = 1233217;
$saltB = 5173123;
$calculatedSecretKeyA = gmp_mod(gmp_pow($gen, $saltA), $mod);
$calculatedSecretKeyB = gmp_mod(gmp_pow($gen, $saltB), $mod);
$calcKeyA = gmp_mod(gmp_pow($calculatedSecretKeyB, $saltA), $mod);
echo $calculatedSecretKeyB . "^" . $saltA . "" . " mod " . $mod . " = " . $calcKeyA;
$calcKeyB = gmp_mod(gmp_pow($calculatedSecretKeyA, $saltB), $mod);
echo $calculatedSecretKeyA . "^" . $saltB . "" . " mod " . $mod . " = " . $calcKeyB;
Use gmp_powm
gmp_powm ( GMP|int|string $num , GMP|int|string $exponent , GMP|int|string $modulus ) : GMP
for the below lines.
$calculatedSecretKeyA = gmp_powm($gen, $saltA, $mod);
$calculatedSecretKeyB = gmp_powm($gen, $saltB, $mod);
$calcKeyA = gmp_powm($calculatedSecretKeyB, $saltA, $mod);
$calcKeyB = gmp_powm($calculatedSecretKeyA, $saltB, $mod);
It uses the modular form of square-and-multiply technique. The intermediate values will never exceed mod^2. Also, it has O(log n) complexity.

Working with large numbers in php is outputing 0

I am trying to do some Diffie-Hellman which works fine with smaller generator, mod and private keys. But when the numbers get to large it just outputs zero. I've looked at other posts with no real solution. Could it be configured in php.ini?
$gen = 877;
$mod = 80182923;
$saltA = 517;
$saltB = 1517;
echo "calculation of Ga og Gb<br>";
echo "<br>user A shareable value<br>";
$calculatedSecretKeyA = ($gen**$saltA) % $mod;
echo $gen . "^" . $saltA . " mod " . $mod . " = " . $calculatedSecretKeyA;
echo "<br>user B shareable value<br>";
$calculatedSecretKeyB = ($gen**$saltB) % $mod;
echo $gen . "^" . $saltB . " mod " . $mod . " = " . $calculatedSecretKeyB;
echo "<br>user As calc of secret key <br>";
$calcKeyA = ($calculatedSecretKeyB**$saltA) % $mod;
echo $calculatedSecretKeyB . "^" . $saltA . "" . " mod " . $mod . " = " . $calcKeyA;
echo "<br><br>user Bs calc of secret key <br>";
$calcKeyB = ($calculatedSecretKeyA**$saltB) % $mod;
echo $calculatedSecretKeyA . "^" . $saltB . "" . " mod " . $mod . " = " . $calcKeyB;
echo "<br><br>bruteforcer A calc of secret key <br>";
$calcKeyB = ($calculatedSecretKeyA**7) % $mod;
echo $calculatedSecretKeyA . "^" . 7 . "" . " mod " . $mod . " = " . $calcKeyB;
You can use GMP library for this (https://www.php.net/manual/en/ref.gmp.php):
echo "calculation of Ga og Gb<br>";
echo "<br>user A shareable value<br>";
$calculatedSecretKeyA = gmp_mod ( gmp_pow ($gen, $saltA), $mod);
echo $gen . "^" . $saltA . " mod " . $mod . " = " . $calculatedSecretKeyA;
Output:
calculation of Ga og Gbuser A shareable value877^517 mod 80182923 = 79127908

Listing all directories (on hard-disk) and its folders and files

Is there a way I could read the entire file-system on my PC? I was trying to list all the files in W: directory, but the code doesn't work.
<?php
$fso = new COM('Scripting.FileSystemObject');
$D = $fso->Drives;
$type = array("Unknown","Removable","Fixed","Network","CD-ROM","RAM Disk");
foreach($D as $d ){
$dO = $fso->GetDrive($d);
$s = "";
if($dO->DriveType == 3){
$n = $dO->Sharename;
}else if($dO->IsReady){
$n = $dO->VolumeName;
$s = file_size($dO->FreeSpace) . " free of: " . file_size($dO->TotalSize);
}else{
$n = "[Drive not ready]";
}
echo "Drive " . $dO->DriveLetter . ": - " . $type[$dO->DriveType] . " - " . $n . " - " . $s . "<br>";
$dir = $dO->DriveLetter;
if (is_dir($dir.':\\')){ // NEVER ENTERS THE IF BLOCK
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
}
function file_size($size)
{
$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes';
}
?>
In the above code is_dir call always fails. What could be the reason for this? What is the correct way to read the windows file system?
try this http://php.net/manual/en/class.recursivedirectoryiterator.php
<?php
$path = realpath('/etc');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
echo "$name\n";
}

Using arrays in PHP

I am working on my first program written in PHP. This is a basic question. I am trying to print a string stored in an array. I have two arrays. One array, $content, stores a number at $content[$i][15] (i am using arrays in arrays, and looping through it with a for loop). Lets say this number is 3. I now want to access a string in another array, called $type. This string is at position $type[3]. Logically, I think, $type[3] should print the same thing as $type[$content[$i][15]]. But, when I call $type[$content[$i][15]], it doesn't print anything, but when i print $type[3] it works fine. Is this type of call not allowed in PHP?
$type = array();
$typetemp = readfile("type.csv");
foreach ($typetemp as $sa){
$type[$sa[0]] = $sa[1];
}
$content = readfile("content.csv");
for($i=0; $i<sizeof($content); $i++){
if($content[$i][15] == 3){
if($content[$i][4] == 1){
$temp = $content[$i][15];
echo "id is " . $content[$i][0] . "title is " . $content[$i][1] . "introtext is " . $content[$i][2] . "restoftext is " . $content[$i][3] . "visible is " . $content[$i][4] . "category is " . $content[$i][5] . "creation_date is " . $content[$i][6] . "author is " . $content[$i][7] . "img is " . $content[$i][8] . "ordering is " . $content[$i][9] . "rank is " . $content[$i][10] . "vid is " . $content[$i][11] . "start_date is " . $content[$i][12] . "stop_date is " . $content[$i][13] . "event_date is " . $content[$i][14] . "type is " . $content[$i][15] . "thumb is " . $content[$i][16] . "type name is " . $type[$temp] . "<br/>";
}
}
}
function readfile($filename) {
$line_of_text = array();
$file_handle = fopen($filename, "r");
while (!feof($file_handle) ) {
array_push($line_of_text, fgetcsv($file_handle, 1024));
}
fclose($file_handle);
return $line_of_text;
}
You are missing a $ sign before content:
$type[$content[$i][15]]
This accesses the value in $type with index $content[$i][15]

Categories