/** * Serializes the graph to the given destination. * @param destination The destination to which should be saved. * @throws WikiApiException Thrown if errors occurred. */ public void saveGraph(String destination) throws WikiApiException { try { GraphSerialization.saveGraph(graph, destination); } catch (IOException e) { throw new WikiApiException(e); } }
/** * Get infos only for a subset of articles. * @param pPages A set of pages. Only this subset of wiki pages is used in the info object. */ public WikipediaInfo(Iterable<Page> pPages) throws WikiApiException { if (pPages == null) { throw new WikiApiException("The page set has to be initialized."); } pages = pPages; averageFanOut = -1.0; // lazy initialization => it is computed and stored when it is accessed degreeDistribution = new HashMap<Integer,Integer>(); categorizedArticleSet = new HashSet<Integer>(); // get number of pages numberOfPages = 0; while (pages.iterator().hasNext()) { numberOfPages++; pages.iterator().next(); } }
public int getTaxonomicallyBoundPathLengthInNodes(Category cat1, Category cat2) throws WikiApiException { int retValue = getTaxonomicallyBoundPathLengthInEdges(cat1, cat2); if (retValue == 0) { return 0; } else if (retValue > 0) { return (--retValue); } else if (retValue == -1) { return -1; } else { throw new WikiApiException("Unknown return value."); } }
/** * Creates an {@link CategoryGraph} using a serialized DirectedGraph object. * @param pWiki A {@link Wikipedia} object. * @param location The location of the serialized graph * @throws WikiApiException Thrown if errors occurred. */ public CategoryGraph(Wikipedia pWiki, File location) throws WikiApiException{ try { constructCategoryGraph(pWiki, GraphSerialization.loadGraph(location)); } catch (IOException e) { throw new WikiApiException(e); } catch (ClassNotFoundException e) { throw new WikiApiException(e); } }
/** * Gets the path length between two category nodes - measured in "nodes". * @param node1 The first node. * @param node2 The second node. * @return The number of nodes of the path between node1 and node2. 0, if the nodes are identical or neighbors. -1, if no path exists. */ public int getPathLengthInNodes(Category node1, Category node2) throws WikiApiException { int retValue = getPathLengthInEdges(node1, node2); if (retValue == 0) { return 0; } else if (retValue > 0) { return (--retValue); } else if (retValue == -1) { return -1; } else { throw new WikiApiException("Unknown return value."); } }
private static CategoryGraph tryToLoadCategoryGraph(Wikipedia wiki, String wikiId, String size) throws WikiApiException { String defaultSerializedGraphLocation = getCategoryGraphSerializationFileName(wikiId, size); File defaulSerializedGraphFile = new File(defaultSerializedGraphLocation); if (defaulSerializedGraphFile.exists()) { try { logger.info("Loading category graph from " + defaultSerializedGraphLocation); return new CategoryGraph(wiki, GraphSerialization.loadGraph(defaultSerializedGraphLocation)); } catch (IOException e) { throw new WikiApiException(e); } catch (ClassNotFoundException e) { throw new WikiApiException(e); } } else { return null; } }
private Connection getConnection(RevisionAPIConfiguration config) throws WikiApiException { Connection c; try { String driverDB = "com.mysql.jdbc.Driver"; Class.forName(driverDB); c = DriverManager.getConnection( "jdbc:mysql://" + config.getHost() + "/" + config.getDatabase(), config.getUser(), config.getPassword()); if (!c.isValid(5)) { throw new WikiApiException("Connection could not be established."); } } catch (SQLException e) { throw new WikiApiException(e); } catch (ClassNotFoundException e) { throw new WikiApiException(e); } return c; }
private Connection getConnection(Wikipedia wiki) throws WikiApiException { DatabaseConfiguration config = wiki.getDatabaseConfiguration(); Connection c; try { String driverDB = "com.mysql.jdbc.Driver"; Class.forName(driverDB); c = DriverManager.getConnection("jdbc:mysql://" + config.getHost() + "/" + config.getDatabase()+"?autoReconnect=true", config.getUser(), config.getPassword()); if (!c.isValid(5)) { throw new WikiApiException( "Connection could not be established."); } } catch (SQLException e) { throw new WikiApiException(e); } catch (ClassNotFoundException e) { throw new WikiApiException(e); } return c; }
/** * Helper method to obtain a connection via the given {@link RevisionAPIConfiguration} parameter. * @param config Must not be {@code null}. * @return A valid {@link Connection} to the database endpoint. * @throws WikiApiException Thrown if errors occurred while opening a connection. */ protected Connection getConnection(RevisionAPIConfiguration config) throws WikiApiException { Connection c; try { String driverDB = config.getDatabaseDriver(); Class.forName(driverDB); c = DriverManager.getConnection(config.getJdbcURL(), config.getUser(), config.getPassword()); if (!c.isValid(5)) { throw new WikiApiException("Connection could not be established."); } } catch (SQLException | ClassNotFoundException e) { throw new WikiApiException(e); } return c; }
private static void saveCategoryGraph(CategoryGraph catGraph, String wikiId, String size) throws WikiApiException { String defaultSerializedGraphLocation = getCategoryGraphSerializationFileName(wikiId, size); try { logger.info("Saving category graph to " + defaultSerializedGraphLocation); GraphSerialization.saveGraph(catGraph.getGraph(), defaultSerializedGraphLocation); } catch (IOException e) { throw new WikiApiException(e); } }
private int[] getCachedInlinkIds(Page article) throws WikiApiException { if (cache.isCacheEmpty()) { try { cache.fillInLinkCache(); } catch (FileNotFoundException e) { e.printStackTrace(); throw new WikiApiException(e); } catch (IOException e) { e.printStackTrace(); throw new WikiApiException(e); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new WikiApiException(e); } } if (cache.cachedInLinks.containsKey(article.getPageId())) { return cache.cachedInLinks.get(article.getPageId()) ; } else { return new int[0] ; } }
private int[] getCachedInlinkIds(Page article) throws WikiApiException { if (cache.isCacheEmpty()) { try { cache.fillInLinkCache(); } catch (FileNotFoundException e) { e.printStackTrace(); throw new WikiApiException(e); } catch (IOException e) { e.printStackTrace(); throw new WikiApiException(e); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new WikiApiException(e); } } if (cache.cachedInLinks.containsKey(article.getPageId())) { return cache.cachedInLinks.get(article.getPageId()) ; } else { return new int[0] ; } }
/** * (Constructor) Creates a new RevisionIterator object. * * @param config * Reference to the configuration object * * @throws WikiApiException * if an error occurs */ public RevisionIterator(final RevisionAPIConfiguration config) throws WikiApiException { this.config = config; try { this.primaryKey = -1; this.endPK = Integer.MAX_VALUE; this.statement = null; this.result = null; this.previousRevision = null; MAX_NUMBER_RESULTS = config.getBufferSize(); connect(); } catch (SQLException e) { throw new WikiApiException(e); } }
/** * (Constructor) Creates the IndexIterator object. * * @param config * Reference to the configuration * * @throws WikiApiException * if an error occurs */ public IndexIterator(final RevisionAPIConfiguration config) throws WikiApiException { try { this.primaryKey = -1; this.statement = null; this.result = null; String driverDB = "com.mysql.jdbc.Driver"; Class.forName(driverDB); MAX_NUMBER_RESULTS = config.getBufferSize(); this.connection = DriverManager.getConnection("jdbc:mysql://" + config.getHost() + "/" + config.getDatabase(), config.getUser(), config.getPassword()); } catch (SQLException e) { throw new WikiApiException(e); } catch (ClassNotFoundException e) { throw new WikiApiException(e); } }
/** * Intrinsic information content (Seco Etal. 2004) allows to compute information content from the structure of the taxonomy (no corpus needed). * IC(n) = 1 - log( hypo(n) + 1) / log(#cat) * hypo(n) is the (recursive) number of hyponyms of a node n. Recursive means that the hyponyms of hyponyms are also taken into account * #cat is the number of categories in the graph * @param category The category node for which the intrinsic information content should be returned. * @return The intrinsic information content for this category node. * @throws WikiApiException Thrown if errors occurred. */ public double getIntrinsicInformationContent(Category category) throws WikiApiException { int node = category.getPageId(); int hyponymCount = getHyponymCountMap().get(node); int numberOfNodes = this.getNumberOfNodes(); if (hyponymCount > numberOfNodes) { throw new WikiApiException("Something is wrong with the hyponymCountMap. " + hyponymCount + " hyponyms, but only " + numberOfNodes + " nodes."); } logger.debug(category.getTitle().getPlainTitle() + " has # hyponyms: " + hyponymCount); double intrinsicIC = -1; if (hyponymCount >= 0) { intrinsicIC = (1 - ( Math.log(hyponymCount + 1) / Math.log(numberOfNodes) ) ); } return intrinsicIC; }
/** * (Constructor) Creates the IndexIterator object. * * @param config * Reference to the configuration * * @throws WikiApiException * if an error occurs */ public IndexIterator(final RevisionAPIConfiguration config) throws WikiApiException { try { this.primaryKey = -1; this.statement = null; this.result = null; String driverDB = "com.mysql.jdbc.Driver"; Class.forName(driverDB); MAX_NUMBER_RESULTS = config.getBufferSize(); this.connection = DriverManager.getConnection("jdbc:mysql://" + config.getHost() + "/" + config.getDatabase(), config.getUser(), config.getPassword()); } catch (SQLException e) { throw new WikiApiException(e); } catch (ClassNotFoundException e) { throw new WikiApiException(e); } }
throw new WikiApiException(e); throw new WikiApiException(e);
throw new WikiApiException(sql); throw new WikiApiException(sql);
/** * a Returns CompiledPage produced by the SWEBLE parser using the SimpleWikiConfiguration. * * @return the parsed page */ private CompiledPage getCompiledPage(Page page) throws WikiApiException { CompiledPage cp; try { PageTitle pageTitle = PageTitle.make(config, page.getTitle()); PageId pageId = new PageId(pageTitle, -1); // Compile the retrieved page cp = compiler.postprocess(pageId, page.getCurrentContent(), null); } catch (Exception e) { throw new WikiApiException(e); } return cp; } }
/** * Returns CompiledPage produced by the SWEBLE parser using the SimpleWikiConfiguration. * * @return the parsed page * @throws WikiApiException Thrown if errors occurred. */ private EngProcessedPage getCompiledPage() throws WikiApiException { EngProcessedPage cp; try{ WtEngineImpl engine = new WtEngineImpl(this.wiki.getWikConfig()); PageTitle pageTitle = PageTitle.make(this.wiki.getWikConfig(), this.getTitle().toString()); PageId pageId = new PageId(pageTitle, -1); // Compile the retrieved page cp = engine.postprocess(pageId, this.getText(), null); } catch(Exception e){ throw new WikiApiException(e); } return cp; }