jobimtext.thesaurus.distributional.DatabaseThesaurusDatastructure.java Source code

Java tutorial

Introduction

Here is the source code for jobimtext.thesaurus.distributional.DatabaseThesaurusDatastructure.java

Source

package jobimtext.thesaurus.distributional;

/*******************************************************************************
 * Copyright 2012
 * Technische Universitt Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
import java.io.File;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.iterators.ArrayListIterator;

import jobimtext.util.db.DatabaseResource;
import jobimtext.util.db.conf.SqlThesaurusConfiguration;
import jobimtext.util.tunnel.TunnelConfiguration;

/**
 * 
 * @author Martin Riedl (riedl@cs.tu-darmstadt.de)
 * 
 */
public class DatabaseThesaurusDatastructure extends DatabaseResource
        implements IThesaurusDatastructure<String, String> {

    private String tableOrder2;
    private String tableOrder1;
    private String tableValues;
    private String tableKey;

    public DatabaseThesaurusDatastructure(File dbConfigurationFile) {
        this.setDbConfigurationFile(dbConfigurationFile);
    }

    public DatabaseThesaurusDatastructure(SqlThesaurusConfiguration conf) {
        super.setDbConf(conf);
    }

    public DatabaseThesaurusDatastructure(SqlThesaurusConfiguration dbConf, TunnelConfiguration tunnelConf) {
        super.setDbConf(dbConf);
        super.setTunnelConf(tunnelConf);
    }

    public DatabaseThesaurusDatastructure(String dbConfigurationFile) {
        this(new File(dbConfigurationFile));
    }

    public DatabaseThesaurusDatastructure(File dbConfigurationFile, File tunnelConfigurationFile) {
        this.setDbConfigurationFile(dbConfigurationFile);
        this.setTunnelConfigurationFile(tunnelConfigurationFile);
    }

    public DatabaseThesaurusDatastructure() {

    }

    @Override
    public boolean connect() {
        boolean value = super.connect();
        tableOrder2 = getDatabaseConfiguration().getTableOrder2();
        tableOrder1 = getDatabaseConfiguration().getTableOrder1();
        tableValues = getDatabaseConfiguration().getTableValues();
        tableKey = getDatabaseConfiguration().getTableKey();
        return value;
    }

    public List<Order2> getExpansions(String key) {
        try {
            String sql = "SELECT word1, word2,count FROM " + tableOrder2 + " WHERE word1 = ? ORDER BY count desc";
            PreparedStatement ps = getDatabaseConnection().getConnection().prepareStatement(sql);
            ps.setString(1, key);
            return fillExpansions(ps);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Order2> getExpansions(String key, int numberOfEntries) {
        try {

            String sql = "SELECT word1, word2,count FROM " + tableOrder2
                    + " WHERE word1 = ? ORDER BY count desc LIMIT 0," + numberOfEntries;
            PreparedStatement ps = getDatabaseConnection().getConnection().prepareStatement(sql);
            ps.setString(1, key);

            return fillExpansions(ps);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Order2> getExpansions(String key, double threshold) {
        try {
            String sql = "SELECT word1, word2,count FROM " + tableOrder2
                    + " WHERE word1 = ? and count > ? ORDER BY count desc";
            PreparedStatement ps = getDatabaseConnection().getConnection().prepareStatement(sql);
            ps.setString(1, key);
            ps.setDouble(2, threshold);
            return fillExpansions(ps);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    private List<Order2> fillExpansions(PreparedStatement ps) throws SQLException {
        List<Order2> list = new ArrayList<Order2>();
        ResultSet set = ps.executeQuery();
        while (set.next()) {
            list.add(new Order2(set.getString("word2"), set.getDouble("count")));
        }
        ps.close();
        return list;
    }

    public Long getKeyCount(String key) {
        Long count = 0L;
        String sql = "SELECT count FROM " + tableKey + " WHERE word = ?";
        PreparedStatement ps;
        try {
            ps = getDatabaseConnection().getConnection().prepareStatement(sql);

            ps.setString(1, key);
            ResultSet set = ps.executeQuery();

            if (set.next()) {
                count = set.getLong("count");
            }
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public Long getValuesCount(String key) {
        Long count = 0L;
        String sql = "SELECT count FROM " + tableValues + " WHERE feature = ?";
        PreparedStatement ps;
        try {
            ps = getDatabaseConnection().getConnection().prepareStatement(sql);
            ps.setString(1, key);
            ResultSet set = ps.executeQuery();
            if (set.next()) {
                count = set.getLong("count");
            }
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public Double getKeyValuesScore(String key, String val) {
        double score = 0.0;
        String sql = "SELECT sig FROM " + tableOrder1 + " WHERE word = ? and feature = ?";
        PreparedStatement ps;
        try {
            ps = getDatabaseConnection().getConnection().prepareStatement(sql);
            ps.setString(1, key);
            ps.setString(2, val);
            ResultSet set = ps.executeQuery();
            if (set.next()) {
                score = set.getDouble("sig");
            }
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return score;
    }

    @Override
    public List<Order1> getKeyValuesScores(String key) {
        String sql = "SELECT feature, sig FROM " + tableOrder1 + " WHERE word = ? ORDER BY sig desc LIMIT 0,1000";
        List<Order1> list = fillKeyValuesScores(sql, key);
        return list;
    }

    private List<Order1> fillKeyValuesScores(String sql, String key) {
        PreparedStatement ps;
        List<Order1> list = new ArrayList<Order1>();
        try {
            ps = getDatabaseConnection().getConnection().prepareStatement(sql);

            ps.setString(1, key);
            ResultSet set = ps.executeQuery();
            while (set.next()) {
                list.add(new Order1(set.getString("feature"), set.getDouble("sig")));
            }
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Order1> getKeyValuesScores(String key, int numberOfEntries) {
        String sql = "SELECT feature, sig FROM " + tableOrder1 + " WHERE word = ? ORDER BY sig desc LIMIT 0,"
                + numberOfEntries;
        return fillKeyValuesScores(sql, key);
    }

    @Override
    public List<Order1> getKeyValuesScores(String key, double threshold) {
        String sql = "SELECT feature, sig FROM " + tableOrder1 + " WHERE word = ? AND sig> " + threshold
                + " ORDER BY sig desc ";
        return fillKeyValuesScores(sql, key);
    }

}