Write an Adblocker!
code-golf
Introduction
You've gotten tired of reading text ads dispersed throughout your content.
You've tried solving this problem using a simple find-and-replace solution, but the advertisers have wisened up!
Every text ad now comes in slightly different variations to slip past your filter.
Challenge
Write an adblocker that, given a sample ad, removes all variations of it from the content.
Your program must remove every sentence in the input for which 75% of its words are identical, and in the same location.
This is codegolf, shortest answer in bytes wins!
Example
Given an example sentence from an advertisement, such as
Buy my computer now with cheap free shipping.
Buy my laptop today with cheap free shipping.
is an ad because 6 out of 8 words (>= 75%) are identical and in the same locations.
The computer was expensive but shipping was free.
is not an ad because 0 out of 8 words (< 75%) are identical and in the same location.
Note how computer
, shipping
, and free
don't count as identical because they are in different positions in the sentence.
If a sentence is not the same length as the ad, it isn't an ad.
Bonus
After matching an ad, remember which words were used as replacements, and use them to match future ads.
For example, when Buy my laptop today with cheap free shipping.
was matched to the given sample Buy my computer now with cheap free shipping.
, your program should remember that laptop
and today
are synonyms for computer
and now
, respectively.
If laptop
or today
are ever encountered in a future sentence, they should be matched with their synonyms in the ad.
Your program only needs to match synonyms in content that appears after the synonym was found.
So, if the sentence Purchase my laptop today with cheap free shipping.
was encountered after your program found out about the aforementioned synonyms, it would match because 7 out of 8 words matched in the same location (>= 75%). Note that a program that doesn't claim the bonus would only match 5 out of 8 words (< 75%), and would not consider the sentence an ad.
Your program should then proceed to make note that Purchase
is a synonym for Buy
, and continue processing the content.
Programs claiming this bonus can claim a 35% reduction of their score, i.e. score_with_bonus = number_of_bytes * 0.65
.
Input
Accept one sentence as a sample advertisement followed by a number of sentences of content.
Only letters of the alphabet, spaces, and periods will appear in the input.
Input may be taken in any convenient format for your language. For example, accepting a string for the ad sentence and an array of strings for the content is acceptable.
Output
Output the content without the ad variations.
The content must be in the same order.
The output may come in any reasonable format for your language. For example, printing to a file or stdout is acceptable. If your language makes it easier to output an array like ["Content 1", "Content 2", ...]
, or in a similar such format, this is acceptable.
Test Cases
Input
Two for one deal on lumber for a limited time only.
Make sure to pace yourself well when you run long distances.
You don't want to tire too quickly.
Three for one deal on tires for a limited time only.
Training is a must.
Two for one deal on lumber for a limited time only.
Three for one deal on tires for a short period only.
Output (with bonus)
Make sure to pace yourself well when you run long distances.
You don't want to tire too quickly.
Training is a must.
Output (no bonus)
Make sure to pace yourself well when you run long distances.
You don't want to tire too quickly.
Training is a must.
Three for one deal on tires for a short period only.
Notes
You may write a program or function.
More test cases to come!
This is my first challenge, so please give suggestions on how to improve!
In particular, comments on whether or not the bonus is appropriate would be appreciated. Also, it would be nice to know thoughts concerning the challenge as a whole (good, bad, too easy, too hard...?), so that I can better frame future ideas!
Thanks!