Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/CalculatedProximityStore.java |
— | — | @@ -0,0 +1,68 @@ |
| 2 | +package de.brightbyte.wikiword.store; |
| 3 | + |
| 4 | +import de.brightbyte.data.Functor2; |
| 5 | +import de.brightbyte.data.LabeledVector; |
| 6 | +import de.brightbyte.data.MapLabeledVector; |
| 7 | +import de.brightbyte.data.cursor.DataCursor; |
| 8 | +import de.brightbyte.data.cursor.DataSet; |
| 9 | +import de.brightbyte.util.PersistenceException; |
| 10 | +import de.brightbyte.wikiword.model.WikiWordConcept; |
| 11 | +import de.brightbyte.wikiword.model.WikiWordConceptFeatures; |
| 12 | +import de.brightbyte.wikiword.model.WikiWordConceptReference; |
| 13 | + |
| 14 | +public class CalculatedProximityStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> |
| 15 | + implements ProximityStore<T, R> { |
| 16 | + |
| 17 | + protected FeatureTopologyStore<T, R> featureStore; |
| 18 | + protected Functor2<Double, LabeledVector<Integer>, LabeledVector<Integer>> proximityMeasure; |
| 19 | + |
| 20 | + public CalculatedProximityStore(FeatureTopologyStore<T, R> featureStore, Functor2<Double, LabeledVector<Integer>, LabeledVector<Integer>> proximityMeasure) { |
| 21 | + this.featureStore = featureStore; |
| 22 | + this.proximityMeasure = proximityMeasure; |
| 23 | + } |
| 24 | + |
| 25 | + public DataSet<? extends R> getEnvironment(int concept, double minProximity) |
| 26 | + throws PersistenceException { |
| 27 | + LabeledVector<Integer> c = getFeatureVector(concept); |
| 28 | + DataSet<WikiWordConceptFeatures> n = featureStore.getNeighbourhoodFeatures(concept); |
| 29 | + |
| 30 | + return new EnvironmentDataSet(n); |
| 31 | + } |
| 32 | + |
| 33 | + public LabeledVector<Integer> getEnvironmentVector(int concept, double minProximity) |
| 34 | + throws PersistenceException { |
| 35 | + |
| 36 | + LabeledVector<Integer> env = new MapLabeledVector<Integer>(); |
| 37 | + |
| 38 | + LabeledVector<Integer> c = getFeatureVector(concept); |
| 39 | + DataSet<WikiWordConceptFeatures> n = featureStore.getNeighbourhoodFeatures(concept); |
| 40 | + |
| 41 | + WikiWordConceptFeatures f; |
| 42 | + DataCursor<WikiWordConceptFeatures> cursor = n.cursor(); |
| 43 | + while ((f = cursor.next()) != null) { |
| 44 | + double prox = getProximity(c, f.getFeatures()); |
| 45 | + if (prox>=minProximity) env.set(f.getId(), prox); |
| 46 | + } |
| 47 | + |
| 48 | + return env; |
| 49 | + } |
| 50 | + |
| 51 | + public LabeledVector<Integer> getFeatureVector(int concept) |
| 52 | + throws PersistenceException { |
| 53 | + |
| 54 | + return featureStore.getFeatureVector(concept); |
| 55 | + } |
| 56 | + |
| 57 | + protected double getProximity(LabeledVector<Integer> v, LabeledVector<Integer> w) { |
| 58 | + return proximityMeasure.apply(v, w); |
| 59 | + } |
| 60 | + |
| 61 | + public double getProximity(int concept1, int concept2) |
| 62 | + throws PersistenceException { |
| 63 | + LabeledVector<Integer> v = getFeatureVector(concept1); |
| 64 | + LabeledVector<Integer> w = getFeatureVector(concept2); |
| 65 | + |
| 66 | + return getProximity(v, w); |
| 67 | + } |
| 68 | + |
| 69 | +} |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/FeatureTopologyStore.java |
— | — | @@ -0,0 +1,21 @@ |
| 2 | +package de.brightbyte.wikiword.store; |
| 3 | + |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import de.brightbyte.data.LabeledVector; |
| 7 | +import de.brightbyte.data.cursor.DataSet; |
| 8 | +import de.brightbyte.util.PersistenceException; |
| 9 | +import de.brightbyte.wikiword.model.WikiWordConcept; |
| 10 | +import de.brightbyte.wikiword.model.WikiWordConceptFeatures; |
| 11 | +import de.brightbyte.wikiword.model.WikiWordConceptReference; |
| 12 | + |
| 13 | +public interface FeatureTopologyStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> { |
| 14 | + |
| 15 | + public LabeledVector<Integer> getFeatureVector(int concept) throws PersistenceException; |
| 16 | + |
| 17 | + public DataSet<? extends R> getNeighbours(int concept) throws PersistenceException; |
| 18 | + |
| 19 | + public List<Integer> getNeighbourList(int concept) throws PersistenceException; |
| 20 | + |
| 21 | + public DataSet<WikiWordConceptFeatures> getNeighbourhoodFeatures(int concept) throws PersistenceException; |
| 22 | +} |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/DatabaseProximityStore.java |
— | — | @@ -1,90 +1,50 @@ |
2 | 2 | package de.brightbyte.wikiword.store; |
3 | 3 | |
4 | | -import java.sql.ResultSet; |
5 | 4 | import java.sql.SQLException; |
6 | 5 | |
7 | 6 | import de.brightbyte.data.LabeledVector; |
8 | | -import de.brightbyte.data.MapLabeledVector; |
9 | 7 | import de.brightbyte.data.cursor.DataSet; |
10 | | -import de.brightbyte.db.EntityTable; |
11 | 8 | import de.brightbyte.db.QueryDataSet; |
12 | 9 | import de.brightbyte.db.RelationTable; |
13 | 10 | import de.brightbyte.util.PersistenceException; |
14 | 11 | import de.brightbyte.wikiword.TweakSet; |
15 | | -import de.brightbyte.wikiword.model.LocalConcept; |
16 | 12 | import de.brightbyte.wikiword.model.WikiWordConcept; |
17 | 13 | import de.brightbyte.wikiword.model.WikiWordConceptReference; |
18 | 14 | import de.brightbyte.wikiword.schema.ProximityStoreSchema; |
19 | | -import de.brightbyte.wikiword.store.DatabaseWikiWordConceptStore.DatabaseConceptInfoStore.ConceptFactory; |
20 | 15 | |
21 | 16 | public class DatabaseProximityStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> |
22 | | - extends DatabaseWikiWordStore |
| 17 | + extends DatabaseFeatureStore<T, R> |
23 | 18 | implements ProximityStore<T, R> { |
24 | 19 | |
25 | | - private DatabaseWikiWordConceptStore conceptStore; |
26 | | - private RelationTable featureTable; |
27 | 20 | private RelationTable proximityTable; |
28 | | - private EntityTable conceptTable; |
29 | 21 | |
30 | | - protected DatabaseProximityStore(DatabaseWikiWordConceptStore conceptStore, ProximityStoreSchema database, TweakSet tweaks) throws SQLException { |
31 | | - super(database, tweaks); |
| 22 | + protected DatabaseProximityStore(DatabaseWikiWordConceptStore<T, R> conceptStore, ProximityStoreSchema database, TweakSet tweaks) throws SQLException { |
| 23 | + super(conceptStore, database, tweaks); |
32 | 24 | |
33 | | - this.conceptStore = conceptStore; |
34 | | - |
35 | | - this.conceptTable = (EntityTable)conceptStore.getDatabaseAccess().getTable("concept"); |
36 | | - this.featureTable = (RelationTable)database.getTable("feature"); |
37 | 25 | this.proximityTable = (RelationTable)database.getTable("proximity"); |
38 | 26 | } |
39 | 27 | |
40 | 28 | public DataSet<? extends R> getEnvironment(int concept, double minProximity) throws PersistenceException { |
41 | | - String sql = "SELECT C.*, proximity FROM " + conceptTable.getSQLName() + " as C "; |
| 29 | + String sql = "SELECT C.id as cId, C.name as cName, proximity as qRelev "; |
| 30 | + sql += " FROM " + conceptTable.getSQLName() + " as C "; |
42 | 31 | sql += " JOIN "+proximityTable.getSQLName()+" as P ON P.concept2 = C.id "; |
43 | 32 | sql += " WHERE concept1 = "+concept; |
| 33 | + if (minProximity>0) sql += " AND proximity >= "+minProximity; |
44 | 34 | |
45 | | - return new QueryDataSet<R>(database, referenceFactory, "getEnvironment", sql, false); |
| 35 | + return new QueryDataSet<R>(database, conceptStore.getReferenceFactory(), "getEnvironment", sql, false); |
46 | 36 | } |
47 | 37 | |
48 | 38 | public LabeledVector<Integer> getEnvironmentVector(int concept, double minProximity) throws PersistenceException { |
49 | 39 | String sql = "SELECT concept2, proximity FROM " +proximityTable.getSQLName()+" as P "; |
50 | 40 | sql += " WHERE concept1 = "+concept; |
| 41 | + if (minProximity>0) sql += " AND proximity >= "+minProximity; |
51 | 42 | |
52 | 43 | return readVector("getEnvironmentVector", sql, "concept2", "proximity"); |
53 | 44 | } |
54 | 45 | |
55 | | - public LabeledVector<Integer> getFeatureVector(int concept) throws PersistenceException { |
56 | | - String sql = "SELECT feature, normal_value FROM " +featureTable.getSQLName()+" as F "; |
57 | | - sql += " WHERE concept = "+concept; |
58 | | - |
59 | | - return readVector("getFeatireVector", sql, "feature", "normal_value"); |
60 | | - } |
61 | | - |
62 | | - protected <K> LabeledVector<K> readVector(String name, String sql, String keyField, String valueField) throws PersistenceException { |
63 | | - try { |
64 | | - ResultSet rs = database.executeQuery(name, sql); |
65 | | - try { |
66 | | - return readVector(rs, keyField, valueField, new MapLabeledVector<K>()); |
67 | | - } finally { |
68 | | - rs.close(); |
69 | | - } |
70 | | - } catch (SQLException e) { |
71 | | - throw new PersistenceException(e); |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - protected <K> LabeledVector<K> readVector(ResultSet rs, String keyField, String valueField, LabeledVector<K> v) throws SQLException { |
76 | | - if (v==null) v = new MapLabeledVector<K>(); |
77 | | - |
78 | | - while (rs.next()) { |
79 | | - K k = (K)rs.getObject(keyField); |
80 | | - Number n = (Number)rs.getObject(valueField); |
81 | | - |
82 | | - v.set(k, n.doubleValue()); |
83 | | - } |
84 | | - |
85 | | - return v; |
86 | | - } |
87 | | - |
88 | 46 | public double getProximity(int concept1, int concept2) throws PersistenceException { |
| 47 | + if (concept1 == concept2) return 1; |
| 48 | + |
89 | 49 | String sql = "SELECT proximity FROM " +proximityTable.getSQLName()+" as P "; |
90 | 50 | sql += " WHERE concept1 = "+concept1; |
91 | 51 | sql += " AND concept2 = "+concept2; |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/DatabaseLocalConceptStore.java |
— | — | @@ -150,7 +150,7 @@ |
151 | 151 | |
152 | 152 | String sql = referenceSelect("M.freq") + meaningWhere(term); |
153 | 153 | |
154 | | - return new QueryDataSet<LocalConceptReference>(database, new ReferenceFactory(), "listMeanings", sql, false); |
| 154 | + return new QueryDataSet<LocalConceptReference>(database, getReferenceFactory(), "listMeanings", sql, false); |
155 | 155 | } |
156 | 156 | |
157 | 157 | public LocalConcept getConceptByName(String name) throws PersistenceException { |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/FeatureStore.java |
— | — | @@ -0,0 +1,12 @@ |
| 2 | +package de.brightbyte.wikiword.store; |
| 3 | + |
| 4 | +import de.brightbyte.data.LabeledVector; |
| 5 | +import de.brightbyte.util.PersistenceException; |
| 6 | +import de.brightbyte.wikiword.model.WikiWordConcept; |
| 7 | +import de.brightbyte.wikiword.model.WikiWordConceptReference; |
| 8 | + |
| 9 | +public interface FeatureStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> { |
| 10 | + |
| 11 | + public LabeledVector<Integer> getFeatureVector(int concept) throws PersistenceException; |
| 12 | + |
| 13 | +} |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/DatabaseWikiWordConceptStore.java |
— | — | @@ -33,12 +33,14 @@ |
34 | 34 | implements WikiWordConceptStore<T, R> { |
35 | 35 | |
36 | 36 | |
37 | | - protected class ReferenceFactory implements DatabaseDataSet.Factory<R> { |
| 37 | + private class ReferenceFactory implements DatabaseDataSet.Factory<R> { |
38 | 38 | public R newInstance(ResultSet row) throws SQLException, PersistenceException { |
39 | 39 | return newReference(row); |
40 | 40 | } |
41 | 41 | } |
42 | 42 | |
| 43 | + private ReferenceFactory referenceFactory = new ReferenceFactory(); |
| 44 | + |
43 | 45 | protected EntityTable conceptTable; |
44 | 46 | protected RelationTable broaderTable; |
45 | 47 | protected RelationTable langlinkTable; |
— | — | @@ -63,6 +65,10 @@ |
64 | 66 | langlinkTable = (RelationTable)database.getTable("langlink"); |
65 | 67 | } |
66 | 68 | |
| 69 | + protected DatabaseDataSet.Factory<R> getReferenceFactory() { |
| 70 | + return referenceFactory; |
| 71 | + } |
| 72 | + |
67 | 73 | protected R newReference(ResultSet row) throws SQLException { |
68 | 74 | int id = row.getInt("cId"); |
69 | 75 | String name = asString(row.getObject("cName")); |
— | — | @@ -131,7 +137,7 @@ |
132 | 138 | public DataSet<R> listAllConcepts() throws PersistenceException { |
133 | 139 | try { |
134 | 140 | String sql = referenceSelect("-1"); |
135 | | - return new ChunkedQueryDataSet<R>(database, new ReferenceFactory(), "listAllConcepts", "query", sql, null, null, conceptTable, "id", queryChunkSize); |
| 141 | + return new ChunkedQueryDataSet<R>(database, getReferenceFactory(), "listAllConcepts", "query", sql, null, null, conceptTable, "id", queryChunkSize); |
136 | 142 | } catch (SQLException e) { |
137 | 143 | throw new PersistenceException(e); |
138 | 144 | } |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/DatabaseGlobalConceptStore.java |
— | — | @@ -126,7 +126,7 @@ |
127 | 127 | throws PersistenceException { |
128 | 128 | |
129 | 129 | String sql = referenceSelect("M.freq") + meaningsSQL(lang, term); |
130 | | - return new QueryDataSet<GlobalConceptReference>(database, new ReferenceFactory(), "listMeanings", sql, false); |
| 130 | + return new QueryDataSet<GlobalConceptReference>(database, getReferenceFactory(), "listMeanings", sql, false); |
131 | 131 | } |
132 | 132 | |
133 | 133 | protected void registerLocalStore(DatabaseLocalConceptStore store) throws PersistenceException { |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/ProximityStore.java |
— | — | @@ -6,10 +6,8 @@ |
7 | 7 | import de.brightbyte.wikiword.model.WikiWordConcept; |
8 | 8 | import de.brightbyte.wikiword.model.WikiWordConceptReference; |
9 | 9 | |
10 | | -public interface ProximityStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> { |
| 10 | +public interface ProximityStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> extends FeatureStore<T, R> { |
11 | 11 | |
12 | | - public LabeledVector<Integer> getFeatureVector(int concept) throws PersistenceException; |
13 | | - |
14 | 12 | public double getProximity(int concept1, int concept2) throws PersistenceException; |
15 | 13 | |
16 | 14 | public DataSet<? extends R> getEnvironment(int concept, double minProximity) throws PersistenceException; |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/DatabaseFeatureStore.java |
— | — | @@ -0,0 +1,160 @@ |
| 2 | +package de.brightbyte.wikiword.store; |
| 3 | + |
| 4 | +import java.sql.ResultSet; |
| 5 | +import java.sql.SQLException; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import de.brightbyte.data.LabeledVector; |
| 10 | +import de.brightbyte.data.MapLabeledVector; |
| 11 | +import de.brightbyte.data.cursor.DataSet; |
| 12 | +import de.brightbyte.db.DatabaseDataSet; |
| 13 | +import de.brightbyte.db.EntityTable; |
| 14 | +import de.brightbyte.db.QueryDataSet; |
| 15 | +import de.brightbyte.db.RelationTable; |
| 16 | +import de.brightbyte.util.PersistenceException; |
| 17 | +import de.brightbyte.wikiword.TweakSet; |
| 18 | +import de.brightbyte.wikiword.model.WikiWordConcept; |
| 19 | +import de.brightbyte.wikiword.model.WikiWordConceptFeatures; |
| 20 | +import de.brightbyte.wikiword.model.WikiWordConceptReference; |
| 21 | +import de.brightbyte.wikiword.schema.ProximityStoreSchema; |
| 22 | + |
| 23 | +public class DatabaseFeatureStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> |
| 24 | + extends DatabaseWikiWordStore |
| 25 | + implements FeatureTopologyStore<T, R> { |
| 26 | + |
| 27 | + protected DatabaseWikiWordConceptStore<T, R> conceptStore; |
| 28 | + protected RelationTable featureTable; |
| 29 | + protected EntityTable conceptTable; |
| 30 | + |
| 31 | + private DatabaseDataSet.Factory<WikiWordConceptFeatures> conceptFeaturesFactory = new DatabaseDataSet.Factory<WikiWordConceptFeatures>() { |
| 32 | + |
| 33 | + public WikiWordConceptFeatures newInstance(ResultSet row) throws Exception { |
| 34 | + int concept = -1; |
| 35 | + String name = null; |
| 36 | + LabeledVector<Integer> f = new MapLabeledVector<Integer>(); |
| 37 | + |
| 38 | + do { |
| 39 | + int c = row.getInt("concept"); |
| 40 | + if (concept < 0) { |
| 41 | + concept = c; |
| 42 | + name = row.getString("name"); |
| 43 | + } |
| 44 | + else if (concept != c) break; |
| 45 | + |
| 46 | + int feature = row.getInt("feature"); |
| 47 | + double value = row.getDouble("value"); |
| 48 | + |
| 49 | + f.set(feature, value); |
| 50 | + } while (row.next()); |
| 51 | + |
| 52 | + if (concept<0) return null; |
| 53 | + |
| 54 | + WikiWordConceptReference<WikiWordConcept> r = new WikiWordConceptReference<WikiWordConcept>(concept, name, 1, -1); //TODO: global vs. local |
| 55 | + return new WikiWordConceptFeatures(r, f); |
| 56 | + } |
| 57 | + |
| 58 | + }; |
| 59 | + |
| 60 | + protected DatabaseFeatureStore(DatabaseWikiWordConceptStore<T, R> conceptStore, ProximityStoreSchema database, TweakSet tweaks) throws SQLException { |
| 61 | + super(database, tweaks); |
| 62 | + |
| 63 | + this.conceptStore = conceptStore; |
| 64 | + |
| 65 | + this.conceptTable = (EntityTable)conceptStore.getDatabaseAccess().getTable("concept"); |
| 66 | + this.featureTable = (RelationTable)database.getTable("feature"); |
| 67 | + } |
| 68 | + |
| 69 | + public LabeledVector<Integer> getFeatureVector(int concept) throws PersistenceException { |
| 70 | + String sql = "SELECT feature, normal_value FROM " +featureTable.getSQLName()+" as F "; |
| 71 | + sql += " WHERE concept = "+concept; |
| 72 | + |
| 73 | + return readVector("getFeatireVector", sql, "feature", "normal_value"); |
| 74 | + } |
| 75 | + |
| 76 | + protected <K> LabeledVector<K> readVector(String name, String sql, String keyField, String valueField) throws PersistenceException { |
| 77 | + try { |
| 78 | + ResultSet rs = database.executeQuery(name, sql); |
| 79 | + try { |
| 80 | + return readVector(rs, keyField, valueField, new MapLabeledVector<K>()); |
| 81 | + } finally { |
| 82 | + rs.close(); |
| 83 | + } |
| 84 | + } catch (SQLException e) { |
| 85 | + throw new PersistenceException(e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + protected <K> LabeledVector<K> readVector(ResultSet rs, String keyField, String valueField, LabeledVector<K> v) throws SQLException { |
| 90 | + if (v==null) v = new MapLabeledVector<K>(); |
| 91 | + |
| 92 | + while (rs.next()) { |
| 93 | + K k = (K)rs.getObject(keyField); |
| 94 | + Number n = (Number)rs.getObject(valueField); |
| 95 | + |
| 96 | + v.set(k, n instanceof Double ? (Double)n : n.doubleValue()); |
| 97 | + } |
| 98 | + |
| 99 | + return v; |
| 100 | + } |
| 101 | + |
| 102 | + public DataSet<WikiWordConceptFeatures> getNeighbourhoodFeatures(int concept) throws PersistenceException { |
| 103 | + String sql = "SELECT X.concept as concept, X.feature as feature, X.value as value "; |
| 104 | + sql += " FROM " + featureTable.getSQLName() + " as X "; |
| 105 | + sql += " JOIN "+featureTable.getSQLName()+" as N ON N.feature = C.id "; |
| 106 | + sql += " JOIN "+featureTable.getSQLName()+" as F ON F.feature = N.concept "; |
| 107 | + sql += " WHERE F.concept = "+concept; |
| 108 | + sql += " ORDER BY C.id"; |
| 109 | + |
| 110 | + return new QueryDataSet<WikiWordConceptFeatures>(database, getConceptFeaturesFactory(), "getNeighbourhoodFeatures", sql, false); |
| 111 | + } |
| 112 | + |
| 113 | + private DatabaseDataSet.Factory<WikiWordConceptFeatures> getConceptFeaturesFactory() { |
| 114 | + return conceptFeaturesFactory; |
| 115 | + } |
| 116 | + |
| 117 | + public DataSet<? extends R> getNeighbours(int concept) throws PersistenceException { |
| 118 | + String sql = "SELECT DISTINCT C.id as cId, C.name as cName "; |
| 119 | + sql += " FROM " + conceptTable.getSQLName() + " as C "; |
| 120 | + sql += " JOIN "+featureTable.getSQLName()+" as N ON N.feature = C.id "; |
| 121 | + sql += " JOIN "+featureTable.getSQLName()+" as F ON F.feature = N.concept "; |
| 122 | + sql += " WHERE F.concept = "+concept+" "; |
| 123 | + |
| 124 | + return new QueryDataSet<R>(database, conceptStore.getReferenceFactory(), "getNeighbours", sql, false); |
| 125 | + } |
| 126 | + |
| 127 | + public List<Integer> getNeighbourList(int concept) throws PersistenceException { |
| 128 | + String sql = "SELECT DISTINCT N.feature as concept "; |
| 129 | + sql += " FROM "+featureTable.getSQLName()+" as N "; |
| 130 | + sql += " JOIN "+featureTable.getSQLName()+" as F ON F.feature = N.concept "; |
| 131 | + sql += " WHERE F.concept = "+concept; |
| 132 | + |
| 133 | + return readIdList("getNeighbourList", sql, "concept"); |
| 134 | + } |
| 135 | + |
| 136 | + protected List<Integer> readIdList(String name, String sql, String valueField) throws PersistenceException { |
| 137 | + try { |
| 138 | + ResultSet rs = database.executeQuery(name, sql); |
| 139 | + try { |
| 140 | + return readIdList(rs, valueField, new ArrayList<Integer>()); |
| 141 | + } finally { |
| 142 | + rs.close(); |
| 143 | + } |
| 144 | + } catch (SQLException e) { |
| 145 | + throw new PersistenceException(e); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + protected List<Integer> readIdList(ResultSet rs, String valueField, List<Integer> v) throws SQLException { |
| 150 | + if (v==null) v = new ArrayList<Integer>(); |
| 151 | + |
| 152 | + while (rs.next()) { |
| 153 | + Number n = (Number)rs.getObject(valueField); |
| 154 | + |
| 155 | + v.add(n instanceof Integer ? (Integer)n : n.intValue()); |
| 156 | + } |
| 157 | + |
| 158 | + return v; |
| 159 | + } |
| 160 | + |
| 161 | +} |
\ No newline at end of file |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/model/WikiWordConceptFeatures.java |
— | — | @@ -0,0 +1,68 @@ |
| 2 | +package de.brightbyte.wikiword.model; |
| 3 | + |
| 4 | +import de.brightbyte.data.LabeledVector; |
| 5 | + |
| 6 | +public class WikiWordConceptFeatures implements WikiWordRanking { |
| 7 | + protected WikiWordConceptReference reference; |
| 8 | + protected LabeledVector<Integer> features; |
| 9 | + |
| 10 | + public WikiWordConceptFeatures(WikiWordConceptReference reference, LabeledVector<Integer> features) { |
| 11 | + if (features==null) throw new NullPointerException(); |
| 12 | + |
| 13 | + this.features = features; |
| 14 | + this.reference = reference; |
| 15 | + } |
| 16 | + |
| 17 | + public LabeledVector<Integer> getFeatures() { |
| 18 | + return features; |
| 19 | + } |
| 20 | + |
| 21 | + public int getId() { |
| 22 | + return reference.getId(); |
| 23 | + } |
| 24 | + |
| 25 | + public String getName() { |
| 26 | + return reference.getName(); |
| 27 | + } |
| 28 | + |
| 29 | + public WikiWordConceptReference getReference() { |
| 30 | + return reference; |
| 31 | + } |
| 32 | + |
| 33 | + public int getCardinality() { |
| 34 | + return reference==null ? 1 : reference.getCardinality(); |
| 35 | + } |
| 36 | + |
| 37 | + public double getRelevance() { |
| 38 | + return reference==null ? 1 : reference.getRelevance(); |
| 39 | + } |
| 40 | + |
| 41 | + public boolean hasRanking() { |
| 42 | + return reference != null && ( reference.getCardinality()>0 || reference.getRelevance()>0 ); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public int hashCode() { |
| 47 | + return reference.hashCode(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean equals(Object obj) { |
| 52 | + if (this == obj) |
| 53 | + return true; |
| 54 | + if (obj == null) |
| 55 | + return false; |
| 56 | + if (getClass() != obj.getClass()) |
| 57 | + return false; |
| 58 | + final WikiWordConceptFeatures other = (WikiWordConceptFeatures) obj; |
| 59 | + |
| 60 | + return reference.equals(other.reference); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Override |
| 65 | + public String toString() { |
| 66 | + return reference.toString(); |
| 67 | + } |
| 68 | + |
| 69 | +} |
\ No newline at end of file |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/model/WikiWordConceptReference.java |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | package de.brightbyte.wikiword.model; |
3 | 3 | |
4 | 4 | |
5 | | -public abstract class WikiWordConceptReference<T extends WikiWordConcept> extends WikiWordReference<T> { |
| 5 | +public class WikiWordConceptReference<T extends WikiWordConcept> extends WikiWordReference<T> { |
6 | 6 | |
7 | 7 | public WikiWordConceptReference(int id, String name, int cardinality, double relevance) { |
8 | 8 | super(id, name, cardinality, relevance); |