Implement the training phase of the Byte Pair Encoding (BPE) algorithm, a subword tokenization method widely used in modern NLP models like GPT and RoBERTa.
Given a corpus represented as a dictionary mapping space-separated token sequences to their frequencies, and a number of merge operations to perform, implement the BPE training algorithm.
The algorithm works iteratively:
Note: When counting pairs in a word, if a pair appears multiple times in the same word (e.g., 'a b a b' contains 'a b' twice), count each occurrence separately, multiplied by the word frequency.
If there are fewer possible merges than num_merges (e.g., all words are single tokens), stop early.
The special token '</w>' represents end-of-word and should be treated as a regular token that can participate in merges.
Write a function that returns the list of merge operations performed, where each merge is a tuple of the two tokens that were merged.