단어수 세기
예전에 다른 곳에 답변으로 올려봤던 프로그램인데, 보다 간략화해 여기에 올려봅니다. 한글이나 영문 상관없습니다. 공백을 기준으로 분리한 다음 해당 문자열을 첨자로 하여 카운트를 증가시키고, 완료되면 정렬을 통해 빈도수가 높은 단어부터 출력되게 했습니다.
정확한 단어 빈도수를 세자면 모든 문자의 소문자화(또는 대문자화)를 하고, 쉼표(,)를 공백으로, "등도 공백으로 바꾸는 등 어느 정도의 문자열 가공을 끝낸 상태로 해야 합니다.
아래 예문은 http://php.net 의 document 내용 중의 일부입니다. ^^;
<?
$string = "Notice how this is different from a CGI script written in other languages like Perl or C -- instead of writing a program with lots of commands to output HTML, you write an HTML script with a some embedded code to do something (in this case, output some text). The PHP code is enclosed in special start and end tags that allow you to jump into and out of PHP mode. What distinguishes PHP from something like client-side Javascript is that the code is executed on the server. If you were to have a script similar to the above on your server, the client would receive the results of running that script, with no way of determining what the underlying code may be. You can even configure your web server to process all your HTML files with PHP, and then there's really no way that users can tell what you have up your sleeve.";
$data = explode(" ",$string);
$size = sizeof($data);
for ($i=0; $i<$size; $i++) $temp[$data[$i]]++;
arsort($temp);
while(list($name,$value) = each($temp)) {
echo "$name - $value<br>\n";
}
?>
정확한 단어 빈도수를 세자면 모든 문자의 소문자화(또는 대문자화)를 하고, 쉼표(,)를 공백으로, "등도 공백으로 바꾸는 등 어느 정도의 문자열 가공을 끝낸 상태로 해야 합니다.
아래 예문은 http://php.net 의 document 내용 중의 일부입니다. ^^;
<?
$string = "Notice how this is different from a CGI script written in other languages like Perl or C -- instead of writing a program with lots of commands to output HTML, you write an HTML script with a some embedded code to do something (in this case, output some text). The PHP code is enclosed in special start and end tags that allow you to jump into and out of PHP mode. What distinguishes PHP from something like client-side Javascript is that the code is executed on the server. If you were to have a script similar to the above on your server, the client would receive the results of running that script, with no way of determining what the underlying code may be. You can even configure your web server to process all your HTML files with PHP, and then there's really no way that users can tell what you have up your sleeve.";
$data = explode(" ",$string);
$size = sizeof($data);
for ($i=0; $i<$size; $i++) $temp[$data[$i]]++;
arsort($temp);
while(list($name,$value) = each($temp)) {
echo "$name - $value<br>\n";
}
?>
함께 나눈 이야기
댓글댓글 남기기