Group Transitive Synonyms by Four-Token Context
Learn this problemProblem statement
You are given an array of sentences. Split each sentence on whitespace and compare tokens exactly, including case and punctuation.
An occurrence of a word has a complete four-token context when it has at least two tokens before it and at least two tokens after it. Its context is the ordered tuple consisting of those two preceding tokens followed by those two following tokens. Two distinct words are directly synonymous if they occur with an identical complete context.
Synonymy is transitive. Return every connected synonym group containing at least two distinct words. Sort each group lexicographically, then sort the groups lexicographically by their full word lists.
Function
groupContextSynonyms(sentences: String[]) → String[][]Examples
Example 1
sentences = ["we really like cats very much","we really adore cats very much","people often adore music at work","people often love music at work"]return = [["adore","like","love"]]like and adore share one context, while adore and love share another. Transitivity joins all three words.
Example 2
sentences = ["red blue cat x y","red blue dog x y","m n run p q","m n jog p q"]return = [["cat","dog"],["jog","run"]]The two independent contexts create two groups. Their sorted word lists determine the group order.
Example 3
sentences = ["cat x y z","dog x y z"]return = []The differing first tokens do not have two tokens before them, so they cannot create a direct relation.
Constraints
0 <= sentences.length <= 50000.- Each sentence contains at most 200 whitespace-separated tokens.
- The total number of tokens is at most 1000000.
- Each token is non-empty and contains no whitespace.
- Token comparison and lexicographic ordering are case-sensitive.