Typing tutor help script needed, please

T

Throw

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)
 
S

SPE - Stani's Python Editor

#---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,ignoreCase=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()
 
M

Mike Meyer

Throw said:
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
 
A

Anno Siegel

Throw said:
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
 
R

RedGrittyBrick

#---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,ignoreCase=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()

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top