summaryrefslogtreecommitdiffstats
path: root/ktouch/extras/training-gen/perl/ktouch-gen.pl
blob: 901df6e0a4e063c5f3ad56b2e6cde061847e623d (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/perl -w --strict

# default values
$length_of_line = 60;
$number_of_line = 30;


# genword( accumulated, core )
# This function will generate a random sequens of character
# containing only characters from accum or core and all words
# generated will contain at lest on character from core
sub genword
{
    (my $accum, my $core) = @_;

    my $all=$accum.$core;
    my $res="";

    for(my $i=0;$i<=rand(5);$i++)
    {
	my $ran_pos=int(rand(length($all)));
	$res=$res.substr($all,$ran_pos,1);
    }

    # check if we have generated a walid word, containig some characters from $core
    if ($res =~ m/[$core]/)
    {
	return $res;
    }
    else
    {
	return genword($accum,$core);
    }
}

# This function will return a list of words containing only characters from
# accum or core. And all words will contain at lest on echaracter from core.
# If we can't find enough words, we will use genword to generate enough words
# to fill the list.
sub genlist
{
    ($accum, $core) = @_;
    my @res;
 
    my $all=$accum.$core;


    #print "$core\n";

    foreach(@word_list)
    {
	chomp($_);
	if (m/[$core]/ && m/^[($all)][$all]*$/)
	{
	    push @res,$_;
	}
    }

    for(my $i=@res;$i<30;$i++)
    {
	push @res,genword($accum,$core);
    }

    return @res;
}


# Genlevel will generate a level
sub genlevel
{
    ($accum, $core) = @_;
    my $res = "";
    my @list = genlist($accum,$core);
    my $max_lines = $number_of_line;
    my $max_length = $length_of_line;
    while($max_lines >0)
    {
	my $tmp=$list[rand(@list)-1];
	$res = $res.$tmp; # first word on line should not have space 
	while($max_length >0)
	{
	    my $tmp=$list[rand(@list)-1];
	    $res = $res." ".$tmp; 
	    $max_length = $max_length - (length($tmp) + 1); # +1 is for counting one extra space for each word
	}
	$res = $res."\n";
	$max_length = $length_of_line;
	$max_lines = $max_lines - 1;
    }	
    return $res;
}

sub rrr
{
    print ".";
    s/\\/\\\\/g; #remove escape character...
    s/-/\\-/g; #remove any - since this will mean range
    s/ //g;
    return $_;
}

sub heading
{
    return
	"######################################################################\n".
	"##\n".
	"# KTouch training file generated ".localtime(time())."\n".
	"#\n".
	"# Perl Script written by Steinar Theigmann & Håvard Frøiland.\n".
	"#\n";
}

sub usage
{
    return
	"\n".
	"usage: ktouch-gen config_file\n".
	"   Example: ./ktouch-gen english-conf < english-word-file\n".
	"\n";
}

# --------------------- START ----------------------------

if(@ARGV == 0) # exit if there is no config_file specified
{
    die usage;
}

open(CONFIG,$ARGV[0]) # First argument should be config file
    or die "\ nCan't find config_file: $ARGV[0]\n\n";

# Read in all elements in config file
while (<CONFIG>)
{
    chomp($_);

    if(s/length\-of\-line//)
    {
	$length_of_line = $_;
    }
    elsif(s/number\-of\-line//)
    {
	$number_of_line = $_;
    }
    elsif($_) # Add to config if not empty
    {
	push @config,$_;
    }
}

#foreach(@config)
#{
#    print "$_\n";
#}
#exit 0;

# Read in all words
while (<STDIN>)
{
    chomp($_);
    if(length($_)>0)
    {
	push @word_list, $_;
    }
}

print heading;

$accum="";
$count=0;
foreach(@config)
{
    $count++;
    print "# Level $count\n";
    print "$_\n";
    print genlevel($accum,$_);
    $accum=$accum.$_;
    print "\n\n";
}