Pencocokan Corak Pantas Tali Menggunakan Pohon Akhiran di Jawa

1. Gambaran keseluruhan

Dalam tutorial ini, kita akan meneroka konsep padanan corak tali dan bagaimana kita dapat membuatnya lebih pantas. Kemudian, kita akan melalui pelaksanaannya di Jawa.

2. Pola Pemadanan Rentetan

2.1. Definisi

Dalam rentetan, pencocokan pola adalah proses memeriksa urutan watak tertentu yang disebut pola dalam urutan watak yang disebut teks .

Harapan asas pemadanan corak apabila corak bukan ungkapan biasa adalah:

  • perlawanan mestilah tepat - tidak separa
  • hasilnya harus mengandungi semua perlawanan - bukan hanya perlawanan pertama
  • hasilnya harus mengandungi kedudukan setiap pertandingan dalam teks

2.2. Mencari Corak

Mari gunakan contoh untuk memahami masalah pemadanan corak mudah:

Pattern: NA Text: HAVANABANANA Match1: ----NA------ Match2: --------NA-- Match3: ----------NA

Kita dapat melihat bahawa pola NA berlaku tiga kali dalam teks. Untuk mendapatkan hasil ini, kita boleh memikirkan meluncurkan corak teks satu watak pada satu masa dan memeriksa pertandingan.

Walau bagaimanapun, ini adalah pendekatan brute-force dengan kerumitan waktu O (p * t) di mana p adalah panjang corak, dan t adalah panjang teks.

Katakan kita mempunyai lebih daripada satu corak untuk dicari. Kemudian, kerumitan waktu juga meningkat secara linear kerana setiap corak memerlukan lelaran yang terpisah.

2.3. Struktur Data Trie untuk Menyimpan Corak

Kami boleh meningkatkan masa carian dengan menyimpan corak dalam struktur data indone, yang terkenal dengan semula yang cepat indone val item.

Kita tahu bahawa struktur data trie menyimpan watak rentetan dalam struktur seperti pohon. Oleh itu, untuk dua rentetan {NA, NAB} , kita akan mendapat sebatang pokok dengan dua jalur:

Setelah diciptakan trie memungkinkan untuk meluncurkan sekumpulan corak ke bawah teks dan memeriksa kecocokan hanya dalam satu lelaran.

Perhatikan bahawa kita menggunakan watak $ untuk menunjukkan akhir rentetan.

2.4. Akhiran Trie Struktur Data untuk Menyimpan Teks

A indone akhiran , di sisi lain, adalah struktur data indone dibina menggunakan semua akhiran kemungkinan rentetan tunggal .

Untuk contoh HAVANABANANA sebelumnya , kita boleh membina akhiran:

Percubaan akhiran dibuat untuk teks dan biasanya dilakukan sebagai sebahagian daripada langkah pra-pemprosesan. Selepas itu, mencari corak dapat dilakukan dengan cepat dengan mencari jalan yang sepadan dengan urutan corak.

Walau bagaimanapun, akhiran trie diketahui memakan banyak ruang kerana setiap watak rentetan disimpan di tepi.

Kami akan melihat versi akhiran akhiran yang diperbaiki di bahagian seterusnya.

3. Pokok Akhiran

A akhiran pokok hanya satu termampat akhiran indone . Maksudnya ialah, dengan menggabungkan bahagian tepi, kita dapat menyimpan sekumpulan watak dan dengan itu mengurangkan ruang simpanan dengan ketara.

Oleh itu, kita boleh membuat pokok akhiran untuk teks HAVANABANANA yang sama :

Setiap jalan yang bermula dari akar hingga daun mewakili akhiran tali HAVANABANANA .

Pokok akhiran juga menyimpan kedudukan akhiran di simpul daun . Contohnya, BANANA $ adalah akhiran bermula dari kedudukan ketujuh. Oleh itu, nilainya akan menjadi enam menggunakan penomboran berdasarkan sifar. Begitu juga, A-> BANANA $ adalah akhiran lain bermula pada kedudukan lima, seperti yang kita lihat dalam gambar di atas.

Oleh itu, dengan meletakkan sesuatu ke dalam perspektif, kita dapat melihat bahawa padanan corak berlaku apabila kita dapat mendapatkan jalan yang bermula dari simpul akar dengan tepi yang sepenuhnya sepadan dengan corak yang diberikan dengan kedudukan .

Sekiranya jalan berakhir pada simpul daun, kita mendapat padanan akhiran. Jika tidak, kita hanya mendapat perlawanan substring. Sebagai contoh, corak NA adalah akhiran HAVANABANA [NA] dan substring HAVA [NA] BANANA .

Di bahagian seterusnya, kita akan melihat bagaimana menerapkan struktur data ini di Java.

4. Struktur Data

Mari buat struktur data pokok akhiran. Kami memerlukan dua kelas domain.

Pertama, kita memerlukan kelas untuk mewakili simpul pokok . Ia perlu menyimpan tepi pokok dan simpul anak-anaknya. Selain itu, apabila ia adalah simpul daun, ia perlu menyimpan nilai kedudukan akhiran.

Oleh itu, mari buat kelas Node kami :

public class Node { private String text; private List children; private int position; public Node(String word, int position) { this.text = word; this.position = position; this.children = new ArrayList(); } // getters, setters, toString() }

Kedua, kita memerlukan kelas untuk mewakili pokok dan menyimpan nod akar . Ia juga perlu menyimpan teks penuh dari mana akhiran dihasilkan.

Oleh itu, kami mempunyai kelas SuffixTree :

public class SuffixTree { private static final String WORD_TERMINATION = "$"; private static final int POSITION_UNDEFINED = -1; private Node root; private String fullText; public SuffixTree(String text) { root = new Node("", POSITION_UNDEFINED); fullText = text; } }

5. Kaedah Pembantu Menambah Data

Sebelum kita menulis logik teras kita untuk menyimpan data, mari tambahkan beberapa kaedah pembantu. Ini akan terbukti berguna kemudian.

Mari ubah suai kelas SuffixTree kami untuk menambahkan beberapa kaedah yang diperlukan untuk membina pokok.

5.1. Menambah Node Anak

Pertama, mari kita kaedah addChildNode untuk menambahkan nod anak baru ke mana-mana nod ibu bapa yang diberikan :

private void addChildNode(Node parentNode, String text, int index) { parentNode.getChildren().add(new Node(text, index)); }

5.2. Mencari Awalan Umum Paling Panjang Dua Rentetan

Secondly, we'll write a simple utility method getLongestCommonPrefix to find the longest common prefix of two strings:

private String getLongestCommonPrefix(String str1, String str2) { int compareLength = Math.min(str1.length(), str2.length()); for (int i = 0; i < compareLength; i++) { if (str1.charAt(i) != str2.charAt(i)) { return str1.substring(0, i); } } return str1.substring(0, compareLength); }

5.3. Splitting a Node

Thirdly, let's have a method to carve out a child node from a given parent. In this process, the parent node's text value will get truncated, and the right-truncated string becomes the text value of the child node. Additionally, the children of the parent will get transferred to the child node.

We can see from the picture below that ANA gets split to A->NA. Afterward, the new suffix ABANANA$ can be added as A->BANANA$:

In short, this is a convenience method that will come in handy when inserting a new node:

private void splitNodeToParentAndChild(Node parentNode, String parentNewText, String childNewText) { Node childNode = new Node(childNewText, parentNode.getPosition()); if (parentNode.getChildren().size() > 0) { while (parentNode.getChildren().size() > 0) { childNode.getChildren() .add(parentNode.getChildren().remove(0)); } } parentNode.getChildren().add(childNode); parentNode.setText(parentNewText); parentNode.setPosition(POSITION_UNDEFINED); }

6. Helper Method for Traversal

Let's now create the logic to traverse the tree. We'll use this method for both constructing the tree and searching for patterns.

6.1. Partial Match vs. Full Match

First, let's understand the concept of a partial match and a full match by considering a tree populated with a few suffixes:

To add a new suffix ANABANANA$, we check if any node exists that can be modified or extended to accommodate the new value. For this, we compare the new text with all the nodes and find that the existing node [A]VANABANANA$ matches at first character. So, this is the node we need to modify, and this match can be called a partial match.

On the other hand, let's consider that we're searching for the pattern VANE on the same tree. We know that it partially matches with [VAN]ABANANA$ on the first three characters. If all the four characters had matched, we could call it a full match. For pattern search, a complete match is necessary.

So to summarize, we'll use a partial match when constructing the tree and a full match when searching for patterns. We'll use a flag isAllowPartialMatch to indicate the kind of match we need in each case.

6.2. Traversing the Tree

Now, let's write our logic to traverse the tree as long as we're able to match a given pattern positionally:

List getAllNodesInTraversePath(String pattern, Node startNode, boolean isAllowPartialMatch) { // ... }

We'll call this recursively and return a list of all the nodes we find in our path.

We start by comparing the first character of the pattern text with the node text:

if (pattern.charAt(0) == nodeText.charAt(0)) { // logic to handle remaining characters } 

For a partial match, if the pattern is shorter or equal in length to the node text, we add the current node to our nodes list and stop here:

if (isAllowPartialMatch && pattern.length() <= nodeText.length()) { nodes.add(currentNode); return nodes; } 

Then we compare the remaining characters of this node text with that of the pattern. If the pattern has a positional mismatch with the node text, we stop here. The current node is included in nodes list only for a partial match:

int compareLength = Math.min(nodeText.length(), pattern.length()); for (int j = 1; j < compareLength; j++) { if (pattern.charAt(j) != nodeText.charAt(j)) { if (isAllowPartialMatch) { nodes.add(currentNode); } return nodes; } } 

If the pattern matched the node text, we add the current node to our nodes list:

nodes.add(currentNode);

But if the pattern has more characters than the node text, we need to check the child nodes. For this, we make a recursive call passing the currentNode as the starting node and remaining portion of the pattern as the new pattern. The list of nodes returned from this call is appended to our nodes list if it's not empty. In case it's empty for a full match scenario, it means there was a mismatch, so to indicate this, we add a null item. And we return the nodes:

if (pattern.length() > compareLength) { List nodes2 = getAllNodesInTraversePath(pattern.substring(compareLength), currentNode, isAllowPartialMatch); if (nodes2.size() > 0) { nodes.addAll(nodes2); } else if (!isAllowPartialMatch) { nodes.add(null); } } return nodes;

Putting all this together, let's create getAllNodesInTraversePath:

private List getAllNodesInTraversePath(String pattern, Node startNode, boolean isAllowPartialMatch) { List nodes = new ArrayList(); for (int i = 0; i < startNode.getChildren().size(); i++) { Node currentNode = startNode.getChildren().get(i); String nodeText = currentNode.getText(); if (pattern.charAt(0) == nodeText.charAt(0)) { if (isAllowPartialMatch && pattern.length() <= nodeText.length()) { nodes.add(currentNode); return nodes; } int compareLength = Math.min(nodeText.length(), pattern.length()); for (int j = 1; j  compareLength) { List nodes2 = getAllNodesInTraversePath(pattern.substring(compareLength), currentNode, isAllowPartialMatch); if (nodes2.size() > 0) { nodes.addAll(nodes2); } else if (!isAllowPartialMatch) { nodes.add(null); } } return nodes; } } return nodes; }

7. Algorithm

7.1. Storing Data

We can now write our logic to store data. Let's start by defining a new method addSuffix on the SuffixTree class:

private void addSuffix(String suffix, int position) { // ... }

The caller will provide the position of the suffix.

Next, let's write the logic to handle the suffix. First, we need to check if a path exists matching the suffix partially at least by calling our helper method getAllNodesInTraversePath with isAllowPartialMatch set as true. If no path exists, we can add our suffix as a child to the root:

List nodes = getAllNodesInTraversePath(pattern, root, true); if (nodes.size() == 0) { addChildNode(root, suffix, position); }

However, if a path exists, it means we need to modify an existing node. This node will be the last one in the nodes list. We also need to figure out what should be the new text for this existing node. If the nodes list has only one item, then we use the suffix. Otherwise, we exclude the common prefix up to the last node from the suffix to get the newText:

Node lastNode = nodes.remove(nodes.size() - 1); String newText = suffix; if (nodes.size() > 0) { String existingSuffixUptoLastNode = nodes.stream() .map(a -> a.getText()) .reduce("", String::concat); newText = newText.substring(existingSuffixUptoLastNode.length()); }

For modifying the existing node, let's create a new method extendNode, which we'll call from where we left off in addSuffix method. This method has two key responsibilities. One is to break up an existing node to parent and child, and the other is to add a child to the newly created parent node. We break up the parent node only to make it a common node for all its child nodes. So, our new method is ready:

private void extendNode(Node node, String newText, int position) { String currentText = node.getText(); String commonPrefix = getLongestCommonPrefix(currentText, newText); if (commonPrefix != currentText) { String parentText = currentText.substring(0, commonPrefix.length()); String childText = currentText.substring(commonPrefix.length()); splitNodeToParentAndChild(node, parentText, childText); } String remainingText = newText.substring(commonPrefix.length()); addChildNode(node, remainingText, position); }

We can now come back to our method for adding a suffix, which now has all the logic in place:

private void addSuffix(String suffix, int position) { List nodes = getAllNodesInTraversePath(suffix, root, true); if (nodes.size() == 0) { addChildNode(root, suffix, position); } else { Node lastNode = nodes.remove(nodes.size() - 1); String newText = suffix; if (nodes.size() > 0) { String existingSuffixUptoLastNode = nodes.stream() .map(a -> a.getText()) .reduce("", String::concat); newText = newText.substring(existingSuffixUptoLastNode.length()); } extendNode(lastNode, newText, position); } }

Finally, let's modify our SuffixTree constructor to generate the suffixes and call our previous method addSuffix to add them iteratively to our data structure:

public void SuffixTree(String text) { root = new Node("", POSITION_UNDEFINED); for (int i = 0; i < text.length(); i++) { addSuffix(text.substring(i) + WORD_TERMINATION, i); } fullText = text; }

7.2. Searching Data

Having defined our suffix tree structure to store data, we can now write the logic for performing our search.

We begin by adding a new method searchText on the SuffixTree class, taking in the pattern to search as an input:

public List searchText(String pattern) { // ... }

Next, to check if the pattern exists in our suffix tree, we call our helper method getAllNodesInTraversePath with the flag set for exact matches only, unlike during the adding of data when we allowed partial matches:

List nodes = getAllNodesInTraversePath(pattern, root, false);

We then get the list of nodes that match our pattern. The last node in the list indicates the node up to which the pattern matched exactly. So, our next step will be to get all the leaf nodes originating from this last matching node and get the positions stored in these leaf nodes.

Let's create a separate method getPositions to do this. We'll check if the given node stores the final portion of a suffix to decide if its position value needs to be returned. And, we'll do this recursively for every child of the given node:

private List getPositions(Node node) { List positions = new ArrayList(); if (node.getText().endsWith(WORD_TERMINATION)) { positions.add(node.getPosition()); } for (int i = 0; i < node.getChildren().size(); i++) { positions.addAll(getPositions(node.getChildren().get(i))); } return positions; }

Once we have the set of positions, the next step is to use it to mark the patterns on the text we stored in our suffix tree. The position value indicates where the suffix starts, and the length of the pattern indicates how many characters to offset from the starting point. Applying this logic, let's create a simple utility method:

private String markPatternInText(Integer startPosition, String pattern) { String matchingTextLHS = fullText.substring(0, startPosition); String matchingText = fullText.substring(startPosition, startPosition + pattern.length()); String matchingTextRHS = fullText.substring(startPosition + pattern.length()); return matchingTextLHS + "[" + matchingText + "]" + matchingTextRHS; }

Now, we have our supporting methods ready. Therefore, we can add them to our search method and complete the logic:

public List searchText(String pattern) { List result = new ArrayList(); List nodes = getAllNodesInTraversePath(pattern, root, false); if (nodes.size() > 0) { Node lastNode = nodes.get(nodes.size() - 1); if (lastNode != null) { List positions = getPositions(lastNode); positions = positions.stream() .sorted() .collect(Collectors.toList()); positions.forEach(m -> result.add((markPatternInText(m, pattern)))); } } return result; }

8. Testing

Now that we have our algorithm in place, let's test it.

First, let's store a text in our SuffixTree:

SuffixTree suffixTree = new SuffixTree("havanabanana"); 

Next, let's search for a valid pattern a:

List matches = suffixTree.searchText("a"); matches.stream().forEach(m -> LOGGER.info(m));

Running the code gives us six matches as expected:

h[a]vanabanana hav[a]nabanana havan[a]banana havanab[a]nana havanaban[a]na havanabanan[a]

Next, let's search for another valid pattern nab:

List matches = suffixTree.searchText("nab"); matches.stream().forEach(m -> LOGGER.info(m)); 

Running the code gives us only one match as expected:

hava[nab]anana

Finally, let's search for an invalid pattern nag:

List matches = suffixTree.searchText("nag"); matches.stream().forEach(m -> LOGGER.info(m));

Running the code gives us no results. We see that matches have to be exact and not partial.

Thus, our pattern search algorithm has been able to satisfy all the expectations we laid out at the beginning of this tutorial.

9. Time Complexity

When constructing the suffix tree for a given text of length t, the time complexity is O(t).

Then, for searching a pattern of length p,the time complexity is O(p). Recollect that for a brute-force search, it was O(p*t). Thus, pattern searching becomes faster after pre-processing of the text.

10. Conclusion

Dalam artikel ini, kami pertama kali memahami konsep tiga struktur data - trie, akhiran trie, dan pohon akhiran. Kami kemudian melihat bagaimana pokok akhiran dapat digunakan untuk menyimpan akhiran dengan padat.

Kemudian, kami melihat cara menggunakan pohon akhiran untuk menyimpan data dan melakukan carian corak.

Seperti biasa, kod sumber dengan ujian tersedia di GitHub.