Last active
August 1, 2020 16:06
-
-
Save qntm/71da7880c46caf8817aad2f00283f653 to your computer and use it in GitHub Desktop.
Parsing highly ambiguous Twitter trend strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { UNICODE, seq } = require('green-parse') | |
const trend = UNICODE.plus().map(chars => chars.join('')) | |
// match e.g. "trend1, trend2, trend3" and return ["trend1", "trend2", "trend3"] | |
const trends = trend.plus(', ') | |
// match e.g. "trend1, trend2 and trend3" and return ["trend1", "trend2", "trend3"] | |
const trendsAndTrend = seq([trends, ' and ', trend]) | |
.map(([trends, and, trend]) => [...trends, trend]) | |
const all = trends.or(trendsAndTrend) | |
const string = 'RIP June, Leon and June, June and Leon' | |
for (const value of all.parse(string)) { | |
console.log(value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Twitter reported that "Leon" was "Trending with: RIP June, Leon and June, June and Leon" but provided no obvious mechanism for parsing that highly ambiguous single string into an array of trend strings. The code above uses my library
green-parse
to generate ten possible parses:of which the first is honestly the most probable.
Disclaimer: I have no clue who these dead people are.