Review Token Tagging
AirbnbPhone screenMedium
A review site marks the names it knows. You get a review text and a map tokens that maps each token to a label. Write tag_review(review, tokens), which returns the review with every occurrence of a token wrapped as [label]{text}, where text is the occurrence exactly as it appears in the review.
Matching ignores case, so the token san francisco matches San Francisco, and the wrapped text keeps the casing of the review. A token may contain spaces and punctuation, and it matches only that exact sequence of characters (a single space matches a single space). A token matches anywhere, including inside a longer word. Everything outside matches is copied unchanged.
Part 1: Wrap each token
Return the tagged review. For example, with review = "I booked a house on Airbnb for my trip to San Francisco. It was a lovely experience." and tokens = {"Airbnb": "business", "san francisco": "city"}, the result is "I booked a house on [business]{Airbnb} for my trip to [city]{San Francisco}. It was a lovely experience.".
- Example 1
- review
"I booked a house on Airbnb for my trip to San Francisco. It was a lovely experience."- tokens
{"Airbnb":"business","san francisco":"city"}- Output
"I booked a house on [business]{Airbnb} for my trip to [city]{San Francisco}. It was a lovely experience."
Why:
Airbnbmatches itself, andsan franciscomatchesSan Franciscoacross the space. Each match keeps the casing the review used, so the label wrapsSan Francisco, notsan francisco.
Constraints
0 <= len(review) <= 10^40 <= len(tokens) <= 10001 <= len(token) <= 100for every token, and1 <= len(label) <= 20- The review, tokens and labels use printable ASCII characters only
- No two tokens are equal when case is ignored
- No two occurrences of tokens in the review share a character