私は Python のバックグラウンドを持っており、現在 Python プログラムを Java に移植しています。問題を解決するための最善のアプローチについて提案が必要です。
もともと、私は Python でタプルのリストを作成しました:
loft = [('india',1),('accepts',1),('narendra',1), ('modi',1),('manmohan',1),('singh',1),('sonia gandhi',1),('rajkot',1),('sharma',1),('raja',1),('india',2),('manmohan',2),('singh',2),('nepal',2),('prime minister',2),('meeting',2),('economy',2),('manmohan',3),('narendra',3),('modi',3),('gupta',3),('rajkot',3),('patel',3),('singh',3),('rajiv',3),('aajtak',3),('manmohan',4),('nepal',4),('bahadur',4),('king',4),('meeting',4),('economy',4),('wife',4),('plane',4)]
(india,accepts はキーワード、数字はデータベースから取得した ID です)。今、適用します:
di = {}
for x,y in ll:
di.setdefault(x,[]).append(y)
newdi = {}
私のリストは辞書になります:
di = {'manmohan': [1, 2, 3, 4], 'sonia gandhi': [1], 'raja': [1], 'india': [1, 2], 'narendra': [1, 3], 'patel': [3], 'sharma': [1], 'nepal': [2, 4], 'gupta': [3], 'singh': [1, 2, 3], 'meeting': [2, 4], 'economy': [2, 4], 'rajkot': [1, 3], 'prime minister': [2], 'plane': [4], 'bahadur': [4], 'king': [4], 'wife': [4], 'accepts': [1], 'modi': [1, 3], 'aajtak': [3], 'rajiv': [3]}
Java 部分:
public void step1() throws SQLException{
Connection con= new Clustering().connect();
Statement st = con.createStatement();
Statement st1 = con.createStatement();
ResultSet rs = st.executeQuery("select uid from url where artorcat=1");
ArrayList<Tuples> allkeyword = new ArrayList<Tuples>();
long starttime = System.currentTimeMillis();
while (rs.next()) {
int id = rs.getInt("uid");
String query = "select tags.tagname from tags left join tag_url_relation on tags.tid=tag_url_relation.tid where tag_url_relation.uid="+id;
ResultSet rs1 = st1.executeQuery(query);
while (rs1.next()){
String tag = rs1.getString(1);
//Creating an object t of type Tuples
//and pass values to constructor
Tuples t = new Tuples(id,tag);
//adding the above tuple to arraylist allkeyword
allkeyword.add(t);
}//job done, now lets test by iterating
}
Iterator<Tuples> it = allkeyword.iterator();
while(it.hasNext()){
Tuples t = it.next();
System.out.println(t.getId());
System.out.println(t.getKeyword());
}
long endtime = System.currentTimeMillis();
long totaltime = endtime-starttime;
System.out.println("Total time:" + totaltime);
}
And here is Tuples class :
/**
*
*
* Tuple class is created to create a multiple data type tuple. We are using this tuples object to retrieve keyword and
* id in step1 in Clustering.java.
* @author akshayy
*
*/
public class Tuples {
int i;
String s;
public Tuples(int i, String s) {
this.i= i;
this.s=s;
}
public int getId(){
return this.i;
}
public String getKeyword(){
return this.s;
}
}
ここまでは順調ですね。キーワードと ID を含むタプル クラスの配列リストが作成されます。では、id でのキーワードの出現を見つける次のステップについてはどうでしょうか。「マンモハン」のようなものは、ID 1、2、3、4 などにあります。
di = {'manmohan': [1, 2, 3, 4], 'sonia gandhi': [1], 'raja': [1], 'india': [1, 2], 'narendra': [1, 3], 'patel': [3], 'sharma': [1], 'nepal': [2, 4], 'gupta': [3], 'singh': [1, 2, 3], 'meeting': [2, 4], 'economy': [2, 4], 'rajkot': [1, 3], 'prime minister': [2], 'plane': [4], 'bahadur': [4], 'king': [4], 'wife': [4], 'accepts': [1], 'modi': [1, 3], 'aajtak': [3], 'rajiv': [3]}
arraylistで同様のアイテムを見つけて上記のように並べ替えるための次のアプローチを教えてください。それとも、まったく別のものが必要ですか?