Php English word analyzer, _ PHP Tutorial

Source: Internet
Author: User
Tags strtok
Php English word statistician ,. Php English word Analyzer. this example shares the php implementation of the English word analyzer for your reference. the specific content is as follows: The program starts to run. press the browser button to select an English php English word analyzer,

This article provides an example of the php implementation of the English word statistician for your reference. the specific content is as follows:

The program starts to run. press the "browse" button to select an English document and then press the "Statistics" button to obtain all the words listed in alphabetical order and their occurrences.
Data document for testing: data.txt
Driver: word. php
Output. php and StringTokenizer. php are programs in the same folder.
1. words_statistics_PHP.png

2. word. php

<? Php/*** the program starts running. press the "browse" button to select an English document and then press the "statistics" button. * All words listed in alphabetical order are displayed, author: Xu Tongchun author Tongchun Xu * @ Open Source China Open Source, Chna communiity * completion date: June 10, 2016 completion date: 10 June, 2016 */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 'find the string :'. $ st-> countTokens (); $ list = new queue list (); while ($ st-> hasMoreTokens ()) {$ list-> orderInsert ($ st-> nextToken ();} $ list-> words_count (); $ list-> traversal (); fclose ($ myfile) ;}}?> English document word Statistics on English words

The program starts to run. press the "browse" button to select an English document and then press the "Statistics" button to obtain all the words listed in alphabetical order and their occurrences.

3. output. php

 <? /*** The class into list allows an application to store strings in * alphabetical order by calling orderInsert (). * the sort list class defined here can call its method orderInsert () to store English strings in the order of letters. * The number of times English words appear simultaneously * author: Xu Tongchun author Tongchun Xu * @ Open Source China Open Source, China communiity * completion date: June 10, 2016 completion date: 10 June, 2016 */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 the next node $ this-> frequency = $ frequency; // Number of times the English string appears} class sequence list {private $ head; // The header node does not store the data function _ construct () {// The construction method of a single-chain table // the data of the header node is "slave ", does not represent any data $ this-> head = new Node ("dummy"); $ this-> first = null;} function isEmpty () {return ($ this-> head-> next = null);}/* orderInsert ($ data) method, * according to the size of the given string $ data, insert it to an appropriate location * to ensure that the strings in a single-chain table are stored in an orderly manner. */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) {// Insert the string to the end of the single-chain table $ 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 a given string exists $ 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 a single-chain table if (! $ This-> isEmpty () {$ p = $ this-> head-> next; echo "output result:
 
 
     "; 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" the linked list is empty! ";} Function words_count () {if ($ this-> isEmpty () echo"
No string is saved
"; Else {$ counter = 0; $ p = $ this-> head-> next; while ($ p-> next! = Null) {$ p = $ p-> next; $ counter ++ ;}; echo "*** Total Words ". $ 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: *  * <?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().'
'; * 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 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 }}?>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Examples in this article share the php implementation of the English word statistician for your reference. the specific content is as follows...

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.