Skip to content

Commit

Permalink
default signature uses comment hash instead of random
Browse files Browse the repository at this point in the history
  • Loading branch information
mh-northlander committed Aug 5, 2024
1 parent 763ffa7 commit d303362
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
16 changes: 11 additions & 5 deletions src/main/java/com/worksap/nlp/sudachi/dictionary/Description.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

/**
* Description of the dictionary blocks, in-memory representation. Basically, an
Expand All @@ -39,13 +38,12 @@
public class Description {
private Instant creationTime = Instant.now();
private String comment = "";
private String signature = defaultSignature(creationTime);
private String signature = defaultSignature(creationTime, comment);
private String reference = "";
private List<Block> blocks = new ArrayList<>();
private long flags;
private int numTotalEntries;
private int numIndexedEntries;
private Random random = new Random();

/**
* Return a slice of the full dictionary with the provided name
Expand Down Expand Up @@ -220,10 +218,10 @@ private static void checkLegacyDictionaryFormat(ByteBuffer raw) {
}
}

private String defaultSignature(Instant date) {
private String defaultSignature(Instant date, String comment) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss", Locale.US);
return String.format("%s-%08x", formatter.format(LocalDateTime.ofInstant(date, ZoneId.systemDefault())),
random.nextLong());
comment.hashCode());
}

public Instant getCreationTime() {
Expand Down Expand Up @@ -258,6 +256,14 @@ public void setSignature(String signature) {
this.signature = signature;
}

/**
* Overwrite signature by the default value with the current creationTime and
* comment.
*/
public void setDefaultSignature() {
this.signature = defaultSignature(creationTime, comment);
}

public String getReference() {
return reference;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ private static void build(Path matrixPath, String description, String posPath, S
if (posPath != null) {
builder = builder.posTable(Paths.get(posPath));
}
if (signature != null) {
builder = builder.signature(signature);
}
for (String lexiconPath : lexiconPaths) {
builder = builder.lexicon(Paths.get(lexiconPath));
}
builder = builder.signature(signature);

try (SeekableByteChannel ch = Files.newByteChannel(outputPath, StandardOpenOption.WRITE,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,21 @@ private System readMatrix(String name, IOSupplier<InputStream> input, long size)
}

/**
* Set the system dictionary signature to the provided string. By default, it is
* current timestamp and a random 8 hexadecimal characters.
* Set the system dictionary signature to the provided string.
*
* If null is provided, set the default value that consists of current timestamp
* and a 8 hexadecimal hashcode calculated from the comment.
*
* @param signature
* provided dictionary signature. Can not be empty.
* @return current object
*/
public System signature(String signature) {
if (signature == null) {
throw new IllegalArgumentException("signature can not be null");
description.setDefaultSignature();
return this;
}

if (signature.isEmpty()) {
throw new IllegalArgumentException("signature can not be empty");
}
Expand Down

0 comments on commit d303362

Please sign in to comment.