updated responsemessages to evaluate own prior responses to new selected responses based on the client

This commit is contained in:
jenzur 2019-04-06 23:58:18 +02:00
parent 376e939980
commit 33ecf7730f
3 changed files with 143 additions and 33 deletions

View File

@ -55,8 +55,12 @@ public class Datahandler {
public static final long EXPIRE_TIME_IN_SECONDS1 = TimeUnit.SECONDS.convert(10, TimeUnit.HOURS); public static final long EXPIRE_TIME_IN_SECONDS1 = TimeUnit.SECONDS.convert(10, TimeUnit.HOURS);
public static Datahandler instance = new Datahandler(); public static Datahandler instance = new Datahandler();
private static volatile Double minDistance; private static volatile Double minDistance;
private static Annotation strAnno;
private static Annotation strAnnoSentiment;
private static Annotation strAnnoJMWE;
private volatile boolean refreshMatrixFromDB; private volatile boolean refreshMatrixFromDB;
private static volatile int secondaryIterator = 0; private static volatile int secondaryIterator = 0;
private static volatile Double preRelationCounters = 0.0;
private final ConcurrentMap<Integer, String> stringCache; private final ConcurrentMap<Integer, String> stringCache;
private static ConcurrentMap<String, Annotation> pipelineAnnotationCache; private static ConcurrentMap<String, Annotation> pipelineAnnotationCache;
private static ConcurrentMap<String, Annotation> pipelineSentimentAnnotationCache; private static ConcurrentMap<String, Annotation> pipelineSentimentAnnotationCache;
@ -395,7 +399,7 @@ public class Datahandler {
} }
} }
public synchronized String getResponseMsg(String str) throws CustomError { public synchronized String getResponseMsg(String str, String MostRecent) throws CustomError {
str = str.trim(); str = str.trim();
if (str.startsWith("<@")) { if (str.startsWith("<@")) {
str = str.substring(str.indexOf("> ") + 2); str = str.substring(str.indexOf("> ") + 2);
@ -405,7 +409,7 @@ public class Datahandler {
double Score = -10000; double Score = -10000;
SimilarityMatrix SMXreturn = new SimilarityMatrix("", ""); SimilarityMatrix SMXreturn = new SimilarityMatrix("", "");
System.out.println("pre mostSimilarSTR \n"); System.out.println("pre mostSimilarSTR \n");
String mostSimilarSTR = mostSimilar(str, strArrs); String mostSimilarSTR = mostSimilar(str, strArrs, MostRecent);
if (mostSimilarSTR != null) { if (mostSimilarSTR != null) {
LinkedHashMap<String, Double> orDefault = LHMSMXLocal.getOrDefault(mostSimilarSTR, null); LinkedHashMap<String, Double> orDefault = LHMSMXLocal.getOrDefault(mostSimilarSTR, null);
if (orDefault != null) { if (orDefault != null) {
@ -438,24 +442,17 @@ public class Datahandler {
} }
} }
System.out.println("none within 8 range"); System.out.println("none within 8 range");
Annotation strAnno = new Annotation(str);
pipeline.annotate(strAnno);
Annotation strAnno2 = new Annotation(str);
pipelineSentiment.annotate(strAnno2);
List<String> notactualList = new ArrayList();
notactualList.add(str);
ConcurrentMap<String, Annotation> jmweAnnotation = PipelineJMWESingleton.INSTANCE.getJMWEAnnotation(notactualList);
final Annotation strAnno3 = jmweAnnotation.values().iterator().next();
ConcurrentMap<Integer, String> strCache = stringCache; ConcurrentMap<Integer, String> strCache = stringCache;
ConcurrentMap<Integer, Future<SimilarityMatrix>> futureslocal = new MapMaker().concurrencyLevel(2).makeMap(); ConcurrentMap<Integer, Future<SimilarityMatrix>> futureslocal = new MapMaker().concurrencyLevel(2).makeMap();
ConcurrentMap<Integer, SimilarityMatrix> futurereturn = new MapMaker().concurrencyLevel(2).makeMap(); ConcurrentMap<Integer, SimilarityMatrix> futurereturn = new MapMaker().concurrencyLevel(2).makeMap();
String strF = str; String strF = str;
getSingularAnnotation(strF);
strCache.values().parallelStream().forEach((str1) -> { strCache.values().parallelStream().forEach((str1) -> {
if (!strF.equals(str1)) { if (!strF.equals(str1)) {
SimilarityMatrix SMX = new SimilarityMatrix(strF, str1); SimilarityMatrix SMX = new SimilarityMatrix(strF, str1);
Callable<SimilarityMatrix> worker = new SentimentAnalyzerTest(strF, str1, SMX, Callable<SimilarityMatrix> worker = new SentimentAnalyzerTest(strF, str1, SMX,
strAnno3, jmweAnnotationCache.get(str1), strAnno, strAnnoJMWE, jmweAnnotationCache.get(str1), strAnno,
pipelineAnnotationCache.get(str1), strAnno2, pipelineAnnotationCache.get(str1), strAnnoSentiment,
pipelineSentimentAnnotationCache.get(str1)); pipelineSentimentAnnotationCache.get(str1));
futureslocal.put(futureslocal.size() + 1, executor.submit(worker)); futureslocal.put(futureslocal.size() + 1, executor.submit(worker));
} }
@ -463,13 +460,50 @@ public class Datahandler {
futureslocal.values().parallelStream().forEach((future) -> { futureslocal.values().parallelStream().forEach((future) -> {
SimilarityMatrix SMX = new SimilarityMatrix("", ""); SimilarityMatrix SMX = new SimilarityMatrix("", "");
try { try {
SMX = future.get(20, TimeUnit.SECONDS); SMX = future.get(5, TimeUnit.SECONDS);
futurereturn.put(futurereturn.size() + 1, SMX); futurereturn.put(futurereturn.size() + 1, SMX);
} catch (InterruptedException | ExecutionException | TimeoutException ex) { } catch (InterruptedException | ExecutionException | TimeoutException ex) {
System.out.println("ex getResponsemsg: " + ex.getMessage() + "\n"); System.out.println("ex getResponsemsg: " + ex.getMessage() + "\n");
} }
}); });
ConcurrentMap<Integer, SimilarityMatrix> smxUpdateReturn = new MapMaker().concurrencyLevel(2).makeMap();
for (SimilarityMatrix SMX : futurereturn.values()) { for (SimilarityMatrix SMX : futurereturn.values()) {
Double scoreRelation = 500.0;
boolean foundmatch = false;
if (!MostRecent.isEmpty()) {
LinkedHashMap<String, Double> orDefault1 = lHMSMX.getOrDefault(MostRecent, null);
if (orDefault1 != null) {
Collection<String> orDefaultstrs = orDefault1.keySet();
for (String strs : orDefaultstrs) {
if (strs.equals(SMX.getSecondaryString())) {
scoreRelation = orDefault1.get(SMX.getSecondaryString());
foundmatch = true;
break;
}
}
}
if (!foundmatch) {
orDefault1 = lHMSMX.getOrDefault(SMX.getSecondaryString(), null);
if (orDefault1 != null) {
Collection<String> orDefaultstrs = orDefault1.keySet();
for (String strs : orDefaultstrs) {
if (strs.equals(MostRecent)) {
scoreRelation = orDefault1.get(MostRecent);
foundmatch = true;
break;
}
}
}
}
}
if (!foundmatch) {
scoreRelation = getScoreRelationNewMsgToRecentMsg(SMX.getSecondaryString(), MostRecent);
}
if (scoreRelation > 500.0 + (25 * smxUpdateReturn.size())) {
smxUpdateReturn.put(smxUpdateReturn.size() + 1, SMX);
}
}
for (SimilarityMatrix SMX : smxUpdateReturn.values()) {
double distance = SMX.getDistance(); double distance = SMX.getDistance();
if (distance > Score) { if (distance > Score) {
Score = distance; Score = distance;
@ -481,31 +515,103 @@ public class Datahandler {
return SMXreturn.getSecondaryString(); return SMXreturn.getSecondaryString();
} }
public String mostSimilar(String toBeCompared, ConcurrentMap<Integer, String> concurrentStrings) { public void getSingularAnnotation(String str) {
strAnno = new Annotation(str);
pipeline.annotate(strAnno);
strAnnoSentiment = new Annotation(str);
pipelineSentiment.annotate(strAnnoSentiment);
List<String> notactualList = new ArrayList();
notactualList.add(str);
ConcurrentMap<String, Annotation> jmweAnnotation = PipelineJMWESingleton.INSTANCE.getJMWEAnnotation(notactualList);
strAnnoJMWE = jmweAnnotation.values().iterator().next();
}
private Double getScoreRelationNewMsgToRecentMsg(String str, String mostRecentMsg) {
SimilarityMatrix SMX = new SimilarityMatrix(str, mostRecentMsg);
Callable<SimilarityMatrix> worker = new SentimentAnalyzerTest(str, mostRecentMsg, SMX,
jmweAnnotationCache.get(str), jmweAnnotationCache.get(mostRecentMsg), pipelineAnnotationCache.get(str),
pipelineAnnotationCache.get(mostRecentMsg), pipelineSentimentAnnotationCache.get(str),
pipelineSentimentAnnotationCache.get(mostRecentMsg));
SimilarityMatrix callSMX = null;
try {
callSMX = worker.call();
} catch (Exception ex) {
Logger.getLogger(Datahandler.class
.getName()).log(Level.SEVERE, null, ex);
}
if (callSMX != null) {
double smxDistance = callSMX.getDistance();
return smxDistance;
}
return 0.0;
}
public String mostSimilar(String toBeCompared, ConcurrentMap<Integer, String> concurrentStrings, String MostRecent) {
similar = ""; similar = "";
minDistance = 12.5; minDistance = 7.0;
preRelationCounters = 500.0;
ConcurrentMap<Integer, String> similardistances = new MapMaker().concurrencyLevel(2).makeMap();
concurrentStrings.values().parallelStream().forEach((str) -> { concurrentStrings.values().parallelStream().forEach((str) -> {
LevenshteinDistance leven = new LevenshteinDistance(toBeCompared, str); LevenshteinDistance leven = new LevenshteinDistance(toBeCompared, str);
double distance = leven.computeLevenshteinDistance(); double distance = leven.computeLevenshteinDistance();
if (distance < minDistance) { if (distance <= minDistance) {
minDistance = distance; minDistance = distance;
System.out.println("distance: " + distance + "\n"); System.out.println("distance: " + distance + "\n");
similar = str; similardistances.put(similardistances.size() + 1, str);
} }
}); });
LinkedHashMap<String, Double> orDefault = lHMSMX.getOrDefault(similar, null); if (similardistances.isEmpty()) {
if (orDefault == null) {
return null; return null;
} }
Double maxDistance = 0.0; Double maxDistance = 0.0;
for (Entry<String, Double> defaultEntry : orDefault.entrySet()) { for (String str : similardistances.values()) {
Double value = defaultEntry.getValue(); LinkedHashMap<String, Double> orDefault = lHMSMX.getOrDefault(str, null);
if (value > maxDistance) { if (orDefault != null) {
maxDistance = value; for (Entry<String, Double> defaultEntry : orDefault.entrySet()) {
similar = defaultEntry.getKey(); Double value = defaultEntry.getValue();
String key = defaultEntry.getKey();
if (value > maxDistance) {
Double RelationScore = 500.0;
boolean foundmatch = false;
if (!MostRecent.isEmpty()) {
LinkedHashMap<String, Double> orDefault1 = lHMSMX.getOrDefault(MostRecent, null);
if (orDefault1 != null) {
Collection<String> orDefaultstrs = orDefault1.keySet();
for (String strs : orDefaultstrs) {
if (strs.equals(key)) {
RelationScore = orDefault1.get(key);
foundmatch = true;
break;
}
}
}
if (!foundmatch) {
orDefault1 = lHMSMX.getOrDefault(key, null);
if (orDefault1 != null) {
Collection<String> orDefaultstrs = orDefault1.keySet();
for (String strs : orDefaultstrs) {
if (strs.equals(MostRecent)) {
RelationScore = orDefault1.get(MostRecent);
foundmatch = true;
break;
}
}
}
}
}
if (!foundmatch) {
RelationScore = getScoreRelationNewMsgToRecentMsg(key, MostRecent);
}
if (RelationScore > preRelationCounters) {
maxDistance = value;
similar = defaultEntry.getKey();
preRelationCounters = RelationScore;
}
}
}
} }
} }
return similar; return similar.isEmpty() ? null : similar;
} }
public static ConcurrentMap<Integer, String> cutContent(ConcurrentMap<Integer, String> str, boolean hlStatsMsg) { public static ConcurrentMap<Integer, String> cutContent(ConcurrentMap<Integer, String> str, boolean hlStatsMsg) {
@ -729,4 +835,5 @@ public class Datahandler {
}); });
return strmap; return strmap;
} }
} }

View File

@ -15,17 +15,17 @@ import java.util.concurrent.ConcurrentMap;
* @author install1 * @author install1
*/ */
public class MessageResponseHandler { public class MessageResponseHandler {
private static ConcurrentMap<Integer, String> str = new MapMaker().concurrencyLevel(2).makeMap(); private static ConcurrentMap<Integer, String> str = new MapMaker().concurrencyLevel(2).makeMap();
public static ConcurrentMap<Integer, String> getStr() { public static ConcurrentMap<Integer, String> getStr() {
return str; return str;
} }
public static void setStr(ConcurrentMap<Integer, String> str) { public static void setStr(ConcurrentMap<Integer, String> str) {
MessageResponseHandler.str = str; MessageResponseHandler.str = str;
} }
public static void getMessage(String message) { public static void getMessage(String message) {
if (message != null && !message.isEmpty()) { if (message != null && !message.isEmpty()) {
message = message.replace("@", ""); message = message.replace("@", "");
@ -38,8 +38,8 @@ public class MessageResponseHandler {
str.put(str.size() + 1, message); str.put(str.size() + 1, message);
} }
} }
public static String selectReponseMessage(String toString) throws CustomError { public synchronized static String selectReponseMessage(String toString, String mostRecentMsg) throws CustomError {
ConcurrentMap<Integer, String> str1 = new MapMaker().concurrencyLevel(2).makeMap(); ConcurrentMap<Integer, String> str1 = new MapMaker().concurrencyLevel(2).makeMap();
str1.put(str1.size() + 1, toString); str1.put(str1.size() + 1, toString);
str1 = Datahandler.cutContent(str1, false); str1 = Datahandler.cutContent(str1, false);
@ -49,7 +49,7 @@ public class MessageResponseHandler {
strreturn = str; strreturn = str;
} }
} }
String getResponseMsg = Datahandler.instance.getResponseMsg(strreturn); String getResponseMsg = Datahandler.instance.getResponseMsg(strreturn, mostRecentMsg);
return getResponseMsg; return getResponseMsg;
} }
} }

View File

@ -33,6 +33,8 @@ import org.javacord.api.entity.user.User;
*/ */
public class DiscordHandler { public class DiscordHandler {
private static String MostRecentMsg = "initial state";
public static void main(String[] args) { public static void main(String[] args) {
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "25"); System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "25");
try { try {
@ -92,10 +94,11 @@ public class DiscordHandler {
|| event.getServerTextChannel().get().toString().contains("general-autism")) { || event.getServerTextChannel().get().toString().contains("general-autism")) {
String ResponseStr; String ResponseStr;
try { try {
ResponseStr = MessageResponseHandler.selectReponseMessage(event.getMessage().toString()); ResponseStr = MessageResponseHandler.selectReponseMessage(event.getMessage().toString(), MostRecentMsg);
if (!ResponseStr.isEmpty()) { if (!ResponseStr.isEmpty()) {
System.out.print("\nResponseStr3: " + ResponseStr + "\n"); System.out.print("\nResponseStr3: " + ResponseStr + "\n");
event.getChannel().sendMessage(ResponseStr); event.getChannel().sendMessage(ResponseStr);
MostRecentMsg = ResponseStr;
} }
new Thread(() -> { new Thread(() -> {
try { try {