Limit sample size for conversation actions

This commit is contained in:
M66B 2021-11-03 08:54:22 +01:00
parent 0c23517e38
commit faa2a334f0
1 changed files with 15 additions and 10 deletions

View File

@ -44,9 +44,10 @@ import java.util.Locale;
import java.util.Set; import java.util.Set;
public class TextHelper { public class TextHelper {
private static final int MAX_SAMPLE_SIZE = 8192; private static final int MAX_DETECT_SAMPLE_SIZE = 8192;
private static final float MIN_PROBABILITY = 0.80f; private static final float MIN_DETECT_PROBABILITY = 0.80f;
private static final String TRANSLITERATOR = "Any-Latin; Latin-ASCII"; private static final String TRANSLITERATOR = "Any-Latin; Latin-ASCII";
private static final int MAX_CONVERSATION_SAMPLE_SIZE = 8192;
static { static {
System.loadLibrary("fairemail"); System.loadLibrary("fairemail");
@ -63,11 +64,11 @@ public class TextHelper {
byte[] octets = text.getBytes(); byte[] octets = text.getBytes();
byte[] sample; byte[] sample;
if (octets.length < MAX_SAMPLE_SIZE) if (octets.length < MAX_DETECT_SAMPLE_SIZE)
sample = octets; sample = octets;
else { else {
sample = new byte[MAX_SAMPLE_SIZE]; sample = new byte[MAX_DETECT_SAMPLE_SIZE];
System.arraycopy(octets, 0, sample, 0, MAX_SAMPLE_SIZE); System.arraycopy(octets, 0, sample, 0, MAX_DETECT_SAMPLE_SIZE);
} }
long start = new Date().getTime(); long start = new Date().getTime();
@ -76,7 +77,7 @@ public class TextHelper {
long elapse = new Date().getTime() - start; long elapse = new Date().getTime() - start;
Log.i("cld3 language=" + result + " elapse=" + elapse); Log.i("cld3 language=" + result + " elapse=" + elapse);
if (result.probability < MIN_PROBABILITY) if (result.probability < MIN_DETECT_PROBABILITY)
return null; return null;
try { try {
@ -139,10 +140,14 @@ public class TextHelper {
.atZone(ZoneId.systemDefault()); .atZone(ZoneId.systemDefault());
List<ConversationActions.Message> input = new ArrayList<>(); List<ConversationActions.Message> input = new ArrayList<>();
for (String text : texts) for (String text : texts)
input.add(new ConversationActions.Message.Builder(author) if (!TextUtils.isEmpty(text)) {
.setReferenceTime(dt) if (text.length() > MAX_CONVERSATION_SAMPLE_SIZE)
.setText(text) text = text.substring(0, MAX_CONVERSATION_SAMPLE_SIZE);
.build()); input.add(new ConversationActions.Message.Builder(author)
.setReferenceTime(dt)
.setText(text)
.build());
}
Set<String> excluded = new HashSet<>(Arrays.asList( Set<String> excluded = new HashSet<>(Arrays.asList(
ConversationAction.TYPE_OPEN_URL, ConversationAction.TYPE_OPEN_URL,