Counting Permutations of Letter-Digraphs in a Large Corpus
In this article, we will explore the process of counting the permutations of letter-digraphs within a large corpus of text. Letter-digraphs are pairs of letters or a letter followed by a digit. For example, in the word "A23B", we have the letter-digraphs "A2", "23", and "3B". We will focus on finding all unique permutations of these letter-digraphs, such as "2A", "23", "32", "A3", "B3", and so on.
What are Letter-Digraphs?
A digraph is a pair of characters, which can be two letters or a letter followed by a digit. For example, in the word "A23B", we have three digraphs: "A2", "23", and "3B".
Counting Permutations of Letter-Digraphs
To count the permutations of letter-digraphs within a text, we first need to extract all unique letter-digraphs from the corpus. This can be done by iterating over the text and keeping track of all unique letter-digraphs found.
let uniqueDigraphs = new Set();
for (let i = 0; i < text.length - 1; i++) {
let digraph = text.substring(i, i + 2);
uniqueDigraphs.add(digraph);
}
Once we have the set of unique letter-digraphs, we can calculate the number of permutations by summing up the factorials of the length of each letter-digraph.
Calculating Factorials
A factorial is the product of all positive integers less than or equal to a given number. For example, the factorial of 4 is 4 × 3 × 2 × 1 = 24.
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
Counting Permutations
Now that we have the factorial function, we can calculate the number of permutations of letter-digraphs by summing up the factorials of the length of each letter-digraph.
let permutations = 0;
for (let digraph of uniqueDigraphs) {
permutations += factorial(digraph.length);
}
In this article, we have learned about letter-digraphs and how to count the permutations of letter-digraphs within a large corpus of text. We have covered the process of extracting unique letter-digraphs and calculating their factorials to find the total number of permutations.