/edu/uncc/parsets/data/CategoryTree.java

https://code.google.com/p/parsets/ · Java · 133 lines · 77 code · 28 blank · 28 comment · 15 complexity · 662b339c5003d024d62fb333b4bf2d55 MD5 · raw file

  1. package edu.uncc.parsets.data;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.TreeMap;
  6. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  7. * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
  8. * and others (see Authors.txt for full list)
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of UNC Charlotte nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
  24. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  27. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  34. public class CategoryTree implements Iterable<List<CategoryNode>> {
  35. private ArrayList<List<CategoryNode>> levels;
  36. private ArrayList<TreeMap<CategoryHandle, List<CategoryNode>>> catGroups;
  37. private CategoryNode rootNode;
  38. public CategoryTree(int numLevels) {
  39. levels = new ArrayList<List<CategoryNode>>(numLevels);
  40. catGroups = new ArrayList<TreeMap<CategoryHandle,List<CategoryNode>>>(numLevels);
  41. for (int i = 0; i < numLevels; i++) {
  42. levels.add(new ArrayList<CategoryNode>());
  43. catGroups.add(new TreeMap<CategoryHandle, List<CategoryNode>>());
  44. }
  45. }
  46. public void addtoLevel(int level, CategoryNode node) {
  47. List<CategoryNode> levelList = levels.get(level);
  48. levelList.add(node);
  49. TreeMap<CategoryHandle, List<CategoryNode>> group = catGroups.get(level);
  50. if (level == 0)
  51. rootNode = node;
  52. else {
  53. List<CategoryNode> catList = group.get(node.getToCategory());
  54. if (catList == null) {
  55. catList = new ArrayList<CategoryNode>();
  56. group.put(node.getToCategory(), catList);
  57. }
  58. catList.add(node);
  59. }
  60. }
  61. public List<CategoryNode> getLevelList(int level) {
  62. return levels.get(level);
  63. }
  64. public List<CategoryNode> getNodesForCategory(int level, CategoryHandle category) {
  65. return catGroups.get(level).get(category);
  66. }
  67. public Iterator<List<CategoryNode>> iterator() {
  68. return levels.iterator();
  69. }
  70. public CategoryNode getRootNode() {
  71. return rootNode;
  72. }
  73. public void print() {
  74. rootNode.print();
  75. }
  76. public float getFilteredFrequency(CategoryHandle cat) {
  77. float total = 0;
  78. List<CategoryNode> nodes = getLevelList(levels.size()-1);
  79. for(CategoryNode node : nodes) {
  80. if (node.isVisible())
  81. total += node.getCount();
  82. }
  83. return (float)getFilteredCount(cat)/total;
  84. }
  85. public int getFilteredCount(CategoryHandle cat) {
  86. int count = 0;
  87. for (int i=0; i<levels.size(); i++) {
  88. List<CategoryNode> nodes = getNodesForCategory(i, cat);
  89. if (nodes != null)
  90. for (CategoryNode node : nodes)
  91. if (node.isVisible())
  92. count += node.getCount();
  93. }
  94. return count;
  95. }
  96. public int getFilteredTotal() {
  97. int total = 0;
  98. List<CategoryNode> nodes = getLevelList(levels.size()-1);
  99. for(CategoryNode node : nodes) {
  100. if (node.isVisible())
  101. total += node.getCount();
  102. }
  103. return total;
  104. }
  105. }