File: spell-checker.php

package info (click to toggle)
php-doc 20061001-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 45,764 kB
  • ctags: 1,611
  • sloc: xml: 502,485; php: 7,645; cpp: 500; makefile: 297; perl: 161; sh: 141; awk: 28
file content (167 lines) | stat: -rw-r--r-- 5,316 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2004 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.0 of the PHP license,       |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_0.txt.                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Authors:    Dave Barr <dave@php.net>                                 |
  |                                                                      |
  | Description: This file spell checks the manual. You must have the    |
  |              pspell extension installed for it to work.              |
  |                                                                      |
  | Personal Word List: http://erigol.com/php/custom.pws                 |
  |                     Put it in your home directory, ~                 |
  +----------------------------------------------------------------------+

*/

/* path to phpdoc CVS checkout. if this file is in the scripts/ directory
 * then the value below will be correct!
 */
$phpdoc = '../';

/* english! */
$lang = 'en';

/* (immediate) tags to check for spelling mistakes */
$check_tags = array(    'para',
                        'simpara',
                        'title',
                    );

$element = '';
$current_file = '';
$word_count = 0;

/* prompt a user and return the response */
function read_line($prompt) {
    echo $prompt;
    $in = chop(fgets(STDIN, 1024));
    return $in;
}

/* recursive glob() with a callback function */
function globbetyglob($globber, $userfunc)
{
    foreach (glob("$globber/*") as $file) {
        if (is_dir($file)) {
            globbetyglob($file, $userfunc);
        }
        else {
            call_user_func($userfunc, $file);
        }
    }
}

function set_current_element($xml, $name, $attrs)
{
    global $element;

    $element = strtolower($name);
}

function end_current_element($xml, $name)
{
    return;
}

/* spell check a chunk of data */
function check_data($xml, $data)
{
    global $element, $dict, $check_tags, $current_file, $word_count;

    if (!in_array($element, $check_tags))
        return;

    if (trim($data) == '')
        return;

    $words = preg_split('/\W+/', trim($data));
    if (is_array($words)) {
        foreach ($words as $word) {
            if (trim($word) == '' || is_numeric($word) || preg_match('/[^a-z]/', $word))
                continue;

            $word_count++;
            $word = strtolower($word);

            if (!pspell_check($dict, $word)) {
                /* known bug: due to trim()ing and whitespace removal, the
                 * line number shown here might not match the actual line
                 * number in the file, but it's usually pretty close
                 */
                echo "$current_file:" . xml_get_current_line_number($xml) . ": $word   (in element $element)\n";
                do {
                    $response = read_line("Add this word to personal wordlist? (yes/no/save): ");
                    if ($response[0] == 's') {
                        pspell_save_wordlist($dict);
                        echo "Wordlist saved.\n";
                    }
                } while ($response[0] != 'y' && $response[0] != 'n');

                if ($response[0] == 'y') {
                    pspell_add_to_personal($dict, $word);
                    echo "Added '$word' to personal wordlist.\n";
                }
            }
        }
    }

    return;
}

function check_file($filename)
{
    global $phpdoc, $lang, $current_file;

    if (!fnmatch('*.xml', $filename) || fnmatch("$phpdoc$lang/functions/*", $filename))
        return;

    echo "checking $filename...\n";
    $current_file = $filename;

    $file = file_get_contents($filename);

    if (!$file)
        return;

    $file = preg_replace('/&(.*?);/', '', $file);
    if (trim($file) == '')
        return;

    $xml = xml_parser_create();
    xml_set_element_handler($xml, 'set_current_element', 'end_current_element');
    xml_set_character_data_handler($xml, 'check_data');

    if (!xml_parse($xml, $file, true)) {
        printf("%s: XML error: %s at line %d\n",
            $filename,
            xml_error_string(xml_get_error_code($xml)),
            xml_get_current_line_number($xml)
        );
    }

    xml_parser_free($xml);
}

if (!function_exists('pspell_new_personal')) {
    echo "You must compile PHP with pspell support for this script to work.\n";
    return;
}

$dict = pspell_new_personal('custom.pws', 'en');

globbetyglob("$phpdoc$lang", 'check_file');
pspell_save_wordlist($dict);
echo "Wordlist saved.\n";
echo "Processed $word_count words.\n";
?>