2016-05-21 20 views
2

私は、最初の文字に応じて各単語をグループ化し、グループを他の文字でソートしたいという段落があります。最初の文字に応じて単語を分割する

サンプルテキスト:最後は、市民頼むかもしれないのはなぜ最初sentence-

ため

$text = "Why end might ask civil again spoil. She dinner she our horses depend. Remember at children by reserved to vicinity. In affronting unreserved delightful simplicity ye. Law own advantage furniture continual sweetness bed agreeable perpetual. Oh song well four only head busy it. Afford son she had lively living. Tastes lovers myself too formal season our valley boy. Lived it their their walls might to by young."; 

期待される結果は、再びそれを行うには

a => again, ask 
c => civil 
e => end 
m => might 
s => spoil 
w => Why 
+0

そして、何が結果を正確に予想されるのですか? – Shafizadeh

+0

各単語で段落をグループ化し、他の文字でグループを並べ替える必要があります。 –

+0

@Shafizadeh、私は何が必要な私の質問を更新しました。 –

答えて

1

非常に多くの方法を台無しに...私はちょうど私が少し興味深い(ちょうど "おかしいスクリプト" ;-)より見つけたものを選んだ

これは、例えば、

---の---
シーズン
彼女
彼女
彼女
シンプル

または

<?php 
$text = "Why end might ask civil again spoil. She dinner she our horses depend. Remember at children by reserved to vicinity. In affronting unreserved delightful simplicity ye. Law own advantage furniture continual sweetness bed agreeable perpetual. Oh song well four only head busy it. Afford son she had lively living. Tastes lovers myself too formal season our valley boy. Lived it their their walls might to by young."; 

// build 
$result = []; 
foreach(preg_split('![^a-zA-Z]+!', $text, -1, PREG_SPLIT_NO_EMPTY) as $word) { 
    // here goes the case-sensitivity; it's all lower-case from now on.... 
    $word = strtolower($word); 
    $char = $word[0]; 
    // not storing as the element's value but the key 
    // takes care of doublets 
    $result[$char][$word] = true; 
} 

// get keys & sort 
$result = array_map(
    function($e) { 
     // remember? The actual words have been stored as the keys 
     $e = array_keys($e); 
     usort($e, 'strcasecmp'); 
     return $e; 
    }, 
    $result 
); 


// print 
var_export($result); 
+0

ありがとう、しかし、私は '' array(a => array(again、ask)、...) 'のような多次元配列として必要です、あなたのスクリプトから可能ですか? –

+0

これはSPLHeapsの配列、つまりイテレータの配列です。あなたのユースケースに(実際の)違いがありますか? – VolkerK

+0

それは単純な配列に変換することは可能ですか?私はヒープを理解していない、私は単純な配列で動作することはほとんどできません。 –

0

私のソリューションは、すでにソートされた単語を最初の文字でフレーズに分割する正規表現の周りに構築されています。

    (\w)
  • :次に単語の最初の文字に一致する文字(技術的には任意の「単語」の文字)、
  • .*?一致捕捉基:(ひとつの文字の可能な最小数とすることができます単語、またはいくつかの)
  • ($| (?!\\1))続く:最初のキャプチャグループと同じ文字が続くテキストまたはスペースないの一番最後。
$text = "Why end might ask civil again spoil. She dinner she our horses" 
    . " depend. Remember at children by reserved to vicinity. In affronting" 
    . " unreserved delightful simplicity ye. Law own advantage furniture" 
    . " continual sweetness bed agreeable perpetual. Oh song well four only" 
    . " head busy it. Afford son she had lively living. Tastes lovers" 
    . " myself too formal season our valley boy. Lived it their their walls" 
    . " might to by young."; 

// Split the text into individual words and sort them, case insensitively. 
$words = preg_split("[\W+]", $text); 
natcasesort($words); 

// Join the sorted words back together and break them into phrases by 
// initial letter. 
preg_match_all("[(\w).*?($| (?!\\1))]i", implode(" ", $words), $matches); 

// Arrange the phrases into an array keyed by lower-case initial letter, 
// split them back into an array of words. 
$words = array_combine(
    array_map("strtolower", $matches[1]), 
    array_map(function($phrase){ return explode(" ", trim($phrase)); }, 
       $matches[0])); 

var_dump($words); 

/* 
array (size=19) 
    'a' => 
    array (size=7) 
     0 => string 'advantage' (length=9) 
     1 => string 'Afford' (length=6) 
     2 => string 'affronting' (length=10) 
     3 => string 'again' (length=5) 
     4 => string 'agreeable' (length=9) 
     5 => string 'ask' (length=3) 
     6 => string 'at' (length=2) 
    'b' => 
    array (size=5) 
     0 => string 'bed' (length=3) 
     1 => string 'boy' (length=3) 
     2 => string 'busy' (length=4) 
     3 => string 'by' (length=2) 
     4 => string 'by' (length=2) 
    ... 
*/ 
+0

ありがとう、答えは。 –

関連する問題