我的代码:
var_dump($POS);
给了我:
array (size=9)
0 => string 'technical' (length=9)
1 => string 'recruiter' (length=9)
2 => string 'experience' (length=10)
3 => string 'work' (length=4)
4 => string 'department' (length=10)
5 => string 'modern' (length=6)
6 => string 'age' (length=3)
7 => string 'profile' (length=7)
8 => string 'prakhar' (length=7)
但是在迭代数组$POS
foreach($POS as $p)
{
echo "POS :".$p."<br/>";
}
它只回显第一个值,即technical
有什么问题?
实际代码:
function getTag($post)
{
$tagger = new PosTagger('lexicon.txt');
$temp = $tagger->tag($post);
$POS = getPOS($temp);
//var_dump($POS);
//$POS = implode(", ",$POS);
$con = mysqli_connect('127.0.0.1', 'root', '', 'mysql');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$selectTag = mysqli_query($con,"SELECT distinct tag from koove_tags");
$i = 0;
$totalPOSs = '';
$POSs = '';
$distinctPOSs = '';
//$prob[$i] = new stdClass();
//calculate distinct POS in all POSs for all tags
$selectPOS3 = mysqli_query($con,"SELECT pos from koove_posts");
while ($row4 = @mysqli_fetch_array($selectPOS3))
{
$distinctPOSs.=$row4['pos'].",";
}
$distinct_pos_Count = count(array_unique(str_word_count($distinctPOSs, 1)));
//var_dump($distinctPOSs);
//echo "Distinv pos:".$distinct_pos_Count.'<br/> ';
var_dump($POS);
foreach($POS as $p)
{
echo "POS :".$p."<br/>";
while ($row1 = @mysqli_fetch_array($selectTag))
{
//Calculate total pos for given tag
//echo "Tag : ".$row1['tag']."<br/>";
$selectPOS1 = mysqli_query($con,"SELECT * from koove_posts where tag = '".$row1['tag']."'");
$totalPOSs = '';
while ($row2 = @mysqli_fetch_array($selectPOS1))
{
//echo $row2['pos']."<br/>";
$totalPOSs.=$row2['pos'].",";
}
$totalPOS_count = str_word_count($totalPOSs);
//echo "Totla POS fo tag ".$row1['tag']." is ".$totalPOS_count."<br/>";
//calculate how many times particular 'pos' appears for given tag
//echo $totalPOSs."<br/>";
echo "pos : ".$p."<br/>";
$pos_Count = substr_count($totalPOSs, $p);
//echo "POS count for POS ".$p." and tag ".$row1['tag']." is ".$pos_Count."<br/>";
//Calculate the probability for each part of speach
$prob[] = (object) array(
"value" => ($pos_Count + 1)/ ($totalPOS_count + $distinct_pos_Count),
"tag" => $row1['tag'],
);
}
}
}
更新
$POS
的循环很好,但问题是
while ($row1 = @mysqli_fetch_array($selectTag))
{
}
因为我注意到了,对于while
循环上方的第一部分循环执行正确,但对于下一个$POS
它没有。
我是否需要刷新/清除$row1
?
答案 0 :(得分:0)
此代码工作正常。
尝试在循环中对所有内容进行注释(只留下回声),并且您将拥有数组中的所有值。
您还应该在代码的开头添加:
error_reporting(E_ALL);
ini_set('display_errors','1');
(仅在测试时)并移除@
前面的mysqli_fetch_array
以显示所有错误