Apologies for getting away from J programming in this snippet.
I've just started having a look at this.
I agree with Devon's point about the cumulative distribution.
Looking at the choices between triples, pairs and singletons,
it seems to me that once you've seeded your text with a
randomly selected triple, there's no need for pairs or
singletons, since you're always appending known triples,
which will always be found in "ternsyl" .
If you run this text I think you'll find that the pair and solo
messages are never shown.
Generate =: 3 : 0
NB. adapted from your loopy code...
:
NB. Text =. terns {~ ?10000
Text =. y
for. i.x do.
tern =. ternsyl i. _3{. Text
pair =. pairsyl i. _2{. Text
if. tern < #ternsyl do.
Text =. Text, ternsyl {~ Next tern { terns
else.
if. pair < #pairsyl do.
smoutput 'pair';_3{.Text
Text =. Text, pairsyl {~ Next pair { pairs
else.
smoutput 'solo';_3{.Text
Text=. Text, Next ({:text){ solos
end.
end.
end.
)
I realise that this is not dealing with your loopless code
query, but loopy code does have it's exploratory uses
Good luck
Mike
Devon McCormick wrote:
Kairit -
I just noticed that, for better efficiency, you might want to define your
tables as cumulative sums in the first place, e.g.
solos =: +/\"1 ?.10 10 $ 100
since the "next" function calculates the cumulative sum each time you call
it. This way, you only do this (somewhat expensive) task once. This would
allow you to substitute the simpler function
nextcum =: 13 : '0 i.~(?{:y)>y' " 1
for "next".
Also, upon examining this simpler expression, notice
we can be sure we'll never "run off the end" since "?N" gives a value from
zero to N-1 so the last element of "(?{:y)>y" will always be zero, so
"nextcum" will never give an index out of range.
I find that being able to do this sort of static analysis is one
benefit of array-oriented thinking.
On 1/20/08, Kairit Sirts <[EMAIL PROTECTED]> wrote:
So here's what to do:
1. First, describe the code's purpose. Tell us, in a
sentence or two, what it's supposed to do (i.e. what
the goal of the program is).
2. Second, take the code you want to refactor.
a. Make sure it runs properly in J.
b. Ensure the example is stand alone. That is,
if we type exactly what you post into J, we get
exactly the same results as you. (So, for
example, if you depend upon a global name, be
sure to include the lines that define that
global name).
c. Include a couple of examples of its use (i.e.
give us example arguments).
3. Third, ensure that your mail client doesn't intrude
again. Right before you hit "send":
a. Highlight the code in the email.
b. Copy it.
c. Start a new, fresh J, without any custom
profile.
d. Open a new, fresh script window (IJS).
e. Paste the code.
f. Run the window.
g. Confirm you see the desired results.
All this is kind of a hassle, but it does make it easier for us to
understand your code and help with your problems.
-Dan
Thanks a lot for helping me. The problem was with evaluating the condition
and I was able to solve it. I have almost refactored the code, now I have
to
put it into loop. But I'll do what You suggest.
The code is used for generating a pseudo language. I have three syllable
frequency matrixes for this purpose. First matrix describes the
frequencies
between syllables, eg how many times 'ma' is followed by 'ny'. The second
matrix contains the frequencies between syllable pairs and syllables, eg
how
many times 'ge','ne' is followed by 'ra'. And the third matrix contains
the
frequencies between syllable triples and syllables, eg how many times
'ge',
'ne', 'ra' is followed by 'te'.
The task is to generate language syllable after syllable, using triple
matrix as much as possible. If this is not possible (because there are
huge
amount of triples and only 10000 of them are involved) then use couple
matrix and if this is also impossible then use syllable matrix. For
testing
the code, randomly generated smaller matrixes can be used.
NB. Global variables
NB. Frequency matrixes
solos =: ?.10 10 $ 100
pairs =: ?.15 10 $ 100
terns =: ?.20 10 $ 100
NB. Pairs and terns corresponding to frequency matrixes rows
pairsyl =: ?.15 2 $ 10
ternsyl =: ?.20 3 $ 10
NB. Verb for generating the next syllable using the probability, argument
is
a list
next =: 13 : '0 i.~(?+/y)>+/\y'
NB. Verb for making the argument for the next generation step from the
already generated text, containing index of triple, index of pair and
index
of syllable
assign =: 13 : '((ternsyl i. _3 {. y), (pairsyl i. _2 {. y), {:y)'
NB. Different scenarios for next generation, including selecting the
necessary part from the argument
gen1 =: next@({&terns)@(0&{)
gen2 =: next@({&pairs)@(1&{)
gen3 =: next@({&solos)@(2&{)
NB. Condition for selecting the correct scenario
n =: 20 15 10
cond =: {.@(I.@(-.@(=&n)))
gen =: [EMAIL PROTECTED]
NB. First a triple will be selected randomly
text =: ternsyl {~?20
NB. Then create argument and generate next syllable
text =: text, gen assign text
This last sentence must be done as many times as the user says. How to put
the self assignment into loop?
Kairit Sirts
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm