By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,714 Members | 750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,714 IT Pros & Developers. It's quick & easy.

Typing tutor help script needed, please

P: n/a

G'day everyone!

I would like to design typing tutor exercises for Afrikaans (and other
languages possibly). This is for a GPL project. For this, I need a
script that can extract words from a long list of words, based on which
letters those words contain, and write then write output to a file.
Does anyone know of an existing script for this, or can anyone write me
one, please?

Preferably I must be able to extract words which contain only certain
letters (they need not contain all of those letters, but they may not
contain any other letters). It would be nice if I can also extract
words which do not contain certain letters.

Any help will be greatly appreciated. I'm on a Windows 2000 machine
with Perl, Python, Tcl/Tk and Java.

Thanks!
Samuel (throw aka voetleuce aka leuce)

Oct 28 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
#---Input data

#List of words; every word or sentence on one line
#If from file: WORDS = open(fileName).readlines()
WORDS = """\
Afrikaans
Anna
Bread
red
word
bored
python""".split('\n')

#---Main program
import re

PATTERN = ['[^(%s)]+','[%s]+']
FILENAME = ['not_%s.txt','%s.txt']

def filter(letters='bdeor',words=WORDS,contain=True,ig noreCase=True):
pattern = PATTERN[contain]%'|'.join(list(letters))
if ignoreCase:
allowed = re.compile(pattern,re.IGNORECASE)
else:
allowed = re.compile(pattern)
result = []
for word in words:
match = allowed.match(word)
if match and match.group(0) == word: result.append(word)
print result
output = open(FILENAME[contain]%letters,'w')
output.write('\n'.join(result))
output.close()

if __name__ == '__main__':
filter()

---------------
This should do it.

Stani
--
http://pythonide.stani.be
http://pythonide.stani.be/manual/html/manual.html

Oct 28 '05 #2

P: n/a
"Throw" <th***@workmail.co.za> writes:
G'day everyone!

I would like to design typing tutor exercises for Afrikaans (and other
languages possibly). This is for a GPL project. For this, I need a
script that can extract words from a long list of words, based on which
letters those words contain, and write then write output to a file.
Does anyone know of an existing script for this, or can anyone write me
one, please?
This sounds nearly trivial in Python. More specifics would help.
Preferably I must be able to extract words which contain only certain
letters (they need not contain all of those letters, but they may not
contain any other letters). It would be nice if I can also extract
words which do not contain certain letters.


Again, this sounds nearly trivial in Python.

For instance. to get a list of all words that don't contain 'a', you'd
do something like:

f = open(textfile)
words = f.read().split()
f.close()
selected = [word for word in words if 'a' not in word]

and then selected has the list of words that don't have an 'a' in
them. Tweaking the test the other way - to select words with an 'a',
remove the 'not'. You can combine the tests with 'and' and 'or'.

But without a UI, that's pretty much useless - and you haven't said
what you want the UI to be.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 28 '05 #3

P: n/a
Throw <th***@workmail.co.za> wrote in comp.lang.perl.misc:

G'day everyone!

I would like to design typing tutor exercises for Afrikaans (and other
languages possibly). This is for a GPL project. For this, I need a
script that can extract words from a long list of words, based on which
letters those words contain, and write then write output to a file.
Does anyone know of an existing script for this, or can anyone write me
one, please?


For the letters a, d, f and g:

perl -ne 'print if /^[adfg]+$/' < /list/of/words > words_with_adfg

For other combinations, change both occurrences of "adfg".

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Oct 28 '05 #4

P: n/a
<reinstated the context that was omitted by SPE>
Throw wrote:
I must be able to extract words which contain only
certain letters (they need not contain all of those letters,
but they may not contain any other letters).

SPE - Stani's Python Editor wrote: #---Input data

#List of words; every word or sentence on one line
#If from file: WORDS = open(fileName).readlines()
WORDS = """\
Afrikaans
Anna
Bread
red
word
bored
python""".split('\n')

#---Main program
import re

PATTERN = ['[^(%s)]+','[%s]+']
FILENAME = ['not_%s.txt','%s.txt']

def filter(letters='bdeor',words=WORDS,contain=True,ig noreCase=True):
pattern = PATTERN[contain]%'|'.join(list(letters))
if ignoreCase:
allowed = re.compile(pattern,re.IGNORECASE)
else:
allowed = re.compile(pattern)
result = []
for word in words:
match = allowed.match(word)
if match and match.group(0) == word: result.append(word)
print result
output = open(FILENAME[contain]%letters,'w')
output.write('\n'.join(result))
output.close()

if __name__ == '__main__':
filter()

---------------
This should do it.


Am I underestimating the task, it looks simple enough for the simplest grep?

# perl -n -e "print if /^[bdeor]+$/i" words.txt
red
bored
Oct 28 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.