Created
December 14, 2014 11:56
-
-
Save cdwijayarathna/7550176443ad2229fae0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sinmin.api; | |
import com.datastax.driver.core.Cluster; | |
import com.datastax.driver.core.Host; | |
import com.datastax.driver.core.Metadata; | |
import com.datastax.driver.core.PreparedStatement; | |
import com.datastax.driver.core.ResultSet; | |
import com.datastax.driver.core.Row; | |
import com.datastax.driver.core.Session; | |
public class DbInterface { | |
private Cluster cluster; | |
private Session session; | |
public void connect(String node) { | |
cluster = Cluster.builder() | |
.addContactPoint(node).build(); | |
Metadata metadata = cluster.getMetadata(); | |
System.out.printf("Connected to cluster: %s\n", | |
metadata.getClusterName()); | |
for ( Host host : metadata.getAllHosts() ) { | |
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n", | |
host.getDatacenter(), host.getAddress(), host.getRack()); | |
} | |
session = cluster.connect(); | |
} | |
public void getWordFrequency(String word,int year, String category){ | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.word_time_category_frequency WHERE word=? AND year=? AND category=?"); | |
ResultSet results = session.execute(query.bind(word,year,category)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getWordFrequency(String word,int year){ | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.word_time_frequency WHERE word=? AND year=?"); | |
ResultSet results = session.execute(query.bind(word,year)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getWordFrequency(String word, String category){ | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.word_category_frequency WHERE word=? AND category=?"); | |
ResultSet results = session.execute(query.bind(word,category)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getWordFrequency(String word){ | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.word_frequency WHERE word=?"); | |
ResultSet results = session.execute(query.bind(word)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getBigramFrequency(String word1, String word2,int year, String category){ | |
System.out.println("getBigramFrequency(String word1, String word2,int year, String category)"); | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.bigram_time_category_frequency WHERE word1=? AND word2=? AND year=? AND category=?"); | |
ResultSet results = session.execute(query.bind(word1,word2,year,category)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getBigramFrequency(String word1, String word2,int year){ | |
System.out.println("getBigramFrequency(String word1, String word2,int year)"); | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.bigram_time_frequency WHERE word1=? AND word2=? AND year=?"); | |
ResultSet results = session.execute(query.bind(word1,word2,year)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getBigramFrequency(String word1, String word2, String category){ | |
System.out.println("getBigramFrequency(String word1, String word2,String category)"); | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.bigram_category_frequency WHERE word1=? AND word2=? AND category=?"); | |
ResultSet results = session.execute(query.bind(word1,word2,category)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getBigramFrequency(String word1, String word2){ | |
System.out.println("getBigramFrequency(String word1, String word2)"); | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.bigram_category_frequency WHERE word1=? AND word2=?"); | |
ResultSet results = session.execute(query.bind(word1,word2)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////////// | |
public void getTrigramFrequency(String word1, String word2,String word3,int year, String category){ | |
System.out.println("getTrigramFrequency(String word1, String word2,String word3,int year, String category)"); | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.trigram_time_category_frequency WHERE word1=? AND word2=? AND word3=? AND year=? AND category=?"); | |
ResultSet results = session.execute(query.bind(word1,word2,word3,year,category)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getTrigramFrequency(String word1, String word2, String word3,int year){ | |
System.out.println("getTrigramFrequency(String word1, String word2,String word3,int year)"); | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.trigram_time_frequency WHERE word1=? AND word2=? AND word3=? AND year=?"); | |
ResultSet results = session.execute(query.bind(word1,word2,word3,year)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getTrigramFrequency(String word1, String word2, String word3, String category){ | |
System.out.println("getTrigramFrequency(String word1, String word2,String word3,String category)"); | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.trigram_category_frequency WHERE word1=? AND word2=? AND word3=? AND category=?"); | |
ResultSet results = session.execute(query.bind(word1,word2,word3,category)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
public void getTrigramFrequency(String word1, String word2, String word3){ | |
System.out.println("getTrigramFrequency(String word1, String word2,String word3)"); | |
PreparedStatement query = session.prepare( | |
"select frequency from corpus.trigram_category_frequency WHERE word1=? AND word2=? AND word3=?"); | |
ResultSet results = session.execute(query.bind(word1,word2,word3)); | |
for (Row row : results) { | |
System.out.format("%d\n", row.getInt("frequency")); | |
} | |
} | |
////////////////////////////////////////////////////// | |
public void getFrequentWords(int year, String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_time_category_ordered_frequency WHERE year=? and category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(year,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s %d\n", row.getString("word"),row.getInt("frequency")); | |
} | |
} | |
public void getFrequentWords(int year,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_time_ordered_frequency WHERE year=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(year,amount)); | |
for (Row row : results) { | |
System.out.format("%s %d\n", row.getString("word"),row.getInt("frequency")); | |
} | |
} | |
public void getFrequentWords(String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_category_ordered_frequency WHERE category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(category,amount)); | |
for (Row row : results) { | |
System.out.format("%s %d\n", row.getString("word"),row.getInt("frequency")); | |
} | |
} | |
public void getFrequentWords(int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_category_ordered_frequency WHERE category IN (?,?,?,?,?) order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind("N","C","A","S","G",amount)); | |
for (Row row : results) { | |
System.out.format("%s %d\n", row.getString("word"),row.getInt("frequency")); | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////// | |
public void getFrequentBigrams(int year, String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.bigram_time_category_ordered_frequency WHERE year=? and category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(year,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s %s %d\n", row.getString("word1"),row.getString("word2"),row.getInt("frequency")); | |
} | |
} | |
public void getFrequentBigrams(int year,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.bigram_time_ordered_frequency WHERE year=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(year,amount)); | |
for (Row row : results) { | |
System.out.format("%s %s %d\n", row.getString("word1"),row.getString("word2"),row.getInt("frequency")); | |
} | |
} | |
public void getFrequentBigrams(String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.bigram_category_ordered_frequency WHERE category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(category,amount)); | |
for (Row row : results) { | |
System.out.format("%s %s %d\n", row.getString("word1"),row.getString("word2"),row.getInt("frequency")); | |
} | |
} | |
public void getFrequentBigrams(int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.bigram_category_ordered_frequency WHERE category IN (?,?,?,?,?) order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind("N","C","A","S","G",amount)); | |
for (Row row : results) { | |
System.out.format("%s %s %d\n", row.getString("word1"),row.getString("word2"),row.getInt("frequency")); | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////////////// | |
public void getFrequentTrigrams(int year, String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.trigram_time_category_ordered_frequency WHERE year=? and category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(year,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s %s %s %d\n", row.getString("word1"),row.getString("word2"),row.getString("word3"),row.getInt("frequency")); | |
} | |
} | |
public void getFrequentTrigrams(int year,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.trigram_time_ordered_frequency WHERE year=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(year,amount)); | |
for (Row row : results) { | |
System.out.format("%s %s %s %d\n", row.getString("word1"),row.getString("word2"),row.getString("word3"),row.getInt("frequency")); | |
} | |
} | |
public void getFrequentTrigrams(String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.trigram_category_ordered_frequency WHERE category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(category,amount)); | |
for (Row row : results) { | |
System.out.format("%s %s %s %d\n", row.getString("word1"),row.getString("word2"),row.getString("word3"),row.getInt("frequency")); | |
} | |
} | |
public void getFrequentTrigrams(int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.trigram_category_ordered_frequency WHERE category IN (?,?,?,?,?) order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind("N","C","A","S","G",amount)); | |
for (Row row : results) { | |
System.out.format("%s %s %s %d\n", row.getString("word1"),row.getString("word2"),row.getString("word3"),row.getInt("frequency")); | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////////////// | |
public void getLatestArticlesForWord(String word,int year, String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_year_category_usage WHERE word=? AND year=? AND category =? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word,year,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
public void getLatestArticlesForWord(String word,int year,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_year_usage WHERE word=? AND year=? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word,year,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
public void getLatestArticlesForWord(String word,String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_category_usage WHERE word=? AND category =? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
public void getLatestArticlesForWord(String word,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_usage WHERE word=? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
///////////////////////////////////////////////////////////////////////////////////////////// | |
public void getLatestArticlesForBigram(String word1, String word2,int year, String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.bigram_year_category_usage WHERE word1=? AND word2=? AND year=? AND category =? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word1,word2,year,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
public void getLatestArticlesForBigram(String word1, String word2,int year,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.bigram_year_usage WHERE word1=? AND word2=? AND year=? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word1,word2,year,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
public void getLatestArticlesForBigram(String word1,String word2,String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.bigram_category_usage WHERE word1=? AND word2=? AND category =? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word1,word2,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
public void getLatestArticlesForBigram(String word1,String word2,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.bigram_usage WHERE word1=? AND word2=? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word1,word2,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
///////////////////////////////////////////////////////////////////////////////// | |
public void getLatestArticlesForTrigram(String word1, String word2,String word3, int year, String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.trigram_year_category_usage WHERE word1=? AND word2=? AND word3=? AND year=? AND category =? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word1,word2,word3,year,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
public void getLatestArticlesForTrigram(String word1, String word2,String word3,int year,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.trigram_year_usage WHERE word1=? AND word2=? AND word3=? AND year=? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word1,word2,word3,year,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
public void getLatestArticlesForTrigram(String word1,String word2,String word3,String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.trigram_category_usage WHERE word1=? AND word2=? AND word3=? AND category =? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word1,word2,word3,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
public void getLatestArticlesForTrigram(String word1,String word2, String word3,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.trigram_usage WHERE word1=? AND word2=? AND word3=? order by date DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(word1,word2,word3,amount)); | |
for (Row row : results) { | |
System.out.format("%s. %s %s \n", row.getString("sentence"),row.getString("author"),row.getString("url")); | |
} | |
} | |
////////////////////////////////////////////////////////////////// | |
public void getFrequentWordsInPosition(int position,int year,String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_pos_year_category_frequency WHERE position=? AND year=? AND category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(position,year, category,amount)); | |
for (Row row : results) { | |
System.out.format("%s\n", row.getString("word")); | |
} | |
} | |
public void getFrequentWordsInPositionReverse(int position,int year,String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_inv_pos_year_category_frequency WHERE inv_position=? AND year=? AND category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(position,year, category,amount)); | |
for (Row row : results) { | |
System.out.format("%s\n", row.getString("word")); | |
} | |
} | |
//---------------- | |
public void getFrequentWordsInPosition(int position,int year,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_pos_year_frequency WHERE position=? AND year=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(position,year, amount)); | |
for (Row row : results) { | |
System.out.format("%s\n", row.getString("word")); | |
} | |
} | |
public void getFrequentWordsInPositionReverse(int position,int year,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_inv_pos_year_frequency WHERE inv_position=? AND year=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(position,year, amount)); | |
for (Row row : results) { | |
System.out.format("%s\n", row.getString("word")); | |
} | |
} | |
public void getFrequentWordsInPosition(int position,String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_pos_category_frequency WHERE position=? AND category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(position, category,amount)); | |
for (Row row : results) { | |
System.out.format("%s\n", row.getString("word")); | |
} | |
} | |
public void getFrequentWordsInPositionReverse(int position,String category,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_inv_pos_category_frequency WHERE inv_position=? AND category=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(position,category,amount)); | |
for (Row row : results) { | |
System.out.format("%s\n", row.getString("word")); | |
} | |
} | |
//---------------- | |
public void getFrequentWordsInPosition(int position,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_pos_frequency WHERE position=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(position, amount)); | |
for (Row row : results) { | |
System.out.format("%s\n", row.getString("word")); | |
} | |
} | |
public void getFrequentWordsInPositionReverse(int position,int amount){ | |
PreparedStatement query = session.prepare( | |
"select * from corpus.word_inv_pos_frequency WHERE inv_position=? order by frequency DESC LIMIT ?"); | |
ResultSet results = session.execute(query.bind(position,amount)); | |
for (Row row : results) { | |
System.out.format("%s\n", row.getString("word")); | |
} | |
} | |
public static void main(String[] args){ | |
DbInterface client = new DbInterface(); | |
client.connect("127.0.0.1"); | |
client.getFrequentWordsInPosition(0,10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment