php - Nasıl anahtar kelimeleri bir ilişkisel dizi için bir dize dönüştürmek mi

3 Cevap php

Örnek olarak bu dize almak: "Londra'da yarın ve Kent yarından sonra göreceksin".

Bunu nasıl tercihen bu gibi ortak kelimeleri eksik iken, anahtarlar gibi anahtar kelimeleri içeren bir ilişkisel dizi dönüştürmek istiyorsunuz:

Array ([yarın] => 2 [Londra] => 1 [Kent] => 1)

Herhangi bir yardım büyük takdir.

3 Cevap

dahil edilmeyecek kelimelerin bir kara liste kullanarak

$str = 'will see you in London tomorrow and Kent the day after tomorrow';
$skip_words = array( 'in', 'the', 'will', 'see', 'and', 'day', 'you', 'after' );
// get words in sentence that aren't to be skipped and count their values
$words = array_count_values( array_diff( explode( ' ', $str ), $skip_words ) );

print_r( $words );

Yapabileceğini söyleyebilirim:

  • split the string into an array of words
    • ile explode
    • ya da preg_split
    • karmaşıklığına bağlı olarak sizin kelime ayırıcılar için kabul edeceğiz
  • use array_filter to only keep the lines (i.e. words) you want
    • geri arama işlevi, tüm geçerli olmayan-kelimeleri yanlış geri dönmek zorunda kalacak
  • and, then, use array_count_values on the resulting list of words
    • Her kelime kelime dizisinde kaç kere saymak hangi



EDIT : ve sadece eğlence için, burada hızlı bir örnek:

Her şeyden önce, dize, o kelimelerin içine patladı alır:

$str = "will see you in London tomorrow and Kent the day after tomorrow";
$words = preg_split('/\s+/', $str, -1, PREG_SPLIT_NO_EMPTY);
var_dump($words);

Hangi seni alır:

array
  0 => string 'will' (length=4)
  1 => string 'see' (length=3)
  2 => string 'you' (length=3)
  3 => string 'in' (length=2)
  4 => string 'London' (length=6)
  5 => string 'tomorrow' (length=8)
  6 => string 'and' (length=3)
  7 => string 'Kent' (length=4)
  8 => string 'the' (length=3)
  9 => string 'day' (length=3)
  10 => string 'after' (length=5)
  11 => string 'tomorrow' (length=8)


Then, the filteting :

function filter_words($word) {
    // a pretty simple filter ^^
    if (strlen($word) >= 5) {
        return true;
    } else {
        return false;
    }
}
$words_filtered = array_filter($words, 'filter_words');
var_dump($words_filtered);

Hangi çıkışlar:

array
  4 => string 'London' (length=6)
  5 => string 'tomorrow' (length=8)
  10 => string 'after' (length=5)
  11 => string 'tomorrow' (length=8)


And, finally, the counting :

$counts = array_count_values($words_filtered);
var_dump($counts);

Ve sonuç:

array
  'London' => int 1
  'tomorrow' => int 2
  'after' => int 1


Now, up to you to build up from here ;-)
Mainly, you'll have to work on :

  • Ponctuation ile ilgilenen daha iyi bir patlama fonksiyonu, (or deal with that during filtering)
  • Benimkinden daha ihtiyaçlarınızı daha iyi uyan bir "akıllı" filtreleme fonksiyonu,

Eğlenin!

Ortak kelimelerin bir tablo olabilir, o değil, eğer zaten varsa, o zaman için ilişkilendirilebilir dizinin, veya +1 eklemek, bu tabloda varsa kontrol, bir anda dize tek bir kelime geçer.