|
| 1 | +package edu.cloudy.layout; |
| 2 | + |
| 3 | +import java.awt.geom.Rectangle2D; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import edu.cloudy.nlp.ItemPair; |
| 10 | + |
| 11 | +public class ItemGraph { |
| 12 | + |
| 13 | + private List<Rectangle2D.Double> items; |
| 14 | + private Map<ItemPair<Rectangle2D.Double>, Double> similarity; |
| 15 | + private Map<ItemPair<Rectangle2D.Double>, Double> distance; |
| 16 | + |
| 17 | + private ItemGraphCache cache; |
| 18 | + |
| 19 | + public ItemGraph(List<Rectangle2D.Double> items, Map<ItemPair<Rectangle2D.Double>, Double> similarity) { |
| 20 | + this.items = items; |
| 21 | + this.similarity = similarity; |
| 22 | + |
| 23 | + checkConsistency(); |
| 24 | + |
| 25 | + initializeDistances(); |
| 26 | + cache = new WordGraphCache(items, similarity, distance); |
| 27 | + } |
| 28 | + |
| 29 | + public List<Rectangle2D.Double> getItems() { |
| 30 | + return items; |
| 31 | + } |
| 32 | + |
| 33 | + public Map<ItemPair<Rectangle2D.Double>, Double> getSimilarity() { |
| 34 | + return similarity; |
| 35 | + } |
| 36 | + |
| 37 | + public double distance(Rectangle2D.Double r1, Rectangle2D.Double r2) { |
| 38 | + return distance.get(new ItemPair<Rectangle2D.Double>(r1, r2)); |
| 39 | + } |
| 40 | + |
| 41 | + public double weightedDegree(Rectangle2D.Double r) { |
| 42 | + return cache.weightedDegree(r); |
| 43 | + } |
| 44 | + |
| 45 | + public double shortestPath(Rectangle2D.Double r1, Rectangle2D.Double r2) { |
| 46 | + return cache.shortestPath(r1, r2); |
| 47 | + } |
| 48 | + |
| 49 | + public Integer[] nonZeroAdjacency(Rectangle2D.Double r) { |
| 50 | + return cache.nonZeroAdjacency(r); |
| 51 | + } |
| 52 | + |
| 53 | + public Rectangle2D.Double[] convertWordsToArray() { |
| 54 | + return items.toArray(new Rectangle2D.Double[items.size()]); |
| 55 | + } |
| 56 | + |
| 57 | + public double[][] convertSimilarityToArray() { |
| 58 | + double[][] result = new double[items.size()][items.size()]; |
| 59 | + for( int i = 0; i < items.size(); ++i ) |
| 60 | + for( int j = 0; j < items.size(); ++j ) { |
| 61 | + ItemPair<Rectangle2D.Double> rp = new ItemPair<Rectangle2D.Double>(items.get(i), items.get(j)); |
| 62 | + result[i][j] = similarity.get(rp); |
| 63 | + } |
| 64 | + |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + private void initializeDistances() { |
| 69 | + distance = new HashMap<ItemPair<Rectangle2D.Double>, Double>(); |
| 70 | + for( int i = 0; i < items.size(); ++i ) |
| 71 | + for( int j = 0; j < items.size(); ++j ) { |
| 72 | + ItemPair<Rectangle2D.Double> rp = new ItemPair<Rectangle2D.Double>(items.get(i), items.get(j)); |
| 73 | + double sim = similarity.get(rp); |
| 74 | + double dist = LayoutUtils.idealDistanceConverter(sim); |
| 75 | + distance.put(rp, dist); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public void reorderWords(int startIndex) { |
| 80 | + int n = items.size(); |
| 81 | + List<Rectangle2D.Double> path = new ArrayList<Rectangle2D.Double>(); |
| 82 | + for( int i = 0; i < n; ++i ) |
| 83 | + path.add(items.get((i + startIndex + 1) % n)); |
| 84 | + |
| 85 | + for( int i = 0; i < n; ++i ) |
| 86 | + items.set(i, path.get(i)); |
| 87 | + } |
| 88 | + |
| 89 | + private void checkConsistency() { |
| 90 | + for( int i = 0; i < items.size(); ++i ) { |
| 91 | + |
| 92 | + Rectangle2D.Double ri = items.get(i); |
| 93 | + ItemPair<Rectangle2D.Double> rp = new ItemPair<Rectangle2D.Double>(ri, ri); |
| 94 | + |
| 95 | + assert( similarity.containsKey(rp) && similarity.get(rp) == 1.0 ); |
| 96 | + |
| 97 | + for( int j = 0; j < items.size(); ++j ) { |
| 98 | + Rectangle2D.Double rj = items.get(j); |
| 99 | + ItemPair<Rectangle2D.Double> rp2 = new ItemPair<Rectangle2D.Double>(ri, rj); |
| 100 | + assert( similarity.containsKey(rp2) ); |
| 101 | + |
| 102 | + double sim = similarity.get(rp2); |
| 103 | + assert( 0 <= sim && sim <= 1.0); |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments