Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
any one help me to convert this code to php

Posted by themster (themster), 15 April 2007
this code make a spell check to a word and return the correct word  but its in python

can any one help to convert it in php since i have no idea in python...!!!

import re, collections

def words(text): return re.findall('[a-z]+', text.lower())  

def train(features):    
   model = collections.defaultdict(lambda: 1)    
    for f in features:    
            model[f] += 1    
    return model
NWORDS = train(words(file('big.txt').read()))
alphabet = 'abcdefghijklmnopqrstuvwxyz'

def edits1(word):    
       n = len(word)    
       return set(
                      [word[0:i]+word[i+1:] for i in range(n)]   # deletion  
                      [word[0:i]+word[i+1]+word[i]+word[i+2:] for i in range(n-1)] + # transposition      
                      [word[0:i]+c+word[i+1:] for i in range(n) for c in alphabet] + # alteration    
                      [word[0:i]+c+word[i:] for i in range(n+1) for c in alphabet])  # insertion

def known_edits2(word):    
   return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)

def known(words):  
return set(w for w in words if w in NWORDS)

def correct(word):    
                 candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]    
                 return max(candidates, key=lambda w: NWORDS[w])

and thanks for help

Posted by admin (Graham Ellis), 15 April 2007
That's a series of function definitions rather than a main program (no, wait, I see some main code mixed in!) ... and I fear that some of the spacing that's vital to Python has been lost or damaged. Can you give us an example of how it runs, anc check the spacing?

Posted by themster (themster), 16 April 2007
oh yes.. sorry i messd some thing;
i have this code by searching for some thing that make some spell checking by php without no addons.
and finally i found this code but in python so i ask for help from any one who have any idea about python
so any help is welcome...
-- the use of this code as follow:
>>> correct('speling')
>>> correct('korrecter')


thats all
and thanks for help......
and sorry for any miss understanding i think we are all here for help..

Posted by admin (Graham Ellis), 19 April 2007
If you're not familiar with Python but know PHP, perhaps the following resources would be helpful:

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho