php英文单词统计器


Posted in PHP onJune 23, 2016

本文实例为大家分享了英文单词统计器php 实现,供大家参考,具体内容如下

程序开始运行, 按"浏览"钮选择一个英文文档, 再按"统计 Statistics"钮, 即可得到按字母顺序列出的所有单词,及其出现的次数
用于测试的数据文档: data.txt
驱动程序:word.php
output.php 和 StringTokenizer.php 是 要求在同一个文件夹中的程序
1. words_statistics_PHP.png   

php英文单词统计器

2. word.php

<html>
<style>
td{
  background-color:#CF6;
  width:100px;
  margin:5px;
}
</style>
<body>
<?php
/**
 * 程序开始运行, 按"浏览"钮选择一个英文文档, 再按"统计"钮,
 * 即可得到按字母顺序列出的所有单词,及其出现的次数
 * 
 * 作者: 许同春 author Tongchun Xu 
 * @开源中国 Open Source, Chna communiity
 * 完成日期:2016年6月10日 completion date: 10 June, 2016
 */
 
require("StringTokenizer.php");
require("output.php");
  if($_POST['submit']){
  if ($_FILES["file"]["error"] > 0)
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  else {
$myfile = fopen($_FILES["file"]["tmp_name"], "r") or die("Unable to open file!");
$str = fread($myfile,filesize($_FILES["file"]["tmp_name"]));
$delim = "?\\,. /:!\"()\t\n\r\f%";
$st = new StringTokenizer($str, $delim);
echo '找到字符串: '.$st->countTokens();
$list=new LinkedList();
 while ($st->hasMoreTokens()) {
 $list->orderInsert($st->nextToken());
 }
$list->words_count();
$list->traversal();
fclose($myfile);
  }
}
?>
<h2>英文文档单词统计 Statistics on English words </h2>
<p>程序开始运行, 按"浏览"钮选择一个英文文档, 再按"统计 Statistics"钮,
 即可得到按字母顺序列出的所有单词,及其出现的次数 </p>
 
<form action="word.php" method="post"
enctype="multipart/form-data">
<label for="file">英文文档名 File Name:</label>
<input type="file" name="file" id="file" /> 
<input type="submit" name="submit" value="统计 Statistics" />
</form>
</body>
</html>

3. output.php

<meta charset="utf-8" />
<?
/**
 * The class LinkedList allows an application to store strings in
 * alphabetical order by calling orderInsert().
 * 此处定义的 LinkedList 类,可以调用它的 方法 orderInsert(),来以字母
 * 大小的顺序储存 英文字符串。
 * 同时记录 英文单词出现的次数
 * 作者: 许同春 author Tongchun Xu 
 * @开源中国 Open Source, China communiity
 * 完成日期:2016年6月10日 completion date: 10 June, 2016
 */
class Node{
  public $data;
  public $frequency;
  public $next;
  function __construct($data, $next = null, $frequency = 1){
    $this->data = $data; //英文字符串
    $this->next = $next; //指向后继结点的指针
    $this->frequency=$frequency; //英文字符串出现的次数
  }  
}
 
class LinkedList{
  private $head; //单链表的头结点,不存储数据
 function __construct(){//单链表的构造方法
  //头结点的数据为"傀儡", 不代表 任何数据
  $this->head = new Node("dummy 傀儡"); 
  $this->first = null;
  }
 
 function isEmpty(){ 
    return ($this->head->next == null);
  }  
/* orderInsert($data) 方法, 
 * 按给定字符串 $data 的大小, 将其安插到适当的位置,  
 * 以保证单链表中字符串的存储,始终是有序的。 
 */
 function orderInsert($data){
  $p = new Node($data);  
  if($this->isEmpty()){
    $this->head->next = $p;
  }
  else {
  $node= $this->find($data);
  if(!$node){
  $q = $this->head;
  while($q->next != NULL && strcmp($data, $q->next->data)> 0 ){
  $q = $q->next;
    }
    $p->next = $q->next; 
    $q->next = $p;
  }else
  $node->frequency++;
  }
 }
 
 function insertLast($data){//将字符串插到单链表的尾部
  $p = new Node($data);
   
  if($this->isEmpty()){
    $this->head->next = $p;
  }
  else{
    $q = $this->head->next;
    while($q->next != NULL)
      $q = $q->next;
    $q->next = $p; 
  }   
}
 
  function find($value){//查询是否有给定的字符串
    $q = $this->head->next;
    while($q->next != null){
    if(strcmp($q->data,$value)==0){
        break;
      }
      $q = $q->next;  
    }
    if ($q->data == $value)
    return $q; 
    else
    return null;
  }
 
  function traversal(){//遍历单链表
    if(!$this->isEmpty()){
    $p=$this->head->next;
    echo "输出结果:<table><tr>";
    echo "<td>".$p->data."<br>出现次数:".$p->frequency."</td>";
    $n=1;
    while($p->next != null){
      $p=$p->next;
      echo "<td>".$p->data."<br>出现次数:".$p->frequency."</td>";
      $n++;
      if ($n%11==0) echo "</tr><tr>";
      }
       
      echo "</tr></table>";      
    }else
    echo "链表为空!";
  }
   
   
  function words_count(){
  if($this->isEmpty())
  echo "<br>没有储存字符串 <br>";
    else{
  $counter=0;
  $p=$this->head->next;
  while($p->next != null){
  $p=$p->next;
  $counter++;
      };
  echo "***共有单词 ".$counter." 个***";
    }
  }} 
?>

4. StringTokenizer.php

<?php
 
/**
 * The string tokenizer class allows an application to break a string into tokens.
 *
 * @author Azeem Michael
 * @example The following is one example of the use of the tokenizer. The code:
 * <code>
 * <?php
 * $str = "this is:@\t\n a test!";
 * $delim = " !@:'\t\n\0"; // remove these chars
 * $st = new StringTokenizer($str, $delim);
 * echo 'Total tokens: '.$st->countTokens().'<br/>';
 * while ($st->hasMoreTokens()) {
 * echo $st->nextToken() . '<br/>';
 * }
 * prints the following output:
 * Total tokens: 4
 * this
 * is
 * a
 * test
 * ?>
 * </code>
 */
class StringTokenizer {
 
  /** @var string
   */
  private $string;
 
  /** @var string
   */
  private $token;
 
  /** @var string
   */
  private $delim;
 
  /**
   * Constructs a string tokenizer for the specified string.
   * @param string $str String to tokenize
   * @param string $delim The set of delimiters (the characters that separate tokens)
   * specified at creation time, default to " \n\r\t\0"
   */
  public function __construct($str, $delim=" \n\r\t\0") {
    $this->string = $str;
    $this->delim = $delim;
    $this->token = strtok($str, $delim); 
  }
 
  /**
   * Destructor to prevent memory leaks
   */
  public function __destruct() {
    unset($this);
  }
 
  /**
   * Calculates the number of times that this tokenizer's nextToken method can
   * be called before it generates an exception
   * @return int - number of tokens
   */
  public function countTokens() {
    $counter = 0;
    while($this->hasMoreTokens()) {
      $counter++;
      $this->nextToken();
    }
    $this->token = strtok($this->string, $this->delim);
    return $counter; 
  }
 
  /**
   * Tests if there are more tokens available from this tokenizer's string. It
   * does not move the internal pointer in any way. To move the internal pointer
   * to the next element call nextToken()
   * @return boolean - true if has more tokens, false otherwise
   */
  public function hasMoreTokens() {
    return ($this->token !== false);
  }
 
  /**
   * Returns the next token from this string tokenizer and advances the internal
   * pointer by one.
   * @return string - next element in the tokenized string
   */
  public function nextToken() {
    $hold = $this->token; //hold current pointer value
    $this->token = strtok($this->delim); //increment pointer
    return $hold; //return current pointer value
  }
}
?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
浅谈php中mysql与mysqli的区别分析
Jun 10 PHP
编写Smarty插件在模板中直接加载数据的详细介绍
Jun 26 PHP
浅析echo(),print(),print_r(),return之间的区别
Nov 27 PHP
CI框架中libraries,helpers,hooks文件夹详细说明
Jun 10 PHP
PHP对象递归引用造成内存泄漏分析
Aug 28 PHP
php返回相对时间(如:20分钟前,3天前)的方法
Apr 14 PHP
PHP中preg_match函数正则匹配的字符串长度问题
May 27 PHP
PHP实现简单的新闻发布系统实例
Jul 28 PHP
PHP 前加at符合@的作用解析
Jul 31 PHP
php中文乱码问题的终极解决方案汇总
Aug 01 PHP
TP5(thinkPHP5)框架基于ajax与后台数据交互操作简单示例
Sep 03 PHP
thinkPHP5使用Rabc实现权限管理
Aug 28 PHP
php抽奖概率算法(刮刮卡,大转盘)
Apr 17 #PHP
Yii2 rbac权限控制之rule教程详解
Jun 23 #PHP
[原创]php使用curl判断网页404(不存在)的方法
Jun 23 #PHP
PHP生成静态HTML文档实现代码
Jun 23 #PHP
PHP微信支付开发实例
Jun 22 #PHP
[原创]解决wincache不支持64位PHP5.5/5.6的问题(提供64位wincache下载)
Jun 22 #PHP
PHP树-不需要递归的实现方法
Jun 21 #PHP
You might like
PHP session有效期问题
2009/04/26 PHP
队列在编程中的实际应用(php)
2010/09/04 PHP
php实现MySQL数据库备份与还原类实例
2014/12/09 PHP
WebQQ最新登陆协议的用法
2014/12/22 PHP
PHP编程文件处理类SplFileObject和SplFileInfo用法实例分析
2017/07/22 PHP
正负小数点后两位浮点数实现原理及代码
2013/09/06 Javascript
javascript动态添加删除tabs标签的方法
2015/07/06 Javascript
谈一谈js中的执行环境及作用域
2016/03/30 Javascript
用jmSlip编写移动端顶部日历选择控件
2016/10/24 Javascript
Bootstrap复选框和单选按钮美化插件(推荐)
2016/11/23 Javascript
Angular2入门教程之模块和组件详解
2017/05/28 Javascript
实例讲解DataTables固定表格宽度(设置横向滚动条)
2017/07/11 Javascript
nodejs和react实现即时通讯简易聊天室功能
2019/08/21 NodeJs
python生成1行四列全2矩阵的方法
2018/08/04 Python
Flask框架web开发之零基础入门
2018/12/10 Python
Django数据库类库MySQLdb使用详解
2019/04/28 Python
python实现基于朴素贝叶斯的垃圾分类算法
2019/07/09 Python
django做form表单的数据验证过程详解
2019/07/26 Python
执行Django数据迁移时报 1091错误及解决方法
2019/10/14 Python
tensorflow 查看梯度方式
2020/02/04 Python
解决Pycharm中恢复被exclude的项目问题(pycharm source root)
2020/02/14 Python
Python openpyxl 插入折线图实例
2020/04/17 Python
浅析python实现动态规划背包问题
2020/12/31 Python
使用CSS3的背景渐变Text Gradient 创建文字颜色渐变
2014/08/19 HTML / CSS
Blank NYC官网:夹克、牛仔裤等
2020/12/16 全球购物
党员党性分析材料
2014/02/17 职场文书
《守株待兔》教学反思
2014/03/01 职场文书
新闻传媒系求职信范文
2014/04/19 职场文书
售后服务承诺函格式
2015/01/21 职场文书
讲文明倡议书
2015/04/29 职场文书
纪检部部长竞选稿
2015/11/21 职场文书
Mysql基础之常见函数
2021/04/22 MySQL
html+css实现文字折叠特效实例
2021/06/02 HTML / CSS
详解Java实践之建造者模式
2021/06/18 Java/Android
国产动画《万圣街》日语配音版制作决定!
2022/03/20 国漫
Apache自带的ab压力测试工具的实现
2022/07/23 Servers