예제 #1
0
 private function findPersonBio($page, $person, $org)
 {
     //$this->printDebug('');
     $name_re = LsString::escapeStringForRegex($person->name_last);
     if (preg_match('/<title>([^<]*)<\\/title>/is', $page, $match)) {
         if (stristr($match[1], $person->name_last) && stristr($match[1], $person->name_first) && strlen($person->name_first) > 2) {
             $name_re .= '|' . LsString::escapeStringForRegex($person->name_first);
         }
     }
     $layout_tags = implode('|', LsHtml::$layoutTags);
     $re2 = '/>([^<]*?(' . $name_re . ')(\\s|,|<)(.*?))<\\/?(' . $layout_tags . ')/is';
     $re = $re2 . 'u';
     //$this->printDebug($re);
     $bio_match = null;
     if (preg_match_all($re, $page, $matches) || preg_match_all($re2, $page, $matches)) {
         //$this->printDebug('matches found');
         $arr = array();
         $most_reqs = 0;
         $qual = false;
         $news = false;
         foreach ($matches[1] as $match) {
             if (stristr($match, '}') || stristr($match, '{') || preg_match('/\\svar\\s/is', $match)) {
                 //$this->printDebug('FAILED - curly brackets');
                 continue;
             }
             $str = LsHtml::replaceEntities($match);
             $str = LsHtml::stripTags($str, '');
             $str = trim(LsString::spacesToSpace($str));
             $this->printDebug(strlen($str));
             if (strlen($str) > 3000) {
                 $this->printDebug('FAILED - str too long');
                 continue;
             }
             if (preg_match('/(^|\\b)(' . $name_re . ')\\b/is', $str) == 0) {
                 $this->printDebug($match . 'FAILED - no name match');
                 continue;
             }
             $word_count = count(explode(' ', $str));
             if ($word_count < 12) {
                 $this->printDebug('FAILED - str not long enough');
                 continue;
             } else {
                 if (stristr($str, 'announce') || stristr($str, 'today') || stristr($str, '—') || stristr($str, '–') || preg_match('/^[^\\-]{0,100}\\-(\\-|\\s)/is', $str)) {
                     $news = true;
                     $this->printDebug('FAILED: dash / announced / today');
                 } else {
                     if (preg_match('/(^|\\s)([\'"”])([^\\1]+)\\1/is', $str, $qm) && count(explode(' ', $qm[0])) > 6) {
                         $news = true;
                         $this->printDebug('FAILED: quote');
                     } else {
                         if (preg_match_all('/\\s(\\p{Ll})+\\b/su', $str, $lcm) < 5) {
                             $this->printDebug('FAILED: not enough lowercase');
                         } else {
                             $bio_words = PersonTable::$commonBioWords;
                             if (in_array('Lobbyist', $person->getExtensions())) {
                                 $bio_words = array_merge($bio_words, LobbyistTable::$commonBioWords);
                             }
                             $bio_words = implode('|', $bio_words);
                             $bio_word_ct = preg_match_all('/\\s(' . $bio_words . ')\\s/is', $str, $matches);
                             $str = trim($str);
                             if (preg_match('/\\.$/is', $str) == 0) {
                                 $this->printDebug('no period at end of string');
                             } else {
                                 if ($bio_word_ct > 1) {
                                     $news = false;
                                     $qual = true;
                                     $arr[] = $str;
                                 } else {
                                     $this->printDebug('less than 2 bio words');
                                     if ($news == false) {
                                         $str = preg_replace('/^[\\,\\.\\:\\;]\\s*/su', '', $str);
                                         $arr[] = $str;
                                         //array('str' => $str, 'bio_words' => $bio_word_ct);
                                     }
                                 }
                             }
                         }
                     }
                 }
                 //$this->printDebug('');
             }
         }
         if ($qual) {
             $arr = array_unique($arr);
             $ret = false;
             $bio = implode("\n\n", $arr);
             //$this->printDebug($name_re);
             if (strlen($bio) < 3000 && LsString::withinN($bio, '(' . $name_re . ')', '(is|was|holds|led|has|had|provides|practices|served|leads)', 2)) {
                 if (preg_match('/^.*?\\b(' . $name_re . ')\\b/is', $bio, $m) && count(explode(' ', $m[0])) < 20) {
                     $ret = true;
                     $this->printDebug('SUCCESS');
                 }
             } else {
                 $this->printDebug('within N failed !!!!');
             }
             $org_test = true;
             if ($ret && stristr($org->name, $person->name_last)) {
                 $org_test = false;
                 if (strlen($person->name_first) > 1) {
                     if (preg_match('/([^\\s]+\\s+){0,14}/is', $arr[0], $beg_match)) {
                         $nf_re = LsString::escapeStringForRegex($person->name_first);
                         if (preg_match('/\\b' . $nf_re . '\\b/is', $beg_match[0]) || preg_match('/\\b(Mr|Mrs|Ms)\\b/su', $arr[0])) {
                             $org_test = true;
                             //$this->printDebug('PASSED FIRST NAME TEST');
                         }
                     }
                 } else {
                     if (preg_match('/\\b(he|she|him|her|his|mr|ms|mrs)\\b/is', $arr[0])) {
                         $org_test = true;
                         //$this->printDebug('PASSED POSSESSIVE TEST');
                     }
                 }
             }
             if ($ret && $org_test) {
                 return $bio;
             }
         }
     } else {
         $this->printDebug('no matches found');
     }
     return false;
 }