Search paragraph against a list of words and output display words if found

Hi. I'm tying to create a macro to search a paragraph of text for certain words and then take an action if the words are found.

For example: My trigger words are "Red", "Blue", and "Green".

My Paragraph is: John owns a Blue Sedan, A Purple Sedan, and a Red Sedan. John is has many Sedans.

My Action: Display "One of Johns Sedan's is Blue."

I understand how to copy clipboards, and set them to a variable but I don't know how to turn it into a string and filter it against my word list. I

Any help would be appreciated. Thanks.

It is easy enough to find all of your trigger words (I would call them keywords) in your source paragraph. However, your output of:

One of Johns Sedan's is Blue.

is very specific, and not exactly contained in your source paragraph.

So, are you asking for an output that is a fixed phrase + trigger word:

One of Johns Sedan's is <trigger word>.

If not, please provide real-world examples of both:

  • Source text
  • Keyword list to search for
  • Resultant Output

Please put each of these in a Code Block so that all of the source characters and spacing is preserved.

Also, for future posts, please read:
Tip: How Do I Get The Best Answer in the Shortest Time?

So, are you asking for an output that is a fixed phrase + trigger word:
One of Johns Sedan's is <trigger word> .

Yes Exactly. I want an output that is a fixed phrase + trigger word or words. My biggest issues is how to get KM to search through my document to find keywords based on a dictionary I give it.

I already have an AppleScript that I use in Automator. I'm trying to translate it to KM as KM is far superior and not use Automator at all. I figure I can just drop this script in KM, but I dont know how to pass the clipboard into the script. In Automator, whatever I highlight is automatically passed into the script once I activate it.

Here is my Script:

on run {input, parameters}
	set theDocument to input
	set Dictionary1 to {"Red", "Green", "Blue"}
	set Dictionary2 to {"Apples", "Oranges", "Duck"}
	set Dictionary3 to {"John", "Mary", "Joe"}
	set WordFilter to Dictionary1 & Dictionary2 & Dictionary3
	set ResultList to containsItems from WordFilter against theDocument
	return input
end run

on containsItems from qList against theText
	set foundWords to {}
	set positiveHit to false
	repeat with aText in theText
		repeat with aWord in qList
			if every word of aText contains aWord then set end of foundWords to contents of aWord & " "
			if every word of aText contains aWord then set positiveHit to true
		end repeat
	end repeat
	
	if positiveHit then
		say "Warning! I Found a positive hit"
		display dialog "The Document contains the following words: " & foundWords
	else
		say "This Document does not contain any of the words listed in the dictionary."
	end if
	return foundWords
end containsItems

If I gave this script the following input:

Hello everyone. The Cat named Clyde has a Yellow Mustang. However the Duck named John has a Red Hummer. This is just an example of a random paragraph.

The Script would output the following:

"Warning! I found a positive hit."
The Document contains the following words: Red Duck John

You might be interested in the Concordance feature of my Literary Toolbox.

It prompts for your trigger words (which you would enter as the regex "Red|Blue|Green") and displays the results in context:

ss-244

OK, this can definitely be done in KM.
Here's an example macro to get you started.
You will need to modify, or use as an example, to develop your own macro.

Let us know if this works for you.

==UPDATED==: 2020-01-25 22:08 GMT-6
See my below post for a small update to ensure word boundaries.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Build Statement Based on Keywords in Source Text [Example]

**Requires: KM 8.2.4+&nbsp;&nbsp;&nbsp;macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/6/c/6c2317adad964948001a583fc7c0ed1d22b17fc6.kmmacros">Build Statement Based on Keywords in Source Text [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

### Example Output
Version 1
![image|395x186](upload://ilwLd1GaYXoyf923My6xbbWTAM7.png) 

Note:  This differs slightly from your desired output:

[quote="Nomad76, post:1, topic:16740"]
My Action: Display "One of Johns Sedan's is Blue."
[/quote]

But there are two keywords in the Source String:  "Red" and "Blue"
So it is not clear how you want to choose which KW to use if there are multiple KWs found.  For now, I just took the easy solution of using the first KW found.

`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`


==**UPDATED**==:  2020-01-22 20:20 GMT-6

Taking another look at your OP, I decided to give you a 2nd version that would output all keywords found in the Source String.


`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

### MACRO:&nbsp;&nbsp;&nbsp;Build Statement Based on Keywords in Source Text [Example]

~~~ VER: 2.0&nbsp;&nbsp;&nbsp;&nbsp;2020-01-22 ~~~
**Requires: KM 8.2.4+&nbsp;&nbsp;&nbsp;macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/d/6/d662781a4c7dcf29cdb3c134d37f1dfdcead494a.kmmacros">Build Statement Based on Keywords in Source Text [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### Example Output
Version 2
![image|395x186](upload://4zE46fBvoReF9IFQUTp9mtealci.png) 


---

### ReleaseNotes

Author.@JMichaelTX 

**PURPOSE:**

* **Build Statement Based on Keywords in Source Text**

**HOW TO USE**

1. First, make sure you have followed instructions in the _Macro Setup_ below.
2. Trigger this macro.

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  ??
.

**Make These Changes to this Macro**
1. Assign a Trigger to this macro.
2. Move this macro to a Macro Group that is only Active when you need this Macro.
3. ENABLE this Macro, and the Macro Group it is in.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
   * SET Source String 
   * SET Keywords 
   * SET Output Phrase 

**REQUIRES:**

1. **KM 9.0+** (may work in KM 8.2+ in some cases)
2. **macOS 10.11.6 (El Capitan)+**

TAGS:  @Lookup @Keywords @Example

**==USE AT YOUR OWN RISK==**

* While I have given this a modest amount of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

<img src="/uploads/default/original/3X/e/2/e245bef752814745094fa54ad7cb4c4e9ceb7259.png" width="634" height="2159">

Wow. This is a nice tool. Thanks!

Hi JMichaelTX, I have read your many posts and your contributions are invaluable.
This macro works perfectly. The 2nd Version does exactly what I need. Even the formatting details like adding the commas and cleaning up the last one by removing the comma and adding a period are impeccable.

While playing with it, I noticed that the "For Each" Loop was set to check "Each Line" in the Variable.


That is a nice feature because it allows you to insert phrases in the Key word list and check for whole phrases.

So thank you very much. This is a great study and helped me learn more about how KM works!

1 Like

After playing with it more. I discovered that the macro doesn't not distinguish between whole words and parts of words. If it finds a match, then it pulls out parts of words regardless of context.

In my apple script you could ask it to parse whole words.
06%20AM

Is there a way to do this with KM? I went through all of the conditional options and only two worked "Matches" and "Contains". But they would not do whole words.

To see this, goto the following loop and remove the "Sedan" qualifier.
03%20AM

Then I get the following results based on the given input:


It picked out the word "Red" form "Blurred" and "Blue" from "Blueberry".

Now if I try changing the condition to "Variable Matches"... then It only distinguishes based on Uppercase / Lowecase. But it will not consider the whole word.
20%20AM

But otherwise the solution is great.

Thanks again.

Easy fix.
Replace the IF/THEN in the macro with this one:

image

Two changes to the IF/THEN:

  1. Change "contains" to "matches"
  2. Change the match field to:
    (?i)\b%Local__KW% Sedan\b

This uses the Regex metacharacter \b to ensure the enclosed string in on a word boundary.
The (?i) makes the comparison case insensitive. Remove it if you want a case sensitive match.

Questions?

This works perfect!

Also, thank you for explaining the Regex syntax to me. <--- You made me aware that there is something else I need to study. Now to search the forum to learn more about Regex and KM :slight_smile:

1 Like

Regular expressions (RegEx or RegExp) are extremely powerful, but have an initial steep learning curve that is often intimidating. But once you get over that initial hump, and you continue to write new RegExp, it will become much easier.

I do all of my RegEx development at this free website:

You may also find these sites helpful:

1 Like