×

Search anything:

# Byte Pair Encoding for Natural Language Processing (NLP)

#### Machine Learning (ML) Natural Language Processing (NLP) Get this book -> Problems on Array: For Interviews and Competitive Programming

Byte Pair Encoding is originally a compression algorithm that was adapted for NLP usage.

One of the important steps of NLP is determining the vocabulary.

There are different ways to model the vocabularly such as using an N-gram model, a closed vocabularly, bag of words, and etc. However, these methods are either very computationally memory heavy (usually paired with a large vocabularly size), do not handle OOV (out-of-vocabulary) words very well, or run into issues with words that rarely appear. Often times, many models will simply replace infrequently seen / unseen words with a special unknown token or ignore it. Those solutions will cause information loss which can be extremely important for certain NLP applications.

For those who are unfamilar with OOV, in short, OOV words are words that have not been seen in a model before after training.

Byte Pair Encoding comes in handy for handling the vocabulary issue through a bottom-up process where subwords are created from the initial vocabularly of characters.

• Subword example: Computer -> com pu t er

## Benefits

• Can set the vocabulary size (if feasible based on the given input data)
• Vocabulary is automatically decided for you.
• Vocabulary size is generally lower than other models.
• Thus more scalability for machine learning
• OOV is improved
• Relationships between subwords can be inferred
• Can suggest neogolism (new unseen words)

## Applications

• Used in different models, such as Neural Language Models (NLM)
• NLM useful for: autocompletion, program repair, code readability, and etc.
• Helpful in word segmentation and creating subword model.
• The process can have variance (Google NMT such as wordpiece model, sentencepiece model)

## Algorithm

Lets assume all the preprocessing work has been done in which all the words have been normalized and each word has a frequency count.

• input: [ { low : 5 }, { lower : 2 }, { newest : 6 }, { widest : 3 } ]
• An array of words with their associated frequency count.

Often times, there is an attached suffix token at the end of each word to help indicate when the word/subword is at the end.

• Ex: low -> low<\w>
1. Each word is character splitted while maintaining it's frequency count
• Ex: { low : 5 } -> { l o w : 5 }, { lower : 2 } -> { l o w e r : 2 }
2. Add each character to the vocabularly with their total frequency count (running sum from all the input data)
• Ex: { { l : 7 }, { o : 7 }, { w : 16 } ... }
3. Find the pair of subwords with the highest summed frequency together
• Ex: the pair (e, s) has a frequency of 9
• This is because { n e w e s t : 6 } and { w i d e s t : 3 } -> 6 + 3 = 9, there was a tie (e, s) and (s, t) but we decided to pick (e, s).
4. From step 3 results, add the results to the vocabulary with the frequency along with updating that pair for each word that contains that pair.
• Vocabulary should have all the letter frequencies and { es : 9 }
• Updated subwords should be: { n e w es t : 6 }, { w i d es t : 3 }
5. Repeat steps 3-4 until X amount of iterations have been completed or there are no more subword pairs to find.

## Code

The code below is an example of the algorithm mentioned without the vocabaulary but can easily be modified to include a vocabulary that was built up from the algorithm.

``````import java.util.*;

public class BytePairEncodingSubwords {
private static Map<Collection<String>, Integer> getPairFreq(Map<String, Integer> dictionary) {
Map<Collection<String>, Integer> pairFreqs = new HashMap<>();
for (Entry<String, Integer> wordFreq : dictionary.entrySet()) {
String[] subwords = wordFreq.getKey().split(" ");
int freq = wordFreq.getValue();

//creating all the possible pairs of subwords while mapping their associated count
for (int i = 0; i < subwords.length - 1; i++) {
List<String> pairOfSubwords = Arrays.asList(subwords[i], subwords[i + 1]);
pairFreqs.compute(pairOfSubwords, (k, v) -> v != null ? v + freq : freq);
}
}
return pairFreqs;
}

private static Collection<String> getMax(Map<Collection<String>, Integer> pairFreqs) {
if (pairFreqs.isEmpty()) {
return null;
}
//Determine the pair of subwords that has the highest count
return Collections.max(pairFreqs.entrySet(), (e1, e2) -> e1.getValue().compareTo(e2.getValue())).getKey();
}

private static Map<String, Integer> merge(Collection<String> replacementPair, Map<String, Integer> dict) {
Map<String, Integer> updatedDict = new HashMap<>(dict.size());
String[] replacementPairInfo = replacementPair.toArray(new String);
String replacementPattern = replacementPairInfo + " " + replacementPairInfo;
String replacementSubword = replacementPairInfo + replacementPairInfo;
//Update each subword that has the pair of subwords by concatenating them together.
dict.entrySet().forEach(entry -> updatedDict.put(entry.getKey().replaceAll(replacementPattern, replacementSubword), entry.getValue()));
return updatedDict;
}

private static Map<String, Integer> encode(Map<String, Integer> dict, int iterations) {
while (iterations > 0) {
Map<Collection<String>, Integer> pairings = getPairFreq(dict);
Collection<String> bestPair = getMax(pairings);
if (bestPair == null) { //No more pair of subwords can be generated (all are full words)
break;
}
dict = merge(bestPair, dict); //update the dictionary
iterations--;
}
return dict;
}

private static Map<String, Integer> decode(Map<String, Integer> dict) {
Map<String, Integer> decodedDict = new HashMap<>(dict.size());
dict.entrySet().forEach(entry -> decodedDict.put(entry.getKey().replaceAll(" ", ""), entry.getValue())); //joins the subwords together that are delimited by spaces
return decodedDict;
}

public static void main(String[] args) {
Map<String, Integer> dict = getBasicTestVocab();
System.out.println(dict);
dict = encode(dict, 10);
System.out.println(dict);
dict = decode(dict);
System.out.println(dict);
}

private static Map<String, Integer> getBasicTestVocab() {
Map<String, Integer> dict = new HashMap<>();
dict.put("l o w", 5);
dict.put("l o w e r", 2);
dict.put("n e w e s t", 6);
dict.put("w i d e s t", 3);
return dict;
}
}
``````

Things to take note from this example:

1. There is no vocabulary in the example but it would not be hard to pass around a vocabulary dictionary along with a limit check for the vocabulary dictionary to stop the algorithm. So step 1 was skipped but should be pretty straightforward.
2. If you change the line in main: encode(dict, 10) -> encode(dict, 1) or encode(dict, 2) you can see the process of the algorithm in which pair of subwords that were selected, concatenated and would be added to the vocabularly if there was one.
3. There comes a point to where all the subword pairings have been added and there are no more pairings left to select. So there is a check for that but also iterations is necessary to determine how many times this algorithm should run.
4. Lastly, there is the decode method in which you can see how easy it is to return the input back into our original input which is just concatenating the subwords together.

#### Ethan Z. Booker

Software Developer, Intern at OpenGenus | Student at University of Wisconsin - La Crosse

Byte Pair Encoding for Natural Language Processing (NLP)
{"ft_lang_label":"__label__en","ft_lang_prob":0.78912276,"math_prob":0.77515256,"size":7277,"snap":"2022-27-2022-33","text_gpt3_token_len":1740,"char_repetition_ratio":0.13350749,"word_repetition_ratio":0.029513888,"special_character_ratio":0.2582108,"punctuation_ratio":0.16119403,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9544229,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-28T06:30:26Z\",\"WARC-Record-ID\":\"<urn:uuid:cda347dc-3da3-4b18-b5aa-f6bc6cb8b4b5>\",\"Content-Length\":\"60769\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ef668c82-78e9-44cb-bff9-384fe6d11450>\",\"WARC-Concurrent-To\":\"<urn:uuid:d6ceb208-c4bd-4201-acca-0a1d718a78e6>\",\"WARC-IP-Address\":\"159.89.134.55\",\"WARC-Target-URI\":\"https://iq.opengenus.org/byte-pair-encoding/\",\"WARC-Payload-Digest\":\"sha1:LFOAGDQQ5SKVMIFQECKMQ2276S5MXUIH\",\"WARC-Block-Digest\":\"sha1:FOIQPPZTCTWNYTQTAWMGWWPTFK75D2RF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103355949.26_warc_CC-MAIN-20220628050721-20220628080721-00579.warc.gz\"}"}