[GH-ISSUE #9] [Feature] Regex for website scraping #4

Open
opened 2026-10-15 13:28:15 +00:00 by bsakaguchi · 14 comments
Owner

Originally created by @NXTler on GitHub (Feb 20, 2025).
Original GitHub issue: https://github.com/SillyTavern/Extension-WebSearch/issues/9

Greetings,
I recently started experimenting with this extension and found that there are a lot of characters wasted due to website formatting, like multiple newlines for no apparent reason. I propose the integration of the regex system into the scraping process, so that we can have more information to process (due to increased information density) and better formatting (like removing the extra space before a new paragraph, like some websites have).

Originally created by @NXTler on GitHub (Feb 20, 2025). Original GitHub issue: https://github.com/SillyTavern/Extension-WebSearch/issues/9 Greetings, I recently started experimenting with this extension and found that there are a lot of characters wasted due to website formatting, like multiple newlines for no apparent reason. I propose the integration of the regex system into the scraping process, so that we can have more information to process (due to increased information density) and better formatting (like removing the extra space before a new paragraph, like some websites have).
Author
Owner

@NXTler commented on GitHub (Mar 3, 2025):

In case someone is searching for a quick and dirty solution - you can replace at const text line 708 in index.js with something like

const rawtext = await extractTextFromHTML(data, 'p'); // Only extract text from <p> tags
const text = rawtext
 .replace(/\n\n\n\n+/g, '\n\n\n') //removes excessive new lines
 .replace(/\s\s+/g, ' ') //removes double spaces
 .replace(/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g, ''); //removes emojis

This cleans up the output a bit, but I'm unsure if you actually get more information with this.

<!-- gh-comment-id:2692999857 --> @NXTler commented on GitHub (Mar 3, 2025): In case someone is searching for a quick and dirty solution - you can replace at `const text` line 708 in index.js with something like ``` const rawtext = await extractTextFromHTML(data, 'p'); // Only extract text from <p> tags const text = rawtext .replace(/\n\n\n\n+/g, '\n\n\n') //removes excessive new lines .replace(/\s\s+/g, ' ') //removes double spaces .replace(/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g, ''); //removes emojis ``` This cleans up the output a bit, but I'm unsure if you actually get more information with this.
Author
Owner

@Cohee1207 commented on GitHub (Mar 3, 2025):

There's already a method that post-processes text in core code. So if you need specific processing, add it there.

github.com/SillyTavern/SillyTavern@938c8a9a36/public/scripts/utils.js (L1515)

<!-- gh-comment-id:2693565920 --> @Cohee1207 commented on GitHub (Mar 3, 2025): There's already a method that post-processes text in core code. So if you need specific processing, add it there. https://github.com/SillyTavern/SillyTavern/blob/938c8a9a364919433ac1cb0e6d82710116dabf86/public/scripts/utils.js#L1515
Author
Owner

@NXTler commented on GitHub (Mar 3, 2025):

Thanks, I will experiment a bit and try to get more info into the budget with this. When I have something working I might commit it.

<!-- gh-comment-id:2694094619 --> @NXTler commented on GitHub (Mar 3, 2025): Thanks, I will experiment a bit and try to get more info into the budget with this. When I have something working I might commit it.
Author
Owner

@Cohee1207 commented on GitHub (Mar 3, 2025):

.replace(/\n\n\n\n+/g, '\n\n\n') //removes excessive new lines
.replace(/\s\s+/g, ' ') //removes double spaces

These look good generally.

.replace(/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g, ''); //removes emojis

This is questionable. I think if you're scraping articles about emojis, losing emojis would be detrimental.

<!-- gh-comment-id:2694104837 --> @Cohee1207 commented on GitHub (Mar 3, 2025): > .replace(/\n\n\n\n+/g, '\n\n\n') //removes excessive new lines > .replace(/\s\s+/g, ' ') //removes double spaces These look good generally. > .replace(/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g, ''); //removes emojis This is questionable. I think if you're scraping articles about emojis, losing emojis would be detrimental.
Author
Owner

@NXTler commented on GitHub (Mar 3, 2025):

For the last one I just search how to remove emojis in js and haven't spend much time testing it, I'm also unsure if that's the correct way.

<!-- gh-comment-id:2694123019 --> @NXTler commented on GitHub (Mar 3, 2025): For the last one I just search how to remove emojis in js and haven't spend much time testing it, I'm also unsure if that's the correct way.
Author
Owner

@NXTler commented on GitHub (Mar 3, 2025):

Okay, this is how it looks now:

        const rawtext = await extractTextFromHTML(data, 'p'); // Only extract text from <p> tags

        const text = rawtext
            .replace(/^\n+|\n+$/g, '')                                                // Remove leading and trailing new lines
            .replace(/\n{4,}/g, '\n\n\n')                                             // Remove excessive new lines
            .replace(/(?<!.) +(?=(?:|["'([])(?:\w+ ){2})| +(?!.)/g, '')               // Remove leading and trailing spaces
            .replace(/(?<! )  (?! )/g, ' ')                                           // Remove double spaces
            .replace(/​(?<= \w+) +(?=[.,?!:;])/g, '')                                  // Remove leading spaces from Punctiation
            .replace(/(?<= \w+)[.,!?:;](?=\w+(?: |, ))/g, '$& ')                      // Add missing spaces between sentences
            .replace(/(\p{Emoji_Presentation}|\p{Extended_Pictographic})(?=\1)/g, '') // Remove spammed emojis
            ;

This really cleans up the output without changing the actual information that much. Emojis are still a bit weird, as they sometimes have or not have leading/trailing spaces.

EDIT: fixed some issues with the replacements, should work in more cases now while preserving more information.
EDIT: Further improved leading space detection

<!-- gh-comment-id:2694906311 --> @NXTler commented on GitHub (Mar 3, 2025): Okay, this is how it looks now: ``` const rawtext = await extractTextFromHTML(data, 'p'); // Only extract text from <p> tags const text = rawtext .replace(/^\n+|\n+$/g, '') // Remove leading and trailing new lines .replace(/\n{4,}/g, '\n\n\n') // Remove excessive new lines .replace(/(?<!.) +(?=(?:|["'([])(?:\w+ ){2})| +(?!.)/g, '') // Remove leading and trailing spaces .replace(/(?<! ) (?! )/g, ' ') // Remove double spaces .replace(/​(?<= \w+) +(?=[.,?!:;])/g, '') // Remove leading spaces from Punctiation .replace(/(?<= \w+)[.,!?:;](?=\w+(?: |, ))/g, '$& ') // Add missing spaces between sentences .replace(/(\p{Emoji_Presentation}|\p{Extended_Pictographic})(?=\1)/g, '') // Remove spammed emojis ; ``` This really cleans up the output without changing the actual information that much. Emojis are still a bit weird, as they sometimes have or not have leading/trailing spaces. EDIT: fixed some issues with the replacements, should work in more cases now while preserving more information. EDIT: Further improved leading space detection
Author
Owner

@NXTler commented on GitHub (Mar 3, 2025):

Seems to work correctly with the character budget.

<!-- gh-comment-id:2695238075 --> @NXTler commented on GitHub (Mar 3, 2025): Seems to work correctly with the character budget.
Author
Owner

@NXTler commented on GitHub (Mar 13, 2025):

Okay, after some edits, I would call these expressions good. They differ quite a lot from the ones in postProcessText, so how should go about adding these? Or should I rather create a new function for this?

<!-- gh-comment-id:2722725659 --> @NXTler commented on GitHub (Mar 13, 2025): Okay, after some edits, I would call these expressions good. They differ quite a lot from the ones in `postProcessText`, so how should go about adding these? Or should I rather create a new function for this?
Author
Owner

@Cohee1207 commented on GitHub (Mar 13, 2025):

I'm not sure about a new function, especially around removing emojis. The rest of the regex look fine.

<!-- gh-comment-id:2722764760 --> @Cohee1207 commented on GitHub (Mar 13, 2025): I'm not sure about a new function, especially around removing emojis. The rest of the regex look fine.
Author
Owner

@NXTler commented on GitHub (Mar 13, 2025):

It doesn't remove emojis completely, it simply collapses multiple into one to handle unnecessary spamming from some websites.

<!-- gh-comment-id:2722775257 --> @NXTler commented on GitHub (Mar 13, 2025): It doesn't remove emojis completely, it simply collapses multiple into one to handle unnecessary spamming from some websites.
Author
Owner

@Cohee1207 commented on GitHub (Mar 13, 2025):

You mean if the same emoji is repeated multiple times? You can also target the emoji with a unicode group instead. This example will match all emojis in a string:

/(\p{Emoji_Presentation}|\p{Extended_Pictographic})/gu

<!-- gh-comment-id:2722787199 --> @Cohee1207 commented on GitHub (Mar 13, 2025): You mean if the same emoji is repeated multiple times? You can also target the emoji with a unicode group instead. This example will match all emojis in a string: `/(\p{Emoji_Presentation}|\p{Extended_Pictographic})/gu`
Author
Owner

@NXTler commented on GitHub (Mar 13, 2025):

Yes, that's what I meant. If one emoji is repeated multiple times, it will be reduced to only one instance.
I wasn't aware of that functionality. In that case, the regex would look like this:
.replace(/(\p{Emoji_Presentation}|\p{Extended_Pictographic})(?=\1)/gu, '') // Remove spammed emojis

So, how should I add these to postProcessText? I have some different approaches for some Regexs (see removing double spaces and leading and trailing spaces) to retain more information.

<!-- gh-comment-id:2722814981 --> @NXTler commented on GitHub (Mar 13, 2025): Yes, that's what I meant. If one emoji is repeated multiple times, it will be reduced to only one instance. I wasn't aware of that functionality. In that case, the regex would look like this: `.replace(/(\p{Emoji_Presentation}|\p{Extended_Pictographic})(?=\1)/gu, '') // Remove spammed emojis` So, how should I add these to `postProcessText`? I have some different approaches for some Regexs (see removing double spaces and leading and trailing spaces) to retain more information.
Author
Owner

@NXTler commented on GitHub (Mar 16, 2025):

@Cohee1207 I'm sorry for the ping, as I can see that you are very busy and probably have better more important problems. However, I have some time to spend and would like to complete my issue based on you suggestions.

<!-- gh-comment-id:2727385356 --> @NXTler commented on GitHub (Mar 16, 2025): @Cohee1207 I'm sorry for the ping, as I can see that you are very busy and probably have better more important problems. However, I have some time to spend and would like to complete my issue based on you suggestions.
Author
Owner

@Cohee1207 commented on GitHub (Mar 16, 2025):

These should go to postProcessText.

<!-- gh-comment-id:2727387891 --> @Cohee1207 commented on GitHub (Mar 16, 2025): These should go to postProcessText.
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
SillyTavern/Extension-WebSearch#4
No description provided.