I’m working on an Exact string matching with Suffix Trie, but I have problems in adding child nodes to the suffix trie as it keep returning null. I can’t figure out why my code keeps returning no matter I do anything.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class SuffixTrie {
private SuffixTrieNode root = new SuffixTrieNode();
public SuffixTrieNode insert(String str, int startPosition) {
SuffixTrieNode current = root;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
SuffixTrieNode child = current.getChild(c);
if (child == null) {
child = new SuffixTrieNode();
current.addChild(c, child);
}
current = child;
}
current.addData(startPosition, 0);
if (current.data == null) {
current.data = new SuffixTrieData();
}
return current;
}
public SuffixTrieNode get(String str) {
SuffixTrieNode current = root;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
current = current.getChild(c);
// If current is null at any point, it means the substring does not exist
if (current == null) {
return null;
}
}
return current;
}
public static SuffixTrie readInFromFile(String fileName) {
SuffixTrie trie = new SuffixTrie();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
int sentencePosition = 0;
while ((line = br.readLine()) != null) {
trie.insert(line, sentencePosition);
sentencePosition++;
}
} catch (IOException e) {
e.printStackTrace();
}
return trie;
}
}
import java.util.HashMap;
public class SuffixTrieNode {
SuffixTrieData data = null;
int numChildren = 0;
private final HashMap<Character, SuffixTrieNode> children;
public SuffixTrieNode() {
this.children = new HashMap<>();
}
public SuffixTrieNode getChild(char label) {
return this.children.get(label);
}
public void addChild(char label, SuffixTrieNode node) {
this.children.put(label, node);
numChildren++;
}
public void addData(int sentencePos, int characterPos) {
if (data == null) {
data = new SuffixTrieData();
}
data.addStartIndex(new SuffixIndex(sentencePos, characterPos));
}
public String toString() {
if (data != null) {
return data.toString();
} else {
return null;
}
}
}
import java.util.*;
public class SuffixTrieDriver {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String fileName = in.nextLine();
Queue<String> ss = new ArrayDeque<>();
String suffix = in.nextLine();
while (!suffix.equals(""))
{
ss.offer(suffix);
suffix = in.nextLine();
}
SuffixTrie st = SuffixTrie.readInFromFile(fileName);
while (!ss.isEmpty()) {
String s = ss.poll();
SuffixTrieNode sn = st.get(s);
System.out.println("[" + s + "]: " + sn);
}
}
}
import java.util.ArrayList;
public class SuffixTrieData {
private final ArrayList<SuffixIndex> startIndexes = new ArrayList<>();
public void addStartIndex(SuffixIndex startIndex) {
startIndexes.add(startIndex);
}
public String toString() {
return startIndexes.toString();
}
}
public class SuffixIndex {
int sentence;
int character;
public SuffixIndex(int sentence, int character) {
this.sentence = sentence;
this.character = character;
}
public int getSentence() {
return sentence;
}
public void setSentence(int sentence) {
this.sentence = sentence;
}
public int getCharacter() {
return character;
}
public void setCharacter(int character) {
this.character = character;
}
/**
* The sentence and character position in the format sentence.character notation
* @return the position.
*/
@Override
public String toString() {
return sentence + "." + character;
}
}`
The above is all the classes I’ve got.
the output I get is this
`SuffixTrie/data/Frank02.txt
and
the
, the
onster
ngeuhhh
ing? This
[and]: null
[the]: null
[, the]: null
[onster]: null
[ngeuhhh]: null
[ing? This]: null`
But the output I want is
`[and]: [1.27, 1.87, 3.34, 3.153, 4.15, 6.70, 7.66, 7.148, 8.88, 9.100, 9.124, 9.177, 9.203, 10.17, 12.67, 12.91, 13.97, 13.114, 14.27, 14.90]
[the]: [0.58, 1.116, 3.51, 3.96, 5.39, 7.36, 7.48, 7.127, 8.1, 8.18, 8.76, 9.1, 9.89, 9.232, 9.253, 10.57, 10.74, 12.7, 12.22, 12.56, 12.158, 13.42, 13.65, 13.144, 14.1, 14.31, 14.146, 15.19, 15.69, 15.133, 15.184, 15.288, 15.302]
[, the]: [8.16]
[onster]: null
[monst]: null`
I tried making adjustments to`your text` addChild(), getChild() methods but didn't work.
Wenori Naoshika is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.