2012-11-02 2 views
0

나는 완성하려고하는 작은 개인 프로젝트가 있습니다. 나는 일련의 문자들을 가져 와서 상기 문자열의 변형들로부터 단어들을 "만들어야"한다. 알려진 단어 목록 (단어는 줄 바꿈으로 구분됨)이있는 텍스트 파일을 검사합니다. 요약PHP를 사용하여 문자열을 가능한 단어와 비교하기

는 :

  • 사용자는
  • $ chars_provided
  • 다음
  • 가 폭발 폭발한다, 문자열 $ chars_provided (즉, "jdlwhfushfmgh")를 제공 $ 무작위로했다 문자열에서 단어를 만들려고 배열되어 chars_provided
  • 만든 단어 사전 텍스트 파일에 대해 확인/확인하여
  • 개의 결과가 만든 단어의 글자 수로 표시됩니다. h는 100 단어로 제한됩니다.

나는 내 머리 속에서 어떻게해야하는지 확신 할 수 없다는 개념이 있는데, 나는 그저 그 과정을 설명 할 수있는 사람을 찾고있다.

<?php 

// list of words, one per line 
$dictionary = file_get_contents('dictionary.txt'); 

// provided characters, in the end by user 
$chars_provided = "a,t,w,q,u,i,f,d,s,b,v,x,o"; 

// count the total # of characters 
$chars_count = strlen($chars_provided); 

// display given information so far 
echo "The letters '$chars_provided' were entered, totaling $chars_count letters."; 

// explode the characters by using the comma designator 
$break_chars = explode(",", $chars_provided); 

foreach ($break_chars as $letter) { 
    echo "$letter[0]"; 
} 
+0

"언 스크램블 된"? 그게 무슨 뜻 이죠? "상위 100 개 단어"를 표시 하시겠습니까? 무엇의 상위 100? – Jon

+0

이와 비슷한 것 : http://grecni.com/texttwist.php –

+0

좀 더 많은 코드 예제로 질문을 업데이트했습니다. 문자열을 가져 와서 해당 문자열의 변형에서 단어를 만들려고합니다. –

답변

0

희망 사용할 수 있습니다.

$file = file_get_contents("dictionary.txt"); 
    $SearchString = "jdlwhfushfmgh/maybeasencondword"; 
    $breakstrings = explode('/',$SearchString); 

    foreach ($breakstrings as $values) 
    { 
     if(!strpos($file, $values)) 
     { 
      echo $values." string not found!\n"; 
     } 
     else 
     { 
      echo $values." string Found!\n"; 
     } 
+0

고마워요! 그것은 제가 문자열로 무엇을해야 하는지를 이해하는데 조금 더 가까이 다가갔습니다. 제 질문을 업데이트하고 목표에 관한 질문에 코멘트를 추가했습니다. 감사! –

2

당신은, 사전에 각 단어의 문자 수를 얻을 그것에 누른 상태에서 사용자가 입력 한 문자 카운트에 일치하는 경우가 더 쉽습니다.

예를 들어 'aaab'을 사용하면 3 'a보다 작거나 같거나 1'b보다 작고 다른 단어는 일치하지 않는 단어입니다.

//// 1. Grab letter counts for your user input. 

$user_input_chars = 'abcdefg'; // for example 
$user_in_letter_counts = get_letter_counts($user_input_chars); 

// $letters[$char][$num] will contain all words that have exactly $num number of $char characters 
$letters = array('a' => array(), 'b' => array(), /* ...,*/ 'z' => array()); 

//// 2. Generate list of words with at least $number_of quantity of $letter characters 
//  (only have to be done once for any amount of user input if you keep this in memory) 
foreach ($words as $word){ 
    // get letter counts for each type of character for this word 
    $letter_counts = get_letter_counts($word); 
    // store in array of letters and count 
    foreach($letter_counts as $letter => $number_of){ 
     // we already have a word that had $number_of $letter characters; add word to existing array 
     if (isset($letters[$letter][$number_of])){ 
      $letters[$letter][$number_of][] = $word; 
     } // make array to record that this word has $number_of $letter characters 
     else { 
      $letters[$letter][$number_of] = array($word); 
     } 
     $number_of--; 
    } 
} 

//// 3. Find matching words. 
$potential_words = array(); 
foreach ($letters as $letter => $arr){ 
    foreach($arr as $num => $words){ 
     // if this array has less than or equal to the number of $letter characters that the user input has, 
     // add the words to the potential match list for that character 
     if ($num <= $arr[$user_in_letter_counts[$letter]]){ 
      $potential_words[$letter] = array_merge($potential_words[$letter], $words); 
     } 
    } 
} 

// the words must have met the requirement for each character, so only grab words that satisfy all conditions 
$all_matching_words = array_intersect($potential_words['a'], $potential_words['b'], /* ..., */ $potential_words['z']); 

// (It should be trivial to just grab 100 of these.) 

function get_letter_counts($word){ 
    $result = array(); 
    $result['a'] = substr_count($my_word, 'a'); 
    $result['b'] = substr_count($my_word, 'b'); 
    // ... 
    $result['z'] = substr_count($my_word, 'z'); 
    return $result; 
} 
+0

올바른 방법입니다. 누군가가 무작위 반복을 사용할 것을 제안 할 때마다 나는 항상 동일한 순열이 백만 번 선택되는 극단적 인 경우를 보라고 말한다. –

관련 문제