PHP English word count, _php tutorial

Source: Internet
Author: User
Tags strcmp strtok

PHP English word counting device,


The example of this article for everyone to share the English word statistics PHP implementation, for your reference, the specific content as follows

The program starts to run, press "Browse" button to select an English document, then press "Statistics Statistics" button, you can get all the words in alphabetical order, and the number of occurrences
Data document for testing: Data.txt
Drivers: word.php
output.php and stringtokenizer.php are programs that require the same folder
1. Words_statistics_php.png

2. word.php

<?php/** * Program to run, press "Browse" button to select an English document, and then press "Statistics" button, * you can get all the words in alphabetical order, and the number of occurrences * * Author: Xu Tong Chun author Tongchun xu * @ open source China Open so Urce, Chna communiity * finish date: June 10, 2016 completion date:10 June, */require ("stringtokenizer.php"); require ("output.  PHP "); if ($_post[' submit ']) {if ($_files["file"] ["error"] > 0) echo "Error:". $_files["File" ["Error"]. "
"; 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 ' found string: '. $st->counttokens (); $list =new LinkedList () ; while ($st->hasmoretokens ()) {$list->orderinsert ($st->nexttoken ());} $list->words_count (); $list->traversal (); fclose ($myfile); }}?>

English document Word statistics Statistics on 中文版 words

The program starts to run, press "Browse" button to select an English document, then press "Statistics Statistics" button, you can get all the words in alphabetical order, and the number of occurrences

3. output.php

 
  
 <?/** * The class LinkedList allows an application to store strings in * alphabetical order by calling OrderInsert (). * The LinkedList class, defined here, can call its method OrderInsert () to store the English string in the order of the letter * size.  * Simultaneous recording of the number of occurrences of English words * Author: Xu Tong Chun author Tongchun xu * @ open Source Chinese open source, China communiity * finish Date: June 10, 2016 completion date:10  June, */class node{public $data;  Public $frequency;  Public $next; function __construct ($data, $next = null, $frequency = 1) {$this->data = $data;//english string $this->next = $next; /pointer to subsequent nodes $this->frequency= $frequency;  The number of occurrences of the English string}} class linkedlist{private $head;//The head node of the single-linked list, does not store data function __construct () {//single-linked list construction method//Head node data is "puppet", does not represent   Any data $this->head = new Node ("Dummy puppet");  $this->first = null;  } function IsEmpty () {return ($this->head->next = = null);  }/* OrderInsert ($data) method, * The size of the given string $data, it is placed in the appropriate position, * to ensure that the single-linked list of strings stored, always orderly.    */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) {//Inserts a string into the tail of the single-linked list $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) {//query whether there is a given string $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 () {//Traverse single-linked list if (! $this->isempty ()) {$p = $this->head->next; echo "Output results:
 
 
     "; echo " 
    "; $n = 1; while ($p->next! = null) {$p = $p->next; echo " 
    "; $n + +; if ($n%11==0) echo " 
   
     "; } echo " 
   
". $p->data."
Number of occurrences: ". $p->frequency."
". $p->data."
Number of occurrences: ". $p->frequency."
"; }else echo "linked list is empty!" "; } function Words_count () {if ($this->isempty ()) echo "
No Strings stored
"; else{$counter = 0; $p = $this->head->next; while ($p->next! = null) {$p = $p->next; $counter + +; }; echo "* * * Total words". $counter. "A * * *"; }}}?>

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 tokenizer. The Code: * * <?php * $str = "This is:@\t\n a test!"; * $delim = "!@: ' \t\n\0";//Remove these chars * $st = new Stringtoke Nizer ($str, $delim); * echo ' total tokens: '. $st->counttokens (). ' 
'; * while ($st->hasmoretokens ()) {* echo $st->nexttoken (). '
'; *} * Prints the following output: * Total Tokens:4 * This * is * a * Test *?> *
*/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 token s) * 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 * is 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 is more tokens available from this tokenizer ' s string. It * Does not move the internal pointer on any. To move the internal pointer * to the next element call NextToken () * @return Boolean-true if have 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}}?>

The above is the whole content of this article, I hope that everyone's learning to help, but also hope that we support a lot of help the home.

http://www.bkjia.com/PHPjc/1136610.html www.bkjia.com true http://www.bkjia.com/PHPjc/1136610.html techarticle PHP English word statistics, this example for everyone to share the English word statistics PHP implementation, for your reference, the specific content as follows the program to start running, press "Browse" button to select an English ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.