4

だから私はフレーズと個々の単語の 2-D 配列を持っています$frags.

array(4) { 
[0]=> array(5) { 
    [0]=> string(3) "the" [1]=> string(4) "term" [2]=> string(4) "'AI'" [3]=> string(5) "still" [4]=> string(7) "manages" } 
[1]=> array(2) { 
    [0]=> string(2) "to" [1]=> string(5) "creep" } 
[2]=> array(3) { 
    [0]=> string(4) "into" [1]=> string(2) "my" [2]=> string(6) "speech" } 
[3]=> array(2) { 
    [0]=> string(2) "or" [1]=> string(8) "writing." } 
}  

私がvar_dump($frags[0])それを与えるとき:

array(5) { 
    [0]=> string(3) "the" [1]=> string(4) "term" [2]=> string(4) "'AI'" [3]=> string(5) "still" [4]=> string(7) "manages" } 

しかし、印刷しようとすると、次のように$frags[0][0]なります。

Notice: C:\wamp\www\jpsmythe\parser.php の 57 行目の未定義のオフセット: 0

Notice: C:\wamp\www\jpsmythe\parser.php の 57 行目の未定義のオフセット: 0

文字列(3)「その」

これは意味がありません。0 を文字列のように扱うなど、すべてを試したような気がしますが、まだ表示されません。ヘルプ?

関連するコード:

private function clause($frags) {
   // set the parser through the control hub
    $controller = Controller::getInstance();
    $parser = $controller->parser;
    $parser->init();

   // take the $frags array from elsewhere in the program
   // this $frags is actually punctuation-separated fragments of a sentence, not the  same as the $frags array with which I'm having problems now   
    $i = 0;
    $clauses = array();
   //run through the punctuated $frags array
    while ($i < count($frags)) {
        $pos = array();
        $num = 0;
        $p = 0;
// separated each punc'ed fragment into smaller fragments separated by stopwords        
        while ($p < count($frags[$i])) {
            if (array_key_exists($frags[$i][$p], $parser->stopwords)) {
                $pos[] = $p;
                $clauses[$num] = array();
                $num ++;
            }
            $p ++;
        }
        $pos[] = count($frags[$i]);

        $num = 0;
        $j = 0;
        if (count($pos) > 0) {
            while ($j < count($pos)) {
                $stop = FALSE;
                $k = $pos[$j]-1;
                while ($k >= 0 && !$stop) {
                    if (array_key_exists($frags[$i][$k], $parser->stopwords))
                        $stop = TRUE;
                    $clauses[$num][] = $frags[$i][$k];
                    $k --;
                }
                
                $clauses[$num] = array_reverse($clauses[$num]);
                $num ++;
                $j ++;
            }

           //send the array of stopped fragments to the parser
            $parser->parse($clauses);
            //$controller->setResponse($clauses);
        }
        $i ++;
    }
}

function parse($frags) {
    $i = 0;
    
    // more code here, commented out for the time being
    // below, send response to control hub for output
    $controller = Controller::getInstance();
    $controller->setResponse($frags[$i][0]);
}
4

5 に答える 5

1

を追加する

if (isset($frags[$i][0])) {

パーサー関数の先頭に修正したようです。それは十分に公平ですが、私はまだそれを理解できるかどうか完全にはわかりません.

于 2010-11-18T01:49:13.580 に答える
0

これは私にとってはうまくいきます...

$frags = array(array ('the', 'term', '\'AI\'', 'still', 'manages'), array ('to', 'creep'), array ('into', 'my', 'speech'), array ('or', 'writing.'));

print_r($frags);

Array ( [0] => Array ( [0] => the [1] => term [2] => 'AI' [3] => still [4] => manages ) [1] => Array ( [ 0] => [1] へ => クリープ ) [2] => 配列 ( [0] => [1] へ => 私の [2] => スピーチ ) [3] => 配列 ( [0] =>または [1] => 書き込み。) )

print "<br>";
print_r($frags[0]);

配列 ( [0] => [1] => 用語 [2] => 'AI' [3] => それでも [4] => 管理する )

print "<br>";
print $frags[0][0];

于 2010-11-17T23:38:02.507 に答える
0

何が問題なのかを知らず$parser->stopwordsに、サンプルを実行してどこが失敗しているかを確認するのは困難です。しかし、私の最初の観察は、$frags を parse() に渡しているのではなく、$clauses を渡しているということです。また、$clauses[0] に何も割り当てずに、$frags を $clauses に変換するループを通過する可能性もあります。

parse を呼び出す直前に vardump または print_r $clauses を実行することをお勧めします。

また、どの [0] が失敗しているのか疑問です。最初または2番目?簡単に伝える方法...これを解析してください:

function parse($frags) {
    $i = 0;

    // more code here, commented out for the time being
    // below, send response to control hub for output
    $controller = Controller::getInstance();

    if (!isset($frags[$i])) echo "failure at ".__LINE__." [$i]\n";
    if (!isset($frags[$i][0])) echo "failure at ".__LINE__." [$i][0]\n";

    $controller->setResponse($frags[$i][0]);
}

これは、どのインデックスが php の吐き気を引き起こしているかについて有益なはずです。

于 2010-12-16T06:49:46.250 に答える
0

$fragsは、貼り付けたコードの外で宣言されています (おそらく ::clause() への呼び出しの呼び出しスコープ内)。これは、変数名の競合で頻繁に発生します。このことを考慮:

<?php
$frags = array('apple','orange');
// other stuff
while ($frags = $query->next()) {
  //...
}
// the last value returned from $query->next() was false in this example
// .. more stuff

$this->clause($frags); // actually passes false, not the apple,orange array

これは私たちの最善に起こります (-:

于 2010-12-11T21:44:59.237 に答える
0
$grabs = array(0=> array(0=>"the",1=>"term",2=>"AI",3=> "still",4=> "manages"),
                1=> array(0=>"to",1=>"creep"),
                2=> array(0=>"into", 1=>"my" ,2=>"speech"),
                3=> array(0=> "or",1=>"writing")
                );
 echo $grabs[0][0];
于 2010-12-18T13:39:49.150 に答える