4

段落文字列を文の配列に分割したい。もちろん、段落を文に分割するために文字ドット(。)を使用した正規表現を使用しています。問題は、文中の学術タイトルの略語であり、すべての略語はドット(。)を使用しています。だから私の正規表現は段落を分割するのは完全に間違っています。

段落の例を次に示します。

一方、ボゴール農科大学の学長であるヘリー・スハルディヤント博士は、彼の発言の中で、大学院生は勉強を続け、時間通りに勉強を終えることを要求しました。その一般の聴衆の中には、ボゴール農科大学大学院の副学部長、ボゴール農科大学大学院博士課程長官のデディ・ジュサディ博士が出席しました。マリミン。

正規表現としてドット(。)のみを使用すると、次のようになります。

Array (
[0] => Meanwhile Rector of Bogor Agricultural University, Prof
[1] => Dr
[2] => Herry Suhardiyanto, in his remarks requested that the graduate students should keep on studying and will finalize their studies on time
[3] => ...
)

そしてこれは実際に私が欲しかった:

Array (
[0] => Meanwhile Rector of Bogor Agricultural University, Prof. Dr. Herry Suhardiyanto, in his remarks requested that the graduate students should keep on studying and will finalize their studies on time
[1] => Present in  that general audience were  the Deputy Dean of the Graduate School of Bogor Agricultural University, Dr.Dedi Jusadi, Secretary of the Graduate School for Doctoral Program of Bogor Agricultural University, Prof.Dr. Marimin
)
4

2 に答える 2

3

負の後読みを使用できます。

((?<!Prof)(?<!Dr)(?<!Mr)(?<!Mrs)(?<!Ms))\.必要に応じてさらに追加

ここで説明されたデモ: http://regex101.com/r/xQ3xF9

コードは次のようになります。

$text="Meanwhile Rector of Bogor Agricultural University, Prof. Dr. Herry Suhardiyanto, in his remarks about Mr. John requested that the graduate students should keep on studying and will finalize their studies on time. Present in that general audience were Mrs. Peterson of the Graduate School of Bogor Agricultural University, Dr.Dedi Jusadi, Secretary of the Graduate School for Doctoral Program of Bogor Agricultural University, Prof.Dr. Marimin.";

$titles=array('(?<!Prof)', '(?<!Dr)', '(?<!Mr)', '(?<!Mrs)', '(?<!Ms)');
$sentences=preg_split('/('.implode('',$titles).')\./',$text);
print_r($sentences);
于 2013-03-09T04:51:03.403 に答える
1

これは機能しているように見えますが、厳密には RegEx に対する新しい PHP 関数です -

$begin = array( 0=>'Meanwhile in geography,',
            1=>'Dr',
            2=>'Henry Suhardiyanto, in his remarks, stated that ',
            3=>'Dr',
            4=>'Prof',
            5=>'Jedi Dusadi was another ',
            6=>'Prof');

$exclusions = array("Dr", "Prof", "Mr", "Mrs");

foreach ($begin as $pos => $sentence) {
if (in_array($sentence, $exclusions)) {
    $begin[$pos+1] = $sentence . ". " . $begin[$pos+1];
    unset($begin[$pos]);
    array_values($begin);
    }
}    
于 2013-03-09T05:05:57.347 に答える