Random word generator

Hi,

This is just to share some code that Christian Grunenberg helped me with. So not really a question.

In March this year Christian was kind enough to share the code below (Code 1.) for generating x number of random words from a source text in DevonThink Pro that I had written. The source text was usually about 500 words long, so in order to avoid repeats I was deleting the random words generated by the code in the original text. A time consuming process.

Code 2. is Christian’s code for both generating the random words and deleting them from the original source text.

Thank you again Christian.

Best regards,

Chris.

Code 1.
Here’s a simple example. Just select the documents and the script will
return n random words (see property pWords):

property pWords : 10

tell application “DEVONthink Pro”
set theRecords to the selection
set numRecords to count of theRecords
set num to random number from 1 to numRecords
set theText to plain text of (item num of theRecords)
set numWords to count of words of theText

set theResult to “”
repeat with i from 1 to pWords
if i > 1 then set theResult to theResult & " "
set theResult to theResult & (word (random number from 1 to numWords)
of theText)
end repeat

return theResult
end tell

Code 2.
The new code that deletes the random words from the original text:

property pWords : 25

tell application “DEVONthink Pro”
set theResult to “”
try
set theRecords to the selection
set numRecords to count of theRecords
set num to random number from 1 to numRecords
set theText to plain text of (item num of theRecords)
set numWords to count of words of theText

	set theWords to {}
	repeat with i from 1 to pWords
		if i > 1 then set theResult to theResult & " "
		set theWord to word (random number from 1 to numWords) of theText
		set theResult to theResult & theWord
		set theWords to theWords & theWord
	end repeat
	
	set theWindow to missing value
	show progress indicator "Deleting Words" steps (count of theRecords)
	repeat with theRecord in theRecords
		set theText to plain text of theRecord
		if theWindow is missing value then
			set theWindow to open window for record theRecord
		else
			set record of theWindow to theRecord
		end if
		repeat with theWord in theWords
			if theText contains theWord then tell text of theWindow to set (every word where it is theWord) to ""
		end repeat
		tell theWindow to save
		step progress indicator (name of theRecord as string)
	end repeat
	hide progress indicator
	
	tell theWindow to close
on error
	hide progress indicator
	return ""
end try
return theResult

end tell

So … I’m curious. Why would you want to extract random words from a text and delete them from the original? :confused:

(A suggestion = if you select all of the text in each script in your posting and click the “Code” button on the forum’s edit bar, you’ll surround the script with markup that will make it easier for readers to copy the script from your forum posting.)

Hi Korm,

Thanks for the suggestion. I’m not used to this. First I’ll give you the code then the reasons for the process.

I can’t see any change in the code text though.

Best,

Chris.


Hi,

This is just to share some code that Christian Grunenberg helped me with. So not really a question.

In March this year Christian was kind enough to share the code below (Code 1.) for generating x number of random words from a source text in DevonThink Pro that I had written. The source text was usually about 500 words long, so in order to avoid repeats I was deleting the random words generated by the code in the original text. A time consuming process.

Code 2. is Christian’s code for both generating the random words and deleting them from the original source text.

Thank you again Christian.

Best regards,

Chris.

Code 1.
Here’s a simple example. Just select the documents and the script will
return n random words (see property pWords):

property pWords : 10

tell application "DEVONthink Pro"
set theRecords to the selection
set numRecords to count of theRecords
set num to random number from 1 to numRecords
set theText to plain text of (item num of theRecords)
set numWords to count of words of theText

set theResult to ""
repeat with i from 1 to pWords
if i > 1 then set theResult to theResult & " "
set theResult to theResult & (word (random number from 1 to numWords)
of theText)
end repeat

return theResult
end tell

Code 2.
The new code that deletes the random words from the original text:

property pWords : 25

tell application "DEVONthink Pro"
set theResult to ""
try
set theRecords to the selection
set numRecords to count of theRecords
set num to random number from 1 to numRecords
set theText to plain text of (item num of theRecords)
set numWords to count of words of theText

set theWords to {}
repeat with i from 1 to pWords
if i > 1 then set theResult to theResult & " "
set theWord to word (random number from 1 to numWords) of theText
set theResult to theResult & theWord
set theWords to theWords & theWord
end repeat

set theWindow to missing value
show progress indicator "Deleting Words" steps (count of theRecords)
repeat with theRecord in theRecords
set theText to plain text of theRecord
if theWindow is missing value then
set theWindow to open window for record theRecord
else
set record of theWindow to theRecord
end if
repeat with theWord in theWords
if theText contains theWord then tell text of theWindow to set (every word where it is theWord) to ""
end repeat
tell theWindow to save
step progress indicator (name of theRecord as string)
end repeat
hide progress indicator

tell theWindow to close
on error
hide progress indicator
return ""
end try
return theResult
end tell

Hi Korm,

Now that I’ve posted the code “as code” I can see what you mean.

So why delete key words from a source text?

The random word associations that are generated in Devon Think Pro are only part of a longer process.

Basically I use the process to challenge my imagination to expand an original text or sketches of loose associations.The original text is not deleted as such only the proxy in Devon Think Pro.

I’m using the random word associations to write lyrical prose.

The most important factor is that the source document that I use the script with is engendered by me. In other words it has its own themes and their related associations that I can identify with.

I have developed these experiments through research and teaching - mostly design students - over the past 15 years.

The interesting point here being that anyone who works with text could use this.

After generating the 25 words, for example, I then tumble them as a Wordle (Wordle.net). If I am using my iPad I use WordSalad, which does the same.

The continual regrouping of the 25 words in visual relation ( I cannot emphasise this enough for my process) to each other throws up various combinations of meaning.

I then make a series of - about 50 - paragraph bite sized pieces of text - 250 words - generated from each Wordle of 25 words into DevonThink Pro.

By searching for one phrase I am also given the “See Also and Classify” magic hat list of related texts.

I can then let the one paragraph influence the other as to what I then write.

Here is an example of what I do with the process:

The original associations came from a walk on the beach.

The code generated these bytes:

knifing, cutting, slicing, intimately powdering, slender… skywards, kites - bright jewels, skywards, rayfish, wishbone, bright jewels against the…, surgical, precision instrument, slender line, stitching, sewing, the powder of the surf, slender instrument, precision, sliding knife, a harnessed instrument, a slender wishbone, slicing, cutting, skywards, powdering surf, surfer, a jewel powdering the surf, harnessed instrument

This developed into a final text:

Kite surfing

The kite surfer, a precision surgeon harnessed tight to his slender instrument, skims the shimmering of this lucent surface, senses the shifts of its fluid skin. Straddled keen, wishbone curved to his crisp blade, he slices watery incisions, stitches plumed trails into the powdered surf. Skywards, his stretched kite blooms, blushed full, in a brilliant coloured dance with the wind. Strokes, glides, furls intimate with the airstream. Guides, pulls the blade to the next cutting ground, the next incision.

After each pass, each draw of wind and current, all trace of skimming, cutting, needling are washed over to sift down as yet another layer, another filigree of fresh scarring that nestles deeper into the tissue of this ocean’s sand and salt: its crumbled memories.

Best,

Chris.

@chris

Thank you for posting further on how you use the scripts in your work. The technique you described might be interesting to writers and others who use DEVONthink - and so the description accompanying the scripts make them that much more valuable.

@chris: Very, very cool stuff! :smiley:
It’s nice to see DEVONthink being used to facilitate art instead of always administrative functions. (I’m an artist myself, so this touches home with me.) Well played!