1 Star 0 Fork 54

CXMan/StudyStudio

forked from JoyPoint/StudyStudio 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
word_table.php 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
JoyPoint 提交于 2019-10-07 16:08 +08:00 . WebCode v1.0
<!DOCTYPE html>
<!-- word_table.php
Uses a function to split a given string of text into
its constituant words. It also determines the frequency of
occurrence of each word. The words are separated by whitespace
or punctuation, possibly followed by whitespace. The
punctuation can be a period, a comma, a semicolon, a colon,
an exclamation point, or a questionmark.
The main driver program calls the function and displays
the results.
-->
<html lang = "en">
<head>
<title> word_table.php </title>
<meta charset = "utf-8" />
</head>
<body>
<?php
// Function splitter
// Parameter: a string of text containing words and punctuation
// Returns: an array in which the unique words of the string are
// the keys and their frequencies are the values.
function splitter($str) {
// Create the empty word frequency array
$freq = array();
// Split the parameter string into words
$words = preg_split("/[ .,;:!?]\s*/", $str);
// Loop to count the words (either increment or initialize to 1)
foreach ($words as $word) {
$keys = array_keys($freq);
if(in_array($word, $keys))
$freq[$word]++;
else
$freq[$word] = 1;
}
return $freq;
} #** End of splitter
// Main test driver
$str = "apples are good for you, or don't you like apples?
or maybe you like oranges better than apples";
// Call splitter
$tbl = splitter($str);
// Display the words and their frequencies
print "<br /> Word Frequency <br /><br />";
$sorted_keys = array_keys($tbl);
sort($sorted_keys);
foreach ($sorted_keys as $word)
print "$word $tbl[$word] <br />";
?>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zuofengjun/StudyStudio.git
git@gitee.com:zuofengjun/StudyStudio.git
zuofengjun
StudyStudio
StudyStudio
master

搜索帮助