battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.


battle programming a forum for elite programmers with extreme will power to sharpen their skills
 
HomeGallerySearchRegisterLog in
 

 DIAlerter a skill to manage multiple reminders

Go down 
AuthorMessage
kurosen
codding
codding
kurosen

Posts : 293
Join date : 2012-04-17

DIAlerter a skill to manage multiple reminders Empty
PostSubject: DIAlerter a skill to manage multiple reminders   DIAlerter a skill to manage multiple reminders EmptyWed Dec 02, 2020 7:35 pm

Code:
package chobit;

public class DIAlerter extends DISkill {
   private Alerter alerter = new Alerter();
   private DISkillUtils diSkillUtils = new DISkillUtils();
   protected Algorithm outputAlg = null;
   private Grammer grammer = new Grammer();
   public DIAlerter(Kokoro kokoro) {
      super(kokoro);
   }

   @Override
   public void input(String ear, String skin, String eye) {
      alerter.loadBullet(ear);
      String result = alerter.loadBullet();
      if (result.isEmpty()) {
         return;
      }
      result = grammer.toggleWords(result, "my", "your");
      outputAlg = diSkillUtils.verbatimGorithm(new APVerbatim(result));
   }

   @Override
   public void output(Neuron noiron) {
      if (!isNull(this.outputAlg)) {
         noiron.algParts.add(this.outputAlg);
         this.outputAlg = null;
      }
   }

   @Override
   public Boolean auto() {
      return true;
   }

   private boolean isNull(Object obj) {
      return obj == null;
   }
}

Code:
package chobit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Alerter {
   /*
    * the class manages reminders. here reminders are reffered to as spirit
    * bullets.
    *
    * loadReminder(String) : this func has several roles :
    *
    * 1 to input a reminder. examples : remind me to eat at 11:30 remind me to
    * exercise at 18:00 on sunday
    *
    * 2 to forget the reminders : clear reminders
    *
    * 3 reveal the next reminder compared to the time
    *
    * 4 recycle a reminder. normally a reminder gets deleted after it is fired up
    * unless you loadBullet("thank you"), this will reload the reminder.
    *
    * 5 attempting to add a reminder beyond the msgLim will result in the output :
    * too many reminders.
    *
    * loadBullet :
    *
    * this outputs the requested query or the reminder if its time has come. it
    * uses a timegate to prevent a reminder from firing up more than once.
    *
    */
   private RegexUtil regexUtil = new RegexUtil();
   private ZeroTimeGate timeGate = new ZeroTimeGate(1);
   private PlayGround playGround = new PlayGround();
   private ArrayList<AlerterMsg> msgs = new ArrayList<AlerterMsg>();
   private AlerterMsg activeReminder = new AlerterMsg();
   private int cmd = 0;
   private int msgLim = 4;

   private String translateTimes(String ear) {
      if (!ear.contains("on the clock")) {
         return ear;
      }
      String result = ear;
      result = ear.replace("1 on the clock", "1:00");
      result = ear.replace("2 on the clock", "2:00");
      result = ear.replace("3 on the clock", "3:00");
      result = ear.replace("4 on the clock", "4:00");
      result = ear.replace("5 on the clock", "5:00");
      result = ear.replace("6 on the clock", "6:00");
      result = ear.replace("7 on the clock", "7:00");
      result = ear.replace("8 on the clock", "8:00");
      result = ear.replace("9 on the clock", "9:00");
      result = ear.replace("10 on the clock", "10:00");
      result = ear.replace("11 on the clock", "11:00");
      result = ear.replace("12 on the clock", "12:00");
      result = ear.replace("13 on the clock", "13:00");
      result = ear.replace("14 on the clock", "14:00");
      result = ear.replace("15 on the clock", "15:00");
      result = ear.replace("16 on the clock", "16:00");
      result = ear.replace("17 on the clock", "17:00");
      result = ear.replace("18 on the clock", "18:00");
      result = ear.replace("19 on the clock", "19:00");
      result = ear.replace("20 on the clock", "20:00");
      result = ear.replace("21 on the clock", "21:00");
      result = ear.replace("22 on the clock", "22:00");
      result = ear.replace("23 on the clock", "23:00");
      result = ear.replace("24 on the clock", "24:00");
      return result;
   }
   public int getMsgLim() {
      return msgLim;
   }

   public void setMsgLim(int msgLim) {
      this.msgLim = msgLim;
   }
   public void loadBullet(String ear) {
      if (ear.contains("clear reminders")) {
         cmd = 6;
         msgs.clear();
         return;
      }
      if (ear.contains("next reminder")) {
            cmd = 1;
         return;
      }
      if (!timeGate.isClosed() && activeReminder.isActive()) {
         if (ear.contains("thank you")) {
            msgs.add(this.activeReminder);
            activeReminder = new AlerterMsg();
            cmd = 2;
         }
      }
      String ear2 = regexUtil.regexChecker("(.*)(?=on)", ear);
      if (ear2.isEmpty()) {
         ear2 = ear;
      }
      String msg = regexUtil.regexChecker("(?<=remind me to)(.*)(?=at)", ear2);
      if (msg.isEmpty()) {
         return;
      }
      if (msgs.size() > msgLim) {
         cmd = 3;
         return;
      }
      String time = regexUtil.timeStampRegex(translateTimes(ear2));
      if (regexUtil.regexChecker("(^([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])$)|(^([0-9]|[1][0-9]|[2][0-3])$)", time)
            .isEmpty()) {
         return;
      }
      if (time.isEmpty()) {
         return;
      }

      AlerterMsg alerterMsg = new AlerterMsg();
      alerterMsg
            .setDay(strContains(ear, "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"));
      if (timeUsed(time, alerterMsg.getDay())) {
         return;
      }
      alerterMsg.setTime(time);
      alerterMsg.setMsg(msg);
      // sort the list
      msgs.add(alerterMsg);
      cmd = 5;
      Collections.sort(msgs, new SortByDate());
   }

   private Boolean timeUsed(String t1, String day1) {
      if (msgs.size() > msgLim) {
         cmd = 3;
         return true;
      }
      for (AlerterMsg alerterMsg : msgs) {
         if (alerterMsg.getTime().equals(t1) && alerterMsg.getDay().equals(day1)) {
            cmd = 4;
            return true;
         }
      }
      return false;
   }
   public String loadBullet() {
      switch (cmd) {
      case 1:
         cmd = 0;
         if (msgs.isEmpty()) {
            return "no reminders";
         }
         AlerterMsg alerterMsgTmp = new AlerterMsg();
         alerterMsgTmp.setDay(playGround.getDayOfDWeek());
         alerterMsgTmp.setTime(playGround.getHoursAsInt() + ":" + playGround.getMinutes());
         SortByDate sortByDateTmp = new SortByDate();
         for (int counter = 0; counter < msgs.size(); counter++) {
            if (sortByDateTmp.compare(alerterMsgTmp, msgs.get(counter)) == -1) {
               return msgs.get(counter).toString();
            }
         }
         return msgs.get(0).toString();
      case 2:
         cmd = 0;
         return "you are welcome";
      case 3:
         cmd = 0;
         return "too many reminders";
      case 4:
         cmd = 0;
         return "a reminder already exists for that time";
      case 5:
         cmd = 0;
         return "yes my majesty";
      case 6:
         cmd = 0;
         return "reminders cleared";
      default:
         break;
      }
      if (!timeGate.isClosed()) {
         return "";
      }
      String tNow = playGround.getHoursAsInt() + ":" + playGround.getMinutes();
      for (AlerterMsg alerterMsg : msgs) {
         if (alerterMsg.getDay().equals(playGround.getDayOfDWeek()) || alerterMsg.getDay().isEmpty()) {
            if (tNow.equals(alerterMsg.getTime())) {
               timeGate.open();
               this.activeReminder = alerterMsg;
               msgs.remove(this.activeReminder);
               return alerterMsg.getMsg();
            }
         }
      }
      return "";
   }

   private static String strContains(String str1, String... a) {
      for (String temp : a) {
         if (str1.contains(temp)) {
            return temp;
         }
      }
      return "";
   }

   private void deleteAllMsgs() {
      msgs.clear();
   }

   static class SortByDate implements Comparator<AlerterMsg> {
      @Override
      public int compare(AlerterMsg a, AlerterMsg b) {
         int a1 = dayStrToInt(a.getDay()) * 10000;
         a1 += Integer.parseInt(a.getTime().replace(":", ""));
         int b1 = dayStrToInt(b.getDay()) * 10000;
         b1 += Integer.parseInt(b.getTime().replace(":", ""));
         int result = 0;
         if (a1 > b1) {
            return 1;
         }
         if (a1 == b1) {
            return 0;
         }
         return -1;
      }

      private int dayStrToInt(String day) {
         switch (day) {
         case "sunday":
            return 1;
         case "monday":
            return 2;
         case "tuesday":
            return 3;
         case "wednesday":
            return 4;
         case "thursday":
            return 5;
         case "friday":
            return 6;
         case "saturday":
            return 7;
         default:
            break;
         }
         return 8;
      }
   }
   class AlerterMsg {
      private String day = "";
      private String time = "";
      private String msg = "";

      public String getMsg() {
         return msg;
      }

      public void setMsg(String msg) {
         this.msg = msg;
      }

      public String getDay() {
         return day;
      }

      public void setDay(String day) {
         this.day = day;
      }

      public String getTime() {
         return time;
      }

      public void setTime(String time) {
         this.time = time;
      }

      public Boolean isActive() {
         return !time.isEmpty();
      }

      @Override
      public String toString() {
         if (this.getDay().isEmpty()) {
            return msg + " at " + getTime();
         }
         return msg + " at " + getTime() + " on " + this.getDay();
      }

   }
}

Code:
package chobit;

import java.util.Calendar;
import java.util.Date;

public class ZeroTimeGate {
   // a gate that only opens x minutes after it has been set
   private int pause = 1;
   private Date openedGate = new Date();
   private Date checkPoint = new Date();

   public ZeroTimeGate(int minutes) {
      super();
      this.pause = minutes;
      try {
         Thread.sleep(1000);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         // e.printStackTrace();
      }
   }

   public ZeroTimeGate() {
   }

   public Boolean isClosed() {
      return openedGate.before(new Date());
   }

   public Boolean isOpen() {
      return !openedGate.before(new Date());
   }
   public void open() {
      this.openedGate = addMinutesToJavaUtilDate(new Date(), pause);
   }

   public void open(int minutes) {
      Date now = new Date();
      openedGate = addMinutesToJavaUtilDate(now, minutes);
   }

   private Date addMinutesToJavaUtilDate(Date date, int minutes) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.MINUTE, minutes);
      return calendar.getTime();
   }

   public void setPause(int pause) {
      if (pause < 60 && pause > 0) {
         this.pause = pause;
      }
   }

   public void resetCheckPoint() {
      this.checkPoint = new Date();
   }

   public int givenTwoDateTimesInJava8_whenDifferentiatingInSeconds_thenWeGetTen() {
      Date now = new Date();
      long diff = now.getTime() - this.checkPoint.getTime();
      long diffSeconds = diff / 1000 % 60;
      // long diffMinutes = diff / (60 * 1000) % 60;
      // long diffHours = diff / (60 * 60 * 1000) % 24;
      // long diffDays = diff / (24 * 60 * 60 * 1000);
      // System.out.print(diffDays + " days, ");
      // System.out.print(diffHours + " hours, ");
      // System.out.print(diffMinutes + " minutes, ");
      // System.out.print(diffSeconds + " seconds.");
      return (int) diffSeconds;
   }
}

https://streamable.com/28j9o0
Back to top Go down
kurosen
codding
codding
kurosen

Posts : 293
Join date : 2012-04-17

DIAlerter a skill to manage multiple reminders Empty
PostSubject: Re: DIAlerter a skill to manage multiple reminders   DIAlerter a skill to manage multiple reminders EmptyWed Dec 23, 2020 10:59 pm

Code:
package chobit;

import java.awt.Point;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// returns expression of type theRegex from the string str2Check
public class RegexUtil {
    public String regexChecker(String theRegex, String str2Check) {
        Pattern checkRegex = Pattern.compile(theRegex);
        Matcher regexMatcher = checkRegex.matcher(str2Check);
        while (regexMatcher.find()) {
            if (regexMatcher.group().length() != 0) {
                return regexMatcher.group().trim();
            }
        }
        return "";
    }

   public String numberRegex(String str2Check) {
      String theRegex = "[-+]?[0-9]{1,13}(\\.[0-9]*)?";
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
         }
      }
      return "";
   }

   public String timeStampRegex(String str2Check) {
      String theRegex = "(([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])$)|(^([0-9]|[1][0-9]|[2][0-3])$)";
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
         }
      }
      return "";
   }
   public String intRegex(String str2Check) {
      String theRegex = "[-+]?[0-9]{1,13}";
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
         }
      }
      return "";
   }
   public Point pointRegex(String str2Check) {
      // "[-+]?[0-9]{1,13}(\\.[0-9]*)?" for double numbers
      String theRegex = "[-+]?[0-9]{1,13}";
      Point result = new Point(0, 0);
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            result.y = Integer.parseInt(regexMatcher.group().trim());
         }
      }
      String phase2 = str2Check.replace(result.y + "", "");
      phase2 = numberRegex(phase2);
      result.x = Integer.parseInt(phase2);
      return result;
   }
   public ArrayList<String> regexChecker2(String theRegex, String str2Check) {
      // return a list of all matches
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            list.add(regexMatcher.group().trim());
         }
      }
      return list;
   }

   public String contactRegex(String str2Check) {
      // return a list of all matches
      String theRegex = "(?<=contact)(.*)";
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
         }
      }
      return "";
   }

   public String emailRegex(String str2Check) {
      // return a list of all matches
      String theRegex = "^([_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{1,6}))?$";
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
         }
      }
      return "";
   }

   public String duplicateRegex(String str2Check) {
      // return a list of all matches
      // String theRegex = "\\b(\\w+)(\\b\\W+\\b\\1\\b)*";
      String theRegex = "\\b([\\w\\s']+) \\1\\b"; // set to 1 repeat of a word like hadoken hadoken
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return uniqueWord(regexMatcher.group().trim());
         }
      }
      return "";
   }

   public String uniqueWord(String str) {
      ArrayList<String> list = new ArrayList<String>();
      String s[] = str.split(" ");

      String p = s[0];
      list.add(p);

      for (int i = 1; i < s.length; i++) {

         if (!(p == s[i])) {
            list.add(s[i]);
         }
         p = s[i];
      } // i

      return list.get(0);
   }
   public String afterWord(String word, String str2Check) {
      // return a list of all matches
      String theRegex = "(?<=" + word + ")(.*)";
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
         }
      }
      return "";
   }

   public String phoneRegex1(String str2Check) {
      return regexChecker("[0]\\d{2}\\d{4}\\d{3}$", str2Check);
   }

   public String firstWord(String str2Check) {
      String arr[] = str2Check.split(" ", 2);
      String firstWord = arr[0]; // the
      return firstWord;
   }
}

Code:
package com.yotamarker.lgkotlin1;

public class Point{
    public int x;
    public int y;
    public Point(Point p)
    {
        x = p.x;
        y = p.y;
    }
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }
    public Point getLocation(){
        return new Point(x, y);
    }
    public String toString(){
        return getClass().getName() + "[x=" + x + ",y=" + y + ']';
    }
}

Code:
package chobit;

import java.util.ArrayList;

public class DISkillUtils {
   public Algorithm verbatimGorithm(AbsAlgPart itte) {
      // returns a simple algorithm for saying sent parameter
      String representation = "util";
      ArrayList<AbsAlgPart> algParts1 = new ArrayList<>();
      algParts1.add(itte);
      Algorithm algorithm = new Algorithm("util", representation, algParts1);
      return algorithm;
   }

   public Algorithm verbatimGorithm(String algMarker, AbsAlgPart itte) {
      // returns a simple algorithm for saying sent parameter
      String representation = "util";
      ArrayList<AbsAlgPart> algParts1 = new ArrayList<>();
      algParts1.add(itte);
      Algorithm algorithm = new Algorithm("util", representation, algParts1);
      return algorithm;
   }
}
Back to top Go down
kurosen
codding
codding
kurosen

Posts : 293
Join date : 2012-04-17

DIAlerter a skill to manage multiple reminders Empty
PostSubject: Re: DIAlerter a skill to manage multiple reminders   DIAlerter a skill to manage multiple reminders EmptyWed Dec 23, 2020 11:00 pm

Code:
package com.yotamarker.lgkotlin1;

public class Grammer {
    public String toggleWords(String sentence, String w1, String w2) {
        if (sentence.contains(w1)) {
            return sentence.replace(w1, w2);
        } else {
            if (sentence.contains(w2)) {
                return sentence.replace(w2, w1);
            }
        }
        return sentence;
    }
}


chobit emp it up ¯_(ツ)_/¯ get some guro humiliation krebskulm life fuel morph
Back to top Go down
 
DIAlerter a skill to manage multiple reminders
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: the LivinGrimoire-
Jump to: