PHP Classes

File: text.class.php

Recommend this page to a friend!
  Classes of Ewg   Parse keywords from text   text.class.php   Download  
File: text.class.php
Role: Class source
Content type: text/plain
Description: Class-file
Class: Parse keywords from text
Extract keywords from text file
Author: By
Last change: up
Date: 15 years ago
Size: 1,696 bytes
 

Contents

Class file image Download
<?
/**
 * @package Text class
 * @uses This class can extract keywords from text file
 */
class Text
{
   
/**
     * Source text
     *
     * @var String
     */
   
private $text = '';

   
/**
     * All words
     *
     * @var Array
     */
   
private $words = '';

   
/**
     * Preparation
     *
     * @param $text_file String
     * @param $min_strlen Int
     */
   
public function __construct ($text_file)
    {
        if (
file_exists ($text_file))
        {
       
$this->text = file_get_contents ($text_file);
        }
        else
        {
            throw new
Exception("File $text_file not found");
        }
   
$this->text2words ();
   
$this->gen_words ();
    }

   
/**
     * Convert text to array of words
     */
   
private function text2words ()
    {
   
preg_match_all ('/(\w+)/i', $this->text, $this->words);
   
$this->words = $this->words ['0'];
    }

   
/**
     * Returns words from array of words
     *
     */
   
private function gen_words ()
    {
   
$this->words = array_count_values ($this->words);
   
arsort ($this->words);
    }

   
/**
     * Returns elements from $a to $b
     *
     * @param $a Int
     * @param $b Int
     * @return Array|false
     */
   
public function get_limit ($a, $b)
    {
        if ((int)
$b==="0")
        {
            return
false;
        }

        return
array_slice ($this->words, (int) $a, $b);
    }

   
/**
     * Returns elements frequency > $a
     *
     * @param $a Int Frequency
     * @return Array
     */
   
public function get_count_limit ($a)
    {
   
$a = (int) $a;
        foreach (
$this->words as $key=>$value)
        {
            if (
$value >= $a)
            {
           
$temp[$key] = $value;
            }
            else
            {
                break;
            }
        }

        return
$temp;
    }

   
/**
     * Returns all words
     *
     * @return Array
     */
   
public function get_all ()
    {
        return
$this->words;
    }
}