Sorting Formenu List

Hi all,

I am looking to use the {=extractregexall({site:text})} to identify the total number of defined keywords on a site page. I have been able to successfully do this at a basic level (I'm sure my current method could be refined), however, I am seeing if there is a way to condense the results into a formmenu and then sort() the words based on the total sum of each result while maintaining the association between the word+number (e.g. sort() the number of times specific words appears on a page. "Cats: 3", "Dogs: 5", "Lizards: 7" > "Lizards: 7", "Dogs: 5", "Cats: 3").

Any suggestions on sorting these items inside a formmenu? Would I need to create a list to do this? If so, how would I format it?

Example:

{Identify=extractregexall({site: text}, "Public reply\n([\s\S]+)\nUser Data")}

{formmenu: name=keywords; default=;Cat: {=Cats}; Dog: {=Dogs}; Lizard: {=Lizards}}

Cat: {a1=extractregexall({=Identify}, "Cat")}{a2=extractregexall({=Identify}, "cat")}{Cat1=count(a1)}{Cat2=count(a2)}{Cats=sum([Cat1, Cat2])} {=Cats}
Dog: {a1=extractregexall({=Identify}, "Dog")}{a2=extractregexall({=Identify}, "dog")}{Dog1=count(a1)}{Dog2=count(a2)}{Dogs=sum([Dog1, Dog2])} {=Dogs}
Lizards: {a1=extractregexall({=Identify}, "Lizards")}{a2=extractregexall({=Identify}, "lizards")}{Lizards1=count(a1)}{Lizards2=count(a2)}{Lizards=sum([Lizards1, Lizards2])} {=Lizards}

Hi, it's hard to understand from this example what you are trying to do.

Can you update your example to use some example text rather than {site: text}?

Hi Scott,

I am trying to count multiple keywords on a site page at once (e.g. as if you used CTRL+F to search for words) and then sort the results based on the total sum of each word from highest to lowest.

For example, using https://blaze.today/ I want to find how many times the word "Text Blaze", "Snippet" and "Users" appear. Then sort the answers based on the number of results. In this case, it's already in order due to how I arranged the formulas.

Let me know if this still doesn't make sense.

image
image

{formmenu: name=listkeywords; default=;Text Blaze: {=AllTextBlaze}; Snippets: {=AllSnippets}; Users: {=AllUsers}}

{KeyWords={site:text}}

Text Blaze: {a1=extractregexall({=KeyWords}, "Text Blaze")}{a2=extractregexall({=KeyWords}, "text blaze")}{TextBlaze1=count(a1)}{TextBlaze2=count(a2)}{AllTextBlaze=sum([TextBlaze1, TextBlaze2])} {=AllTextBlaze}

Snippet: {b1=extractregexall({=KeyWords}, "Snippet")}{b2=extractregexall({=KeyWords}, "snippet")}{Snippet1=count(b1)}{Snippet2=count(b2)}{AllSnippets=sum([Snippet1, Snippet2])} {=AllSnippets}

Users: {c1=extractregexall({=KeyWords}, "Users")}{c2=extractregexall({=KeyWords}, "users")}{Users1=count(c1)}{Users2=count(c2)}{AllUsers=sum([Users1, Users2])} {=AllUsers}

{formmenu: name=listkeywords; default=;Text Blaze: {=AllTextBlaze}; Snippets: {=AllSnippets}; Users: {=AllUsers}}

{KeyWords={site:text}}

Text Blaze: {a1=extractregexall({=KeyWords}, "Text Blaze")}{a2=extractregexall({=KeyWords}, "text blaze")}{TextBlaze1=count(a1)}{TextBlaze2=count(a2)}{AllTextBlaze=sum([TextBlaze1, TextBlaze2])} {=AllTextBlaze}

Snippet: {b1=extractregexall({=KeyWords}, "Snippet")}{b2=extractregexall({=KeyWords}, "snippet")}{Snippet1=count(b1)}{Snippet2=count(b2)}{AllSnippets=sum([Snippet1, Snippet2])} {=AllSnippets}

Users: {c1=extractregexall({=KeyWords}, "Users")}{c2=extractregexall({=KeyWords}, "users")}{Users1=count(c1)}{Users2=count(c2)}{AllUsers=sum([Users1, Users2])} {=AllUsers}

1 Like

@Josh_Whitchurch - sounds like a great use case for SEO!

1 Like

Here is the general approach to how you would get an ordered set of word counts for a piece of Text:

{text="red green blue red green green blue blue brown blue"}
{words=split(text, " ")}
{unique_words=unique(words)}
{counts=[["word":word, "count":count(split(text, word))-1] for word in unique_words]}
{ordered_counts=sort(counts, (a, b) -> b.count - a.count)}

Word counts:
{repeat: for item in ordered_counts}
{=item.word}: {=item.count}
{endrepeat}

Perfect. This is exactly what I needed. I also included a filter() function and a list with keywords I only want to be included. Thanks again!

1 Like