tomash Posted April 29, 2007 Share Posted April 29, 2007 Sorry for the huge post - big problem! Hi guys. I'm trying to port a Python program to PHP which counts the number of words in a file - sounds simple? But it's not - it's counting a file in a programming language called RSL, which is to program robots. In this counting program, it needs to omit lines from counting where the line contains a certain command(s), and not count words after a comment indicator. Also, other characters count as words, but some don't. The Python program is quite simple, but uses some annoyingly useful built in Pyton features that I really need, that in PHP is much more complex! The idea is this: 1. A user writes a robot 2. He/she needs the number of words counted to see if it can be entered into a competition 3. He/she copy and pastes the robot into a form on a webpage 4. PHP counts the (correct) words and outputs it to the page I don't know how to do this. I have tried, and failed. Here is the code which I was working on when I gave up: <?php $wordChars = '{}()[]+-*/%&^|=<>!\\'; $a_wordChars = array ( "{","}","(",")","[","]","+","-","*","/","%","&","^","|","=","<",">","!" ); $stopWords = array ('name', 'author', 'version', 'style'); $freeWords = array ('and', 'or', 'endif', 'endw'); $robot = $_POST['textarea']; $code = explode("\n",$robot); $linecnt = 0; $t_wordcnt = 0; $wordcnt = 0; foreach($code as $line_num => $wline) { $line = trim($wline); if(startsWith($line,"/") == 1 or startsWith($line,"#") == 1) continue; $t_wordcnt = str_word_count($line,0,$wordChars); //echo($line . " - " . $t_wordcnt . "<br />"); } function startsWith($line, $del) { $chars = str_split($line); $f_char = $chars[0]; if($f_char == $del) return 1; else return 0; } ?> <form class="countForm" action="index.php" method="POST"> <p> <textarea name="textarea" cols="100" rows="30" wrap="off"></textarea> </p> <p> <input type="submit" name="Count" value="Count" /> </p> </form> I did get a bit further, but I deleted it when it got too complicated! IMPORTANT!: Things it should and shouldn't count: The $freeWords array is an array of words which shouldn't be counted. The $stopWords array is an array of words which indicate the rest of the line shouldn't be counted. Anything inside a "..." string (in the robot) should be counted as one word The $wordChars array is an array of characters which count as words I thinks thats everything. Obviously I dont want to put anyone to the trouble of coding the whole thing, but some suggestions as to possible routes to solutions would be nice! Just ask for more info! Thanks in advance and sorry for the massive post, Tom EDIT: You can wee what it does at the moment at botcount.qubesite.co.uk Quote Link to comment https://forums.phpfreaks.com/topic/49185-complex-word-counting-python-php/ Share on other sites More sharing options...
trq Posted April 29, 2007 Share Posted April 29, 2007 I know a bit of Python, can we see the Python code? Quote Link to comment https://forums.phpfreaks.com/topic/49185-complex-word-counting-python-php/#findComment-241012 Share on other sites More sharing options...
tomash Posted April 29, 2007 Author Share Posted April 29, 2007 Sure: def BotWords (code, verbose = False, wordChars = '{}()[]+-*/%&^|=<>!\xaa\xa5\xa4\xa3\x7c\x26', stopWords = ['name', 'author', 'version', 'style'], freeWords = ['and', 'or', 'endif', 'endw']): if type (code) == type (' '): botFile = open (code, 'r') code = botFile.readlines () botFile.close () wordTbl = string.maketrans (wordChars, ' ' * len (wordChars)) wordCnt = 0 for line in code: line = line.split ('#')[0] line = line.split ('//')[0] line = line.translate (wordTbl) line = line.split () lineCnt = 0 inString = 0 finalWords = [] for word in line: if word.lower () in stopWords: break elif word.endswith ('"'): lineCnt += 1 inString = 0 finalWords.append (word) elif word.startswith ('"'): inString = 1 elif not inString and word and word.lower () not in freeWords: lineCnt += 1 finalWords.append (word) wordCnt += lineCnt if verbose and finalWords: print (lineCnt, finalWords) return wordCnt Quote Link to comment https://forums.phpfreaks.com/topic/49185-complex-word-counting-python-php/#findComment-241046 Share on other sites More sharing options...
trq Posted April 29, 2007 Share Posted April 29, 2007 Man, I havent the time at the moment (I'm at work) though Id'e love to take a look at this when I get home. Ive never looked at the maketrans or translate methods, looks interesting. Quote Link to comment https://forums.phpfreaks.com/topic/49185-complex-word-counting-python-php/#findComment-241078 Share on other sites More sharing options...
tomash Posted April 29, 2007 Author Share Posted April 29, 2007 Just wondering, why has this been moved? It's not about the Python - it's incidental that the original is in Python. I just want to know how to do it in PHP? I don't mind, I just think it's odd. Quote Link to comment https://forums.phpfreaks.com/topic/49185-complex-word-counting-python-php/#findComment-241082 Share on other sites More sharing options...
tomash Posted April 30, 2007 Author Share Posted April 30, 2007 Sorry ignore that, I got a message! But any help? Quote Link to comment https://forums.phpfreaks.com/topic/49185-complex-word-counting-python-php/#findComment-241732 Share on other sites More sharing options...
Nameless12 Posted May 3, 2007 Share Posted May 3, 2007 edit-delete: didnt read your question properly Quote Link to comment https://forums.phpfreaks.com/topic/49185-complex-word-counting-python-php/#findComment-244442 Share on other sites More sharing options...
GingerRobot Posted June 17, 2007 Share Posted June 17, 2007 Doesn't sound like an impossible task. Before i have a go though, i wonder - is this problem still open? I see you made the thread in April. If it is, could you perhaps show an example of the robot code. Might make it a touch easier to solve Quote Link to comment https://forums.phpfreaks.com/topic/49185-complex-word-counting-python-php/#findComment-276292 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.