List of usage examples for java.util HashMap replace
@Override
public V replace(K key, V value)
From source file:com.gmail.frogocomics.schematic.BiomeWorldV2Object.java
public static BiomeWorldV2Object load(File file) throws IOException { BufferedReader settingsReader; if (!file.exists()) { throw new FileNotFoundException(); }//from ww w . ja v a 2s. c om settingsReader = new BufferedReader(new FileReader(file)); int lineNumber = 0; String thisLine; ArrayList<BiomeWorldObjectBlock> bo2Blocks = new ArrayList<>(); while ((thisLine = settingsReader.readLine()) != null) { lineNumber++; if (Pattern.compile("[0-9]").matcher(thisLine.substring(0, 1)).matches() || thisLine.substring(0, 1).equalsIgnoreCase("-")) { //Example: -1,-1,5:18.4 // x,z,y:id.data String[] location = thisLine.split(":")[0].split(","); String[] block = thisLine.split(":")[1].split("\\."); bo2Blocks.add(new BiomeWorldObjectBlock(Integer.parseInt(location[0]), Integer.parseInt(location[2]), Integer.parseInt(location[1]), Short.parseShort(block[0]), Byte.parseByte(block[1]))); } } ArrayList<Integer> maxXMap = new ArrayList<>(); ArrayList<Integer> maxYMap = new ArrayList<>(); ArrayList<Integer> maxZMap = new ArrayList<>(); for (BiomeWorldObjectBlock bo2 : bo2Blocks) { maxXMap.add(bo2.getX()); maxYMap.add(bo2.getY()); maxZMap.add(bo2.getZ()); } int maxX = Collections.max(maxXMap); int maxY = Collections.max(maxYMap); int maxZ = Collections.max(maxZMap); int minX = Collections.min(maxXMap); int minY = Collections.min(maxYMap); int minZ = Collections.min(maxZMap); int differenceX = maxX - minX + 1; int differenceY = maxY - minY + 1; int differenceZ = maxZ - minZ + 1; HashMap<Integer, Set<BiomeWorldObjectBlock>> blocks = new HashMap<>(); for (int i = 0; i < differenceY + 1; i++) { blocks.put(i, new HashSet<>()); } for (BiomeWorldObjectBlock bo2 : bo2Blocks) { Set<BiomeWorldObjectBlock> a = blocks.get(bo2.getY() - minY); a.add(bo2); blocks.replace(bo2.getY(), a); } //System.out.println(differenceX + " " + differenceZ); SliceStack schematic = new SliceStack(differenceY, differenceX, differenceZ); for (Map.Entry<Integer, Set<BiomeWorldObjectBlock>> next : blocks.entrySet()) { Slice slice = new Slice(differenceX, differenceZ); for (BiomeWorldObjectBlock block : next.getValue()) { //System.out.println("Added block at " + String.valueOf(block.getX() - minX) + "," + String.valueOf(block.getZ() - minZ)); slice.setBlock(block.getBlock(), block.getX() - minX, block.getZ() - minZ); } schematic.addSlice(slice); } //System.out.println(schematic.toString()); return new BiomeWorldV2Object(schematic, FilenameUtils.getBaseName(file.getAbsolutePath())); }
From source file:org.korecky.nlp.library.BasicTextProcessing.java
/** * Segments and calculate words in the input text * * @param input: Input string// w ww. java 2 s.co m * @param normalizeText: Indicator, if text should be normalized before * counting * @param removeAccents: Indicator, if should be removed accent in * normalization process * @return Sorted TreeMap by count */ public TreeMap<String, Long> wordSegmentation(String input, boolean normalizeText, boolean removeAccents) { HashMap<String, Long> map = new HashMap<>(); WordCountComparator comparator = new WordCountComparator(map); TreeMap<String, Long> sorted_map = new TreeMap<String, Long>(comparator); if (normalizeText) { input = normalizeText(input, removeAccents); } // Words segmentation and calculation Pattern pattern = Pattern.compile( "[a-zA-Z\\u00C1\\u00C2\\u00C4\\u00C7\\u00C9\\u00CB\\u00CD\\u00CE\\u00D3\\u00D4\\u00D6\\u00DA\\u00DC\\u00DD\\u00DF\\u00E1\\u00E2\\u00E4\\u00E7\\u00E9\\u00EB\\u00ED\\u00EE\\u00F3\\u00F4\\u00F6\\u00FA\\u00FC\\u00FD\\u0102\\u0103\\u0104\\u0105\\u0106\\u0107\\u010C\\u010D\\u010E\\u010F\\u0110\\u0111\\u0118\\u0119\\u011A\\u011B\\u0139\\u013A\\u013D\\u013E\\u0141\\u0142\\u0143\\u0144\\u0147\\u0148\\u0150\\u0151\\u0154\\u0155\\u0158\\u0159\\u015A\\u015B\\u015E\\u015F\\u0160\\u0161\\u0162\\u0163\\u0164\\u0165\\u016E\\u016F\\u0170\\u0171\\u0179\\u017A\\u017B\\u017C\\u017D\\u017E]+"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { if (matcher.group() != null) { String foundString = matcher.group().trim(); if (map.containsKey(foundString)) { map.replace(foundString, map.get(foundString) + 1); } else { map.put(foundString, 1L); } } } sorted_map.putAll(map); return sorted_map; }
From source file:util.allocation.java
public static HashMap<String, LinkedList<String>> generateFinalDeploySolution(AbstractOrbacPolicy p, LinkedList<VM> VMList, LinkedList<HOST> HOSTList, LinkedList<LinkedList<LinkedList<String>>> concreteSeparationPolicy) throws COrbacException { HashMap<String, LinkedList<String>> finalDeploySolution = new HashMap<String, LinkedList<String>>(); LinkedList<String> VMAlreadyAllocateList = new LinkedList<String>(); Set concretePermissionList = p.GetConcretePermissions(); Iterator iter = concretePermissionList.iterator(); while (iter.hasNext()) { CConcretePermission Cpermission = (CConcretePermission) iter.next(); String currentVMID = Cpermission.GetObject(); //System.out.println("Preempted"+Cpermission.GetName()); if ((Cpermission.IsPreempted() == null) && !(VMAlreadyAllocateList.contains(currentVMID))) { VM currentVM = getVMByID(currentVMID, VMList); LinkedList<String> HOSTIDListForCurrentVMID = getAllPermitHostIDInPolicy(currentVMID, p); /* printInfo("HOSTIDListForCurrentVMID"); printLinkedList(HOSTIDListForCurrentVMID);*/ HOSTIDListForCurrentVMID = rankingHostlistFromCheapToHighAndReturnHOSTIDList( HOSTIDListForCurrentVMID, HOSTList); for (String currentHOSTSolutionID : HOSTIDListForCurrentVMID) {// www . j a v a 2 s. c om HOST currentHOST = getHOSTByID(currentHOSTSolutionID, HOSTList); if (!VMIDInSeparationPolicy(finalDeploySolution, currentHOSTSolutionID, currentVMID, concreteSeparationPolicy) && currentHOSTSatisfyCurrentVMForCapacity(currentHOST, currentVM) && !(VMAlreadyAllocateList.contains(currentVMID))) { if (finalDeploySolution.containsKey(currentHOSTSolutionID)) { LinkedList<String> currentVMSolution = finalDeploySolution.get(currentHOSTSolutionID); currentVMSolution.add(currentVMID); finalDeploySolution.replace(currentVMID, currentVMSolution); } else { LinkedList<String> currentVMSolution = new LinkedList<String>(); currentVMSolution.add(currentVMID); finalDeploySolution.put(currentHOSTSolutionID, currentVMSolution); } HOSTList = recalculateSpaceForHOSTList(HOSTList, currentHOSTSolutionID, currentVM); VMAlreadyAllocateList.add(currentVMID); } } } } /* printInfo("seperation policy"); printLinkedList(concreteSeparationPolicy); printInfo("Host list after allocation"); for (HOST eachHOST: HOSTList) { eachHOST.printInfo(); } */ return finalDeploySolution; }
From source file:com.fpuna.preproceso.PreprocesoTS.java
public static HashMap<String, SessionTS> leerBDtrainingSet(String BD, String sensor) { HashMap<String, SessionTS> SessionsTotal = new HashMap<String, SessionTS>(); String Consulta;/*from w ww . j a v a 2 s .c o m*/ Connection c = null; Statement stmt = null; Registro reg = new Registro(); Consulta = "SELECT dat.statusId, dat.sensorName, dat.\"value\", dat.\"timestamp\", lb.\"name\"\n" + "FROM sensor_data AS dat, status AS st, label AS lb\n" + "WHERE dat.statusId = st.\"_id\" AND st.labelId = lb.\"_id\" ORDER BY dat.\"timestamp\""; try { Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:" + path + "sensor.db"); c.setAutoCommit(false); System.out.println("Opened database successfully"); stmt = c.createStatement(); ResultSet rs = stmt.executeQuery(Consulta); while (rs.next()) { reg = new Registro(); reg.setSensor(rs.getString("sensorName")); reg.setTiempo(rs.getTimestamp("timestamp")); String[] values = (rs.getString("value")).split("\\,"); if (values.length == 3) { reg.setValor_x(Double.parseDouble(values[0].substring(1))); reg.setValor_y(Double.parseDouble(values[1])); reg.setValor_z(Double.parseDouble(values[2].substring(0, values[2].length() - 1))); } else if (values.length == 5) { reg.setValor_x(Double.parseDouble(values[0].substring(1))); reg.setValor_y(Double.parseDouble(values[1])); reg.setValor_z(Double.parseDouble(values[2])); reg.setM_1(Double.parseDouble(values[3])); reg.setM_2(Double.parseDouble(values[4].substring(0, values[4].length() - 1))); } if (SessionsTotal.containsKey(rs.getString("statusId"))) { SessionTS s = SessionsTotal.get(rs.getString("statusId")); s.addRegistro(reg); SessionsTotal.replace(rs.getString("statusId"), s); } else { SessionTS s = new SessionTS(); s.setActividad(rs.getString("name")); s.addRegistro(reg); SessionsTotal.put(rs.getString("statusId"), s); } } rs.close(); stmt.close(); c.close(); } catch (ClassNotFoundException | SQLException | NumberFormatException e) { System.err.println("Okapu:" + e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } System.out.println("Operation done successfully"); return SessionsTotal; }
From source file:com.fpuna.preproceso.PreprocesoTS.java
/** * Metodo estatico que lee el archivo y lo carga en una estructura de hash * * @param Archivo path del archivo//from w ww . j a v a2 s . co m * @return Hash con las sessiones leida del archivo de TrainigSet */ public static HashMap<String, SessionTS> leerArchivo(String Archivo, String sensor) { HashMap<String, SessionTS> SessionsTotal = new HashMap<String, SessionTS>(); HashMap<String, String> actividades = new HashMap<String, String>(); Path file = Paths.get(Archivo); if (Files.exists(file) && Files.isReadable(file)) { try { BufferedReader reader = Files.newBufferedReader(file, Charset.defaultCharset()); String line; int cabecera = 0; while ((line = reader.readLine()) != null) { if (line.contentEquals("statusId | label")) { //Leo todos las actividades while ((line = reader.readLine()) != null && !line.contentEquals("statusId|sensorName|value|timestamp")) { String part[] = line.split("\\|"); actividades.put(part[0], part[1]); SessionTS s = new SessionTS(); s.setActividad(part[1]); SessionsTotal.put(part[0], s); } line = reader.readLine(); } String lecturas[] = line.split("\\|"); if (lecturas[1].contentEquals(sensor)) { Registro reg = new Registro(); reg.setSensor(lecturas[1]); String[] values = lecturas[2].split("\\,"); if (values.length == 3) { reg.setValor_x(Double.parseDouble(values[0].substring(1))); reg.setValor_y(Double.parseDouble(values[1])); reg.setValor_z(Double.parseDouble(values[2].substring(0, values[2].length() - 1))); } else if (values.length == 5) { reg.setValor_x(Double.parseDouble(values[0].substring(1))); reg.setValor_y(Double.parseDouble(values[1])); reg.setValor_z(Double.parseDouble(values[2])); reg.setM_1(Double.parseDouble(values[3])); reg.setM_2(Double.parseDouble(values[4].substring(0, values[4].length() - 1))); } reg.setTiempo(new Timestamp(Long.parseLong(lecturas[3]))); SessionTS s = SessionsTotal.get(lecturas[0]); s.addRegistro(reg); SessionsTotal.replace(lecturas[0], s); } } } catch (IOException ex) { System.err.println("Okapu"); Logger.getLogger(PreprocesoTS.class.getName()).log(Level.SEVERE, null, ex); } } return SessionsTotal; }
From source file:com.fpuna.preproceso.PreprocesoTS.java
/** * Metodo estatico que lee el archivo y lo carga en una estructura de hash * * @param Archivo path del archivo//from ww w .j ava 2 s. c om * @return Hash con las sessiones leida del archivo de TrainigSet */ public static HashMap<String, SessionTS> leerArchivos(String Archivo, String sensor) { HashMap<String, SessionTS> SessionsTotal = new HashMap<String, SessionTS>(); HashMap<String, String> actividades = new HashMap<String, String>(); String sDirectorio = path; File dirList = new File(sDirectorio); if (dirList.exists()) { // Directorio existe File[] ficheros = dirList.listFiles(); for (int x = 0; x < ficheros.length; x++) { Path file = Paths.get(path + ficheros[x].getName()); if (Files.exists(file) && Files.isReadable(file)) { try { BufferedReader reader = Files.newBufferedReader(file, Charset.defaultCharset()); String line; int cabecera = 0; while ((line = reader.readLine()) != null) { if (line.contentEquals("statusId | label")) { //Leo todos las actividades while ((line = reader.readLine()) != null && !line.contentEquals("statusId|sensorName|value|timestamp")) { String part[] = line.split("\\|"); actividades.put(part[0], part[1]); SessionTS s = new SessionTS(); s.setActividad(part[1]); SessionsTotal.put(part[0], s); } line = reader.readLine(); } String lecturas[] = line.split("\\|"); if (lecturas[1].contentEquals(sensor)) { Registro reg = new Registro(); reg.setSensor(lecturas[1]); String[] values = lecturas[2].split("\\,"); if (values.length == 3) { reg.setValor_x(Double.parseDouble(values[0].substring(1))); reg.setValor_y(Double.parseDouble(values[1])); reg.setValor_z( Double.parseDouble(values[2].substring(0, values[2].length() - 1))); } else if (values.length == 5) { reg.setValor_x(Double.parseDouble(values[0].substring(1))); reg.setValor_y(Double.parseDouble(values[1])); reg.setValor_z(Double.parseDouble(values[2])); reg.setM_1(Double.parseDouble(values[3])); reg.setM_2(Double.parseDouble(values[4].substring(0, values[4].length() - 1))); } reg.setTiempo(new Timestamp(Long.parseLong(lecturas[3]))); SessionTS s = SessionsTotal.get(lecturas[0]); s.addRegistro(reg); SessionsTotal.replace(lecturas[0], s); } } } catch (IOException ex) { System.err.println("Okapu"); Logger.getLogger(PreprocesoTS.class.getName()).log(Level.SEVERE, null, ex); } } } } else { //Directorio no existe } return SessionsTotal; }
From source file:SeedGenerator.MainForm.java
private void calculateEndpointWordsTF() { try {/*from ww w.j a v a 2 s .co m*/ try { PreparedStatement createtablepstmt = con .prepareStatement("CREATE TABLE `recommender_endpoints_words_tf` (\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " `word` LONGTEXT DEFAULT NULL,\n" + " `endpointid` int(11) DEFAULT NULL,\n" + " `queryid` int(11) DEFAULT NULL,\n" + " `count` int(11) DEFAULT NULL,\n" + " `idf` int(11) DEFAULT NULL,\n" + " `idfid` int(11) DEFAULT NULL,\n" + " `totalNumberofWords` int(11) DEFAULT NULL,\n" + " PRIMARY KEY (`id`),\n" + " KEY `idf` (`idfid`),\n" + " KEY `endpointid` (`endpointid`)\n" + ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;"); createtablepstmt.execute(); } catch (Exception ex) { } PreparedStatement pstmtendpoints = con.prepareStatement( "SELECT * from crawler.endpoints where sourceCode is not null and sourceCode NOT LIKE 'EMPT%' and source != 'spendold' and source != 'spendnew' group by domain ORDER BY id asc;"); ResultSet rs1 = pstmtendpoints.executeQuery(); String htmlsource = ""; HashMap<String, Integer> wordCount = new HashMap(); while (rs1.next()) { HashMap<String, Integer> localwordCount = new HashMap(); htmlsource = rs1.getString("sourceCode"); String words[] = htmlsource.split("\\s+"); for (String word : words) { String cleanword; cleanword = word.replaceAll("[^\\p{L}\\p{Nd}]+", ""); if (!cleanword.equals("")) { if (!word.equals(cleanword)) { word = cleanword;//System.out.println(word+"--"+cleanword); } word = word.toLowerCase().replace("", "i"); if (wordCount.containsKey(word) && !localwordCount.containsKey(word)) { wordCount.replace(word, wordCount.get(word) + 1); } else if (!wordCount.containsKey(word)) { wordCount.put(word, 1); } if (localwordCount.containsKey(word)) { // wordCount.replace(word, wordCount.get(word) + 1); } else { localwordCount.put(word, 1); } } else { } } } pstmtendpoints.close(); rs1.close(); Iterator it = wordCount.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); if (Integer.parseInt(pair.getValue().toString()) > 1) { PreparedStatement insertpstmt = con.prepareStatement( "insert into recommender_endpoints_words_tf (word,count) values(?,?);"); if (pair.getKey().toString().length() > 44) { insertpstmt.setString(1, pair.getKey().toString()); } else { insertpstmt.setString(1, pair.getKey().toString()); } insertpstmt.setInt(2, Integer.parseInt(pair.getValue().toString())); insertpstmt.executeUpdate(); insertpstmt.close(); } it.remove(); // avoids a ConcurrentModificationException } } catch (Exception e) { //System.err.println("Got an exception! "); System.err.println(e.getMessage()); } }
From source file:SeedGenerator.MainForm.java
private void calculateTF(int queryid, String objectName) { try {/* w w w.ja v a2 s . c o m*/ try { PreparedStatement createtablepstmt = con.prepareStatement("CREATE TABLE `recommender_" + objectName + "_tf` (\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " `word` varchar(45) DEFAULT NULL,\n" + " `endpointid` int(11) DEFAULT NULL,\n" + " `queryid` int(11) DEFAULT NULL,\n" + " `count` int(11) DEFAULT NULL,\n" + " `idf` int(11) DEFAULT NULL,\n" + " `idfid` int(11) DEFAULT NULL,\n" + " `totalNumberofWords` int(11) DEFAULT NULL,\n" + " PRIMARY KEY (`id`),\n" + " KEY `word` (`word`),\n" + " KEY `idf` (`idfid`),\n" + " KEY `endpointid` (`endpointid`)\n" + ") ENGINE=InnoDB AUTO_INCREMENT=1261099 DEFAULT CHARSET=utf8;"); createtablepstmt.execute(); } catch (Exception ex) { } PreparedStatement pstmtendpoints = con .prepareStatement("select id, endpointurl from endpoints where tfidfprocessed is null;"); ResultSet rs1 = pstmtendpoints.executeQuery(); int endpointid = 0; while (rs1.next()) { endpointid = rs1.getInt("id"); PreparedStatement pstmt = con .prepareStatement("select s,p,o from endpointtriples where queryid=? and endpointid=?;"); pstmt.setInt(1, queryid); pstmt.setInt(2, endpointid); // execute the query, and get a java resultset ResultSet rs = pstmt.executeQuery(); ; // iterate through the java resultset HashMap<String, Integer> wordCount = new HashMap(); int totalNumberOfWords = 0; while (rs.next()) { String s = rs.getString("s"); String p = rs.getString("p"); String o = rs.getString("o"); if (o != null) { String words[] = o.split(" "); for (String word : words) { String cleanword; cleanword = word.replaceAll("[^\\p{L}\\p{Nd}]+", ""); if (!cleanword.equals("")) { if (!word.equals(cleanword)) { word = cleanword;//System.out.println(word+"--"+cleanword); } word = word.toLowerCase().replace("", "i"); if (wordCount.containsKey(word)) { wordCount.replace(word, wordCount.get(word) + 1); } else { wordCount.put(word, 1); } totalNumberOfWords++; } else { } } } } pstmt.close(); rs.close(); Iterator it = wordCount.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); if (Integer.parseInt(pair.getValue().toString()) > 1) { PreparedStatement insertpstmt = con.prepareStatement("insert into recommender_" + objectName + "_tf (word,count,endpointid,queryid,totalNumberofWords) values(?,?,?,?,?);"); if (pair.getKey().toString().length() > 44) { insertpstmt.setString(1, pair.getKey().toString().substring(0, 44)); } else { insertpstmt.setString(1, pair.getKey().toString()); } insertpstmt.setInt(2, Integer.parseInt(pair.getValue().toString())); insertpstmt.setInt(3, endpointid); insertpstmt.setInt(4, queryid); insertpstmt.setInt(5, totalNumberOfWords); insertpstmt.executeUpdate(); insertpstmt.close(); } it.remove(); // avoids a ConcurrentModificationException } PreparedStatement updatepstmt = con .prepareStatement("update endpoints set tfidfprocessed=1 where id=?;"); // updatepstmt.setInt(1, totalNumberOfWords); updatepstmt.setInt(1, endpointid); updatepstmt.executeUpdate(); updatepstmt.close(); } pstmtendpoints.close(); } catch (Exception e) { //System.err.println("Got an exception! "); System.err.println(e.getMessage()); } }
From source file:SeedGenerator.MainForm.java
private void btnSameAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSameAsActionPerformed try {// w ww. j a v a2 s . co m PreparedStatement pstmtendpoints = con .prepareStatement("select id, endpointurl from endpoints where outLinksCount is null;"); ResultSet rs1 = pstmtendpoints.executeQuery(); int endpointid = 0; int queryid = 23;//sameas ext while (rs1.next()) { endpointid = rs1.getInt("id"); int hashTotalCount = 0; PreparedStatement pstmt = con .prepareStatement("select s,p,o from endpointtriples where queryid=? and endpointid=?;"); pstmt.setInt(1, queryid); pstmt.setInt(2, endpointid); // execute the query, and get a java resultset ResultSet rs = pstmt.executeQuery(); ; // iterate through the java resultset HashMap<String, Integer> hash = new HashMap(); int totalNumberOfWords = 0; while (rs.next()) { String s = rs.getString("s"); String p = rs.getString("p"); String o = rs.getString("o"); if (o != null) { String host = ""; try { URL u = new URL(o); host = u.getHost(); if (InternetDomainName.isValid(host)) { host = InternetDomainName.from(host).topPrivateDomain().toString(); } else { if (com.google.common.net.HostSpecifier.isValid(host)) { host = com.google.common.net.HostSpecifier.from(host).toString(); } else { host = ""; } } } catch (Exception ex) { } if (hash.containsKey(host)) { hash.replace(host, hash.get(host) + 1); } else { hash.put(host, 1); } } //String firstName = rs.getString("url"); } pstmt.close(); rs.close(); Iterator it = hash.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); //<> if (Integer.parseInt(pair.getValue().toString()) > 1) { PreparedStatement insertpstmt = con.prepareStatement( "insert into recommender_sameas_pldcount (pld,endpointid,count,queryid) values(?,?,?,?);"); if (pair.getKey().toString().length() > 44) { insertpstmt.setString(1, pair.getKey().toString().substring(0, 44)); } else { insertpstmt.setString(1, pair.getKey().toString()); } insertpstmt.setInt(3, Integer.parseInt(pair.getValue().toString())); insertpstmt.setInt(2, endpointid); insertpstmt.setInt(4, queryid); insertpstmt.executeUpdate(); } it.remove(); // avoids a ConcurrentModificationException } PreparedStatement updatepstmt = con .prepareStatement("update endpoints set outLinksCount=? where id=?;"); updatepstmt.setInt(1, hash.size()); updatepstmt.setInt(2, endpointid); updatepstmt.executeUpdate(); } } catch (Exception e) { //System.err.println("Got an exception! "); System.err.println(e.getMessage()); } // TODO add your handling code here: }
From source file:SeedGenerator.MainForm.java
private void calculateIDF(int queryid, String objectName) { try {//from www . j a v a 2s . co m try { PreparedStatement createtablepstmt = con.prepareStatement("CREATE TABLE `recommender_" + objectName + "_idf` (\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " `word` varchar(45) DEFAULT NULL,\n" + " `queryid` int(11) DEFAULT NULL,\n" + " `count` int(11) DEFAULT NULL,\n" + " `wordnetProcessed` int(11) DEFAULT NULL,\n" + " `wordnetHypernym` varchar(45) DEFAULT NULL,\n" + " PRIMARY KEY (`id`),\n" + " KEY `word` (`word`),\n" + " KEY `wordnetHypernym` (`wordnetHypernym`)\n" + ") ENGINE=InnoDB AUTO_INCREMENT=1695533 DEFAULT CHARSET=utf8;"); createtablepstmt.execute(); } catch (Exception ex) { } PreparedStatement pstmt = con.prepareStatement( "select s,p,o, endpointid from endpointtriples where queryid=? order by endpointid;"); pstmt.setInt(1, queryid); // //update recommender_comment_tf set idfid = (select id from recommender_comment_idf //where word = recommender_comment_tf.word limit 1) where id>0 and idfid is null limit 500000; // execute the query, and get a java resultset ResultSet rs = pstmt.executeQuery(); ; // iterate through the java resultset HashMap<String, Integer> wordCount = new HashMap(); HashMap<String, Integer> localwordCount = new HashMap(); int totalNumberOfWords = 0; int endpointid = 0; while (rs.next()) { int currentEndpointid = rs.getInt("endpointid"); String s = rs.getString("s"); String p = rs.getString("p"); String o = rs.getString("o"); if (endpointid != currentEndpointid) { localwordCount.clear(); endpointid = currentEndpointid; } if (o != null) { String words[] = o.split(" "); for (String word : words) { String cleanword; cleanword = word.replaceAll("[^\\p{L}\\p{Nd}]+", ""); if (!cleanword.equals("")) { if (!word.equals(cleanword)) { word = cleanword;//System.out.println(word+"--"+cleanword); } word = word.toLowerCase().replace("", "i"); if (wordCount.containsKey(word)) { if (!localwordCount.containsKey(word)) { wordCount.replace(word, wordCount.get(word) + 1); } } else { wordCount.put(word, 1); } if (!localwordCount.containsKey(word)) { localwordCount.put(word, 1); } totalNumberOfWords++; } else { // System.out.println("bos"); } } } //String firstName = rs.getString("url"); } pstmt.close(); rs.close(); Iterator it = wordCount.entrySet().iterator(); PreparedStatement insertpstmt = con.prepareStatement( "insert into recommender_" + objectName + "_idf (word,count,queryid) values(?,?,?);"); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); //<> if (Integer.parseInt(pair.getValue().toString()) > 0) { if (pair.getKey().toString().length() > 44) { insertpstmt.setString(1, pair.getKey().toString().substring(0, 44)); } else { insertpstmt.setString(1, pair.getKey().toString()); } insertpstmt.setInt(2, Integer.parseInt(pair.getValue().toString())); insertpstmt.setInt(3, 24); insertpstmt.addBatch(); } it.remove(); // avoids a ConcurrentModificationException } // pstmt.close(); // rs.close(); // updatepstmt.executeBatch(); insertpstmt.executeBatch(); PreparedStatement s = con.prepareStatement( "SET SQL_SAFE_UPDATES = 0; " + "UPDATE recommender_" + objectName + "_tf as t, recommender_" + objectName + "_idf as i " + "SET t.idfid = i.id " + "where t.word=i.word;"); s.executeQuery(); } catch (Exception e) { //System.err.println("Got an exception! "); System.err.println(e.getMessage()); } }