03-14-2022 09:11 AM
Hello, I have googled and searched the forum, but I can't find anything to get my code right. I want to search a .txt file and see how many times a keyword appears on demand. I've tried using a loop with the match pattern function, but I'm not getting the correct results. I appreciate any help given.
Solved! Go to Solution.
03-14-2022 09:29 AM - edited 03-14-2022 09:31 AM
You need to wire up the shift register to the "start index" input on the Match Pattern.
Personally, I would just use the Search And Replace String, setting the "replace all" to TRUE and then just use the "Number Of Replacements" output.
03-14-2022 09:33 AM - edited 03-14-2022 09:54 AM
Try this:
Honestly, crossrulz solution above is much better.
03-14-2022 10:10 AM
One unpleasant complication is figuring out what delimits a word.
03-14-2022 10:16 AM
@paul_cardinale wrote:
One unpleasant complication is figuring out what delimits a word.
Irrelevant, the program searches for an exact match and returns how many time that exact match was found.
03-14-2022 10:19 AM - edited 03-14-2022 10:30 AM
@paul_cardinale wrote:
One unpleasant complication is figuring out what delimits a word.
I agree that the problem is much more complicated, because there are plenty of delimiters (space, tab, period, linefeed, double-quote, numbers, etc.) so it is important to only use consecutive strings from the correct lexical class. Just searching for a substring will give you plenty of false positives (e.g. searching for "trust" would also count the substring in "trustworthy" as a "word", etc.). You also need to ignore upper/lowercase.
Still, this is a simple homework problem to solve. Try it!
Some comments to the original code:
03-14-2022 10:21 AM
@RTSLVU wrote:
@paul_cardinale wrote:
One unpleasant complication is figuring out what delimits a word.
Irrelevant, the program searches for an exact match and returns how many time that exact match was found.
Certainly not irrelevant. Suppose the text contains " forum ", and you search for the word "rum" without regard for delimiters; you would get a match there in "forum". But that is not the word "rum" and shouldn't be counted as such.
03-14-2022 10:23 AM
@altenbach wrote:
@paul_cardinale wrote:
One unpleasant complication is figuring out what delimits a word.
I agree that the problem is much more complicated, because there are plenty of delimiters (space, tab, period, linefeed, double-quote, numbers, etc.) so it is important to only use consecutive strings from the correct lexical class. Just searching for a substring will give you plenty of false positives (e.g. searching for "trust" would also count the substring in "trustworthy" as a "word", etc.).
Still, this is a simple homework problem to solve. Try it!
And you have to make sure that words at the beginning and end don't get missed for lack of a delimiter on one side.
03-14-2022 10:25 AM
@paul_cardinale wrote:
@RTSLVU wrote:
@paul_cardinale wrote:
One unpleasant complication is figuring out what delimits a word.
Irrelevant, the program searches for an exact match and returns how many time that exact match was found.
Certainly not irrelevant. Suppose the text contains " forum ", and you search for the word "rum" without regard for delimiters; you would get a match there in "forum". But that is not the word "rum" and shouldn't be counted as such.
Oh, good point... I hadn't thought about it that much.
03-14-2022 11:07 AM - edited 03-14-2022 11:22 AM
See if this can give you some ideas.
It would be even easier to place all words and their count into a map (try it!).
NOTE that you should set the keyword control to "limit to single line", else a linefeed might sneak in. You might actually want to verify that only a single word is entered (not shown)!