Glad that worked!
Try this...
fs = require('fs'); //file handling library for NodeJS
wordList = fs.readFileSync(__dirname +
'/list.txt').toString().trim().split('\n'); //get the values in "list.txt"
content = ' ' + fs.readFileSync(__dirname + '/content.txt').toString().trim() +
' '; //gets the content
output = []; //initialize output variable
wordList.forEach(thisSearch => { //iterate through your list of words
var thisRegex, thisRslt, thisCount;
thisRegex = new RegExp('[^a-zA-Z]' + thisSearch.toLowerCase() + '[^a-zA-Z]',
'g'); //Set up the search / count
thisRslt = content.toLowerCase().match(thisRegex); // execute the count
thisCount = (thisRslt == null ? 0 : thisRslt.length); // turn "null" results
into zero
console.log(thisSearch, thisCount, thisRslt); //progress outputted to console
output.push(thisSearch + "\t" + thisCount); //add to the output variable
});
fs.writeFileSync(__dirname + '/output.txt', output.join('\n')); //puts the
final result in a file named "output.txt"
> On Jun 29, 2021, at 02:33, Zoe Barnett <[email protected]> wrote:
>
> Hello again Harvey. Thanks to some wonderful resources on the internet, even
> without understanding a thing about node.js I got this working. Thank you!
>
> Is there a way to make it case insensitive? So "Dog", "DOG" and "dog" all get
> counted as dog?
>
> Zoe
>> On Wednesday, 23 June 2021 at 10:08:44 UTC+3 Zoe Barnett wrote:
>> Hello Harvey... Wow. This is very kind of you. Thank you!
>>
>> I hope to take some time this weekend to see if I can make this work. I'll
>> let you know how it goes.
>>
>> Zoe
>>
>>> On Wednesday, 23 June 2021 at 05:22:31 UTC+3 Harvey Pikelberger wrote:
>>>
>>>> On Jun 22, 2021, at 12:59 PM, Zoe Barnett <[email protected]> wrote:
>>>>
>>>> As neither bear nor mouse. I need to count distinct words or exact
>>>> phrases separated by spaces on either side or followed by punctuation like
>>>> , : ; . So
>>>> popular pet,
>>>> (followed by a comma) would get a hit, but
>>>> popular petshops
>>>> wouldn't.
>>>
>>> There are a lot of ways to approach this.
>>> The BBEdit folks are brilliant w/ AppleScript and integrating that with
>>> BBEdit.
>>> There are also ways to approach this using command line which is very fast,
>>> a little cryptic.
>>> Here's a very simple, probably overly simple NodeJS approach, which can be
>>> modified to work in a web browser.
>>> You would no doubt customize this to suit the nuances and particulars of
>>> your workflow.
>>>
>>> fs = require('fs'); //file handling library for NodeJS
>>> wordList = fs.readFileSync(__dirname +
>>> '/list.txt').toString().trim().split('\n'); //get the values in "list.txt"
>>> content = ' ' + fs.readFileSync(__dirname +
>>> '/content.txt').toString().trim() + ' '; //gets the content
>>> output = []; //initialize output variable
>>> wordList.forEach(thisSearch => { //iterate through your list of words
>>> var thisRegex, thisRslt, thisCount;
>>> thisRegex = new RegExp('[^a-zA-Z]' + thisSearch + '[^a-zA-Z]', 'g');
>>> //Set up the search / count
>>> thisRslt = content.match(thisRegex); // execute the count
>>> thisCount = (thisRslt == null ? 0 : thisRslt.length); // turn "null"
>>> results into zero
>>> console.log(thisSearch, thisCount, thisRslt); //progress outputted to
>>> console
>>> output.push(thisSearch + "\t" + thisCount); //add to the output variable
>>> });
>>> fs.writeFileSync(__dirname + '/output.txt', output.join('\n')); //puts the
>>> final result in a file named "output.txt"
>>>
>>>
>>>
>
> --
> This is the BBEdit Talk public discussion group. If you have a feature
> request or need technical support, please email "[email protected]"
> rather than posting here. Follow @bbedit on Twitter:
> <https://twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google Groups
> "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bbedit/985ce28f-b5c4-4db0-b1b0-5df8f51b25f6n%40googlegroups.com.
--
This is the BBEdit Talk public discussion group. If you have a feature request
or need technical support, please email "[email protected]" rather than
posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/bbedit/11A04C03-29A5-4CC6-9C04-08786A709436%40gmail.com.