Count inverse document frequency (idf) in every documents - php

I wanna count idf, the formula is IDF=log(D/df) that D is total data and df is many data that contain the searched words.
from the tables :
1. tb_stemming
===========================================================================
|stem_id | stem_before | stem_after | stem_freq | sentence_id |document_id|
===========================================================================
| 1 | Data | Data | 1 | 0 | 1 |
| 2 | Discuss | Discuss | 1 | 1 | 1 |
| 3 | Mining | Min | 1 | 0 | 2 |
===========================================================================
here's the code :
countIDF($total_sentence,$doc_id);
that $total_sentence is
Array ( [0] => 644 [1] => 79 [2] => 264 [3] => 441 [4] => 502 [5] => 18 [6] => 352 [7] => 219 [8] => 219 )
function countIDF($total_sentence, $doc_id) {
foreach ($total_sentence as $doc_id => $total_sentences){
$idf = 0;
$query1 = mysql_query("SELECT document_id, DISTINCT(stem_after) AS unique_token FROM tb_stemming group by stem_after where document_id='$doc_id' ' ");
while ($row = mysql_fetch_array($query)) {
$token = $row['unique_token'];
$doc_id = $row['document_id'];
$ndw = countNDW($token);
$idf = log($total_sentences / $ndw)+1;
$q = mysql_query("INSERT INTO tb_idf VALUES ('','$doc_id','$token','$ndw','$idf') ");
}
}
}
and the function of countNDW is :
function countNDW ($word) {
$query = mysql_query("SELECT stem_after, COUNT( DISTINCT sentence_id ) AS ndw FROM `tb_stemming` WHERE stem_after = '$word' GROUP BY stem_after");
while ($row = mysql_fetch_array($query)) {
$ndw = $row['ndw'];
}
return $ndw;
}
It can't worked well, especially in call from database. All I need is to count in every document_id. How to define it in my code ? please, help me.. thank you so much :)

Related

Count array elements based on criterias PHP

Looking for help in counting elements in a php array meeting certain criteria and getting them to properly display in html table.
I have this array named $Array
Array
(
[0] => Array
(
[fMonth] => 12
[fSnowDepth] => 0.2
)
[1] => Array
(
[fMonth] => 12
[fSnowDepth] => 3.7
)
[2] => Array
(
[fMonth] => 12
[fSnowDepth] => 1
)
[3] => Array
(
[fMonth] => 01
[fSnowDepth] => 1
)
[4] => Array
(
[fMonth] => 01
[fSnowDepth] => 0.5
)
[5] => Array
(
[fMonth] => 01
[fSnowDepth] => 4.5
)
[6] => Array
(
[fMonth] => 01
[fSnowDepth] => 1.3
)
)
What I'm trying to do is count the months which meet conditions such as fSnowDepth >= 1 and < 3, fSnowDepth >= 3 and < 5, etc. and place in html table. I'm expecting this with (blank) for the months with no count:
| Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
SD >=1 and < 3 | 2 | | | | | | | | | | | 1
SD >=3 and < 5 | 1 | | | | | | | | | | | 1
... etc depths | | | | | | | | | | | |
The code I have is:
$array = $result['rawSumSnowDepth'];
foreach ($array as $key => $items) {
if ($items['fSnowDepth'] >= 1 && $items['fSnowDepth'] < 3) {
$value = $items['fMonth'];
$output[$value] = ($output[$value] ?? 0) + 1;
for ($x = 0; $x <= 11; $x++) {
if ($x + 1 == $items['fMonth']) {
$result['snDaysOverAmt'][$x] = array($output[$value]);
} elseif (empty($result['snDaysOverAmt'][$x])) {
$result['snDaysOverAmt'][$x] = array($output[$value] => " ");
}
}
}
}
if (isset($result['snDaysOverAmt'])) {
foreach ($result['snDaysOverAmt'] as $amounts => $amount) {
if ($amount) {
echo '<td>' . implode($amount) . '</td>';
}
}
}
This code works like a charm for the first row of snow depths >= 1 and < 3 but when I run the code again for the next row (>= 3 and < 5) and I get what appears to be a doubling of the first row.
Is there another way to do this so I can include different snow depth counts AND is there a more concise way of doing this? I'm still a PHP rookie so any help would be appreciated.
Thanks to #Barmar this was all that was needed:
$output = [];
$result['snDaysOverAmt'] = [];
these were placed after the last closing bracket in the code provided in the question above.

How to create PHP array from MySQL table

I have a MySQL table:
-------------------
|type_id|type_name|
-------------------
| 22 | Toyota |
| 22 | Mazda |
| 23 | Volvo |
| 23 | Man |
| 25 | Scania |
| 25 | Iveco |
| 25 | Fiat |
-------------------
which is created dynamically from user input. I want to create an array from the table using PHP like this:
array(
'22' => array('Toyota', 'Mazda'),
'23' => array('Volvo', 'Man'),
'25' => array('Scania', 'Iveco','Fiat'),
);
where array id will be type_id and elements will be type_name's of the type_id.I tried this:
$results = array();
while($line = mysqli_fetch_array($result, MYSQL_ASSOC))
{
$results[] = $line;
}
print_r($results);
But I am getting:
Array (
[0] => Array (
[type_id] => 22
[type_name] => Toyota
)
[1] => Array (
[type_name] => 22
[type_name] => Mazda
)
...so on
Please help!
You need to change your code a bit and it will work fine:-
$results = array();
while($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$results[$line['type_id']][] = $line['Type_name']; //check change here
}
print_r($results);
Note:- check your column-names and correct them if any mistake is there.
Please replace your db name and table name in the code-
$conn = mysqli_connect("localhost","root","","dbname");
$query ="select * from table";
$exe = mysqli_query($conn,$query);
$input = array();
$type = array();
while ($result = mysqli_fetch_array($exe))
{
if(array_key_exists($result['type_id'], $input))
{
$type[] = $result['type_name'];
}
else
{
$type = array($result['type_name']);
}
$input[$result['type_id']] = $type;
}
echo "<pre>";
print_r($input);
select type_id and Type_name columns from table and in foreach loop do this:
$output = array();
foreach ($result as $item){
$output[$item['type_id']][] = $item['Type_name'];
}

mysqli result in array

i have this table produced from the query SELECT * FROM myTable
+--------------------------+---------+--------+
| Name | Version | Detail |
+--------------------------+---------+--------+
| name0 | 10 | xxx |
| name1 | 30 | yyy |
| name2 | 30 | zzz |
| name3 | 30 | kkk |
+--------------------------+---------+--------+
so, i need to have this table in a php array using mysqli function.
//$this->internalDB have the db connection
$result = $this->internalDB->query($query)->fetch_array(MYSQLI_NUM);
print_r($result);
produced
Array ( [Name] => name0 [Version] => 10 [Detail] => xxx )
cycles with the speech does not change ...
how i can do that?
This will query a database and return your values as an associative array in the $arrReturn variable.
$mysqli = new mysqli("host", "user", "password", "database");
$query = "SELECT * FROM myTable";
$sqlRes = $mysqli->query($query);
if (is_object($sqlRes)) {
$arrReturn = array();
while ($row = $sqlRes->fetch_assoc()) {
$arrRow = array();
foreach($row as $field => $value) {
$arrRow[$field] = $value;
}
$arrReturn[] = $arrRow;
}
echo $arrReturn[0]['Name']; // prints 'name0'
}
It might be helpful to read through the mysqli quickstart guide

Merge rows in the same id into array

I have sentence table :
======================================================================
| id_row | id_document | id_sentence | sentence |
======================================================================
| 1 | 1 | 0 | Example sentences A |
| 2 | 1 | 1 | Example sentences B |
| 3 | 2 | 0 | Example sentences C |
| 4 | 2 | 1 | Example sentences D |
======================================================================
I wanna merge sentence that have same id_document into array like => Array( [1] => Array ( [0] => Example sentences A [1] => Example sentences B ) [2] => Array ( [0] => Example sentences C [1] => Example sentences D ))
Here's the code :
<?php
require_once 'conf.php';
$sql = mysql_query('SELECT sentence FROM sentence group by id_document') or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
$sentence[] = $row['sentence'];
}
print_r($sentence);
?>
but, I just get one sentence in every different id_document. Help me. Thank you :)
Index the array on id_document, and add new entries to the array like this:
while ($row = mysql_fetch_array($sql)) {
$sentence[ $row['id_document'] ][] = $row['sentence'];
}
You can write:
while ($row = mysql_fetch_array($sql))
{
$sentence[$row['id_document']][] = $row['sentence'];
}
while ($row = mysql_fetch_array($sql))
$sentence[$row['id_document']][] = $row['sentence'];
Here's my final code :
<?php
require_once 'conf.php';
$sql = mysql_query('SELECT sentence, id_document FROM sentence ') or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
$sentence[$row['id_document']][] = $row['sentence'];
}
print_r($sentence);
?>

php Multidimensional array question

So I have an array like the following:
Array
(
[0] => Array
(
[user_id] => 684
[sec_id] => 2
[rank_id] => 1
[rank] => usr
)
[1] => Array
(
[user_id] => 693
[sec_id] => 3
[rank_id] => 5
[rank] => usr
)
)
And I have another array like this
Array
(
[0] => 2
[1] => 7
[2] => 27
)
I want the value of the second array to be added at the end of each arrays of the 1st array, and it should be multiplied. I mean, if I have 100 arrays in the first array, and 3 elements in the second array, I should have 300 in the resulting array.
Taking example of the above, I would like to have something as follows:
user_id | sec_id | rank_id | rank | menu_id
684 | 2 | 1 | usr | 2
684 | 2 | 1 | usr | 7
684 | 2 | 1 | usr | 27
693 | 3 | 5 | usr | 2
693 | 3 | 5 | usr | 7
693 | 3 | 5 | usr | 27
I tried with the following function, but it's not working.
function getR($arr_one,$arr_two) {
foreach ($arr_one as $k=>&$v) {
foreach ($arr_two as $x=>&$y) { $v['menu_id'] = $y; }
}
return $arr_one;
}
This is just making an array like this:
user_id | sec_id | rank_id | rank | menu_id
684 | 2 | 1 | usr | 27
693 | 3 | 5 | usr | 27
Means, it's just adding menu_id at the end of each element of the first array, but not multiplying. Any idea, I'm surely missing something.
Thanks guys.
function getR($arr_one,$arr_two) {
$new_arr = array();
foreach ($arr_one as $k=>$v) {
foreach ($arr_two as $x=>$y) {
$this_item = $v;
$this_item['menu_id'] = $y;
$new_arr[] = $this_item;
}
}
return $new_arr;
}
I'm not going to ask... but try this:
<?php
function crazy ($arr1,$arr2) {
foreach ($arr1 as $key=>$value) {
foreach ($arr2 as $value2) {
$nvalue=$value;
$nvalue[]=$value2;
$new[]=$nvalue;
}
}
return $new;
}
$arr1=array(array('user'=>1,'dude'=>2),array('user'=>2,'dude'=>3));
$arr2=array(2,7,27);
print_r(crazy($arr1,$arr2));
this is tested too, http://www.ideone.com/Of126
Without testing (eek!) I imagine something like this:
function getR( $arr_one, $arr_two )
{
$second_key = 0;
foreach ( $arr_one as $k => &$v )
{
$v['menu_id'] = $second_key++;
if ( 3 == $second_key ) $second_key = 0;
}
return $arr;
}
Presumably, you're passing the first array by reference? Not sure what $arr is that you're returning though...

Categories