Create array from word list?

Get help using and writing Nisus Writer Pro macros.
Post Reply
B.Otter
Posts: 27
Joined: 2021-02-06 15:24:00

Create array from word list?

Post by B.Otter »

Greetings,

I have a text document with a word list, one word per line. I'm looking for recommendations for macro text to create an array with each word as a separate element of an array.

Thanks,
Brad
B.Otter
Posts: 27
Joined: 2021-02-06 15:24:00

Re: Create array from word list?

Post by B.Otter »

Ok, I've worked out at least one possibly tedious solution, which is to search each word from top to bottom of document and build array, one wrod at a time. I'm sure there's a cleaner, maybe faster, solution.

I can then randomize the array. Ok.

What I then want to do is to either replace the existing word order with the randomized word order, or create a new document and fill it with the randomized array as one word per line.

Any thoughts?

Thanks.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Create array from word list?

Post by phspaelti »

Code: Select all

$doc = Document.active
$wordSels = $doc.text.find '^\w+', 'Ea'
$words = $wordSels.arrayByMakingValuesDoCommand 'substring'
$words.randomize
Document.newWithText $words.join("\n")
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Create array from word list?

Post by phspaelti »

In way of explanation:

Nisus' strong point is Find. In the macro language a Find statement used on a text object will produce a selection object, or, if you use the "Find All" option, an array of selections. So the first thing is to write the Find statement that you want. I wrote:

Code: Select all

$doc.text.find '^\w+', 'Ea'
Here $doc.text is the text object of the current document, logically enough.
The '^\w+' is PowerFind Pro for "a series of word characters from the beginning of the paragraph". I wrote it that way, because you said there was one word per line/paragraph. Note that '\w' will match "word characters" which are basically alphabetic symbols but also include numbers.
The 'Ea' option tells Nisus to use PowerFind Pro ('E'), and to do a "Find all' ('a').

Note that the result of this command will be an array of TextSelection objects. You can get the actual selected text from such an object using the property 'substring'. And you can do this for a whole array of such objects using the command with the elegant :P name 'arrayByMakingValuesDoCommand'. This command creates another array in one swell foop, as they say.

After shuffling, you can join the values in the array back together into one text object, using an appropriate separator. Using the newline ("\n") will give you one word per line.
philip
B.Otter
Posts: 27
Joined: 2021-02-06 15:24:00

Re: Create array from word list?

Post by B.Otter »

Thank you, Philip. Simple and elegant. And really fast! Again, going to school on you code. Up late?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Create array from word list?

Post by phspaelti »

B.Otter wrote: 2021-04-04 07:45:39 Up late?
So it would seem… Happens too often
philip
User avatar
Hamid
Posts: 777
Joined: 2007-01-17 03:25:42

Re: Create array from word list?

Post by Hamid »

Just a question: Could this have been achieved by using the Edit:Transform Paragraphs:Randomize (shuffle) command?
B.Otter
Posts: 27
Joined: 2021-02-06 15:24:00

Re: Create array from word list?

Post by B.Otter »

Hamid wrote: 2021-04-04 08:34:14 Just a question: Could this have been achieved by using the Edit:Transform Paragraphs:Randomize (shuffle) command?
Yup, turns out you sure can. And it produces a different random order with iterations from the original text. Thank you for pointing this out.

BUT, Philip picked up on where I was at and showed the way to approach the text as a macro--especially helpful. Learning an object oriented language from the ground up.

With Philip's example and explanations, and with the macro code from the "Concordance List," I've been able to build and Philip's example and write a more general, paragraphed-based text document routine to extract a unique word list. A helpful exercise in extending the code.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Create array from word list?

Post by martin »

phspaelti wrote: 2021-04-04 06:57:43Note that the result of this command will be an array of TextSelection objects. You can get the actual selected text from such an object using the property 'substring'. And you can do this for a whole array of such objects using the command with the elegant :P name 'arrayByMakingValuesDoCommand'. This command creates another array in one swell foop, as they say.
The verbose name is somewhat clunky, sorry! But at least it's still shorter than a full "For" loop that accomplishes the same thing. I'll include it here for completeness:

Code: Select all

$words = Array.new
ForEach $wordSel in $wordSels
	$words.appendValue($wordSel.substring)
End
Sometimes it's nice to see the whole thing written out explicitly.

I think ideally it would be nice if Nisus Writer's macro language supported anonymous functions (aka lambdas). Then we could just borrow the "map" operator from functional programming. This hypothetical dream code would be nice and neat, something like:

$words = $wordSels.map( function ($wordSel) => {return $wordSel.substring} )

But I don't know if many Nisus macro authors would want to use of this kind of advanced programming technique.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Create array from word list?

Post by phspaelti »

I was just kidding. .arrayByMakingValuesDoCommand is my favorite macro language feature and I go out of my way to use it whenever I can :D

I think the current Nisus macro language is a very good balance between powerful and still manageable for someone who doesn't want to become a full-time programmer. I compare it to say Visual Basic. After 30+ years of trying, I still can't make heads or tails of that one. And I was completely bowled over, when after spending a considerable amount of time figuring out how to get the data from my spreadsheet into an array, I found that VB has no way to sort an array!! Compare that to Nisus macro language:

Code: Select all

$array.sort
8)
Why is it called "Visual Basic"? Because it's neither visual nor basic :P
philip
B.Otter
Posts: 27
Joined: 2021-02-06 15:24:00

Re: Create array from word list?

Post by B.Otter »

Martin and Philip,

Thank you for stepping in with the help with my learning curve managing and manipulating objects. Delighted to see how fast the .appendValue builds an array when moving through a single body of .text.find.

A foundation and a springboard!

Best
Vanceone
Posts: 211
Joined: 2013-05-03 07:06:31

Re: Create array from word list?

Post by Vanceone »

I'd love a version of Map, Reduce, and a few other functional programming concepts! And Lambdas. Recursion? Maybe not... a for loop should do most of that.
Post Reply