CalRunable.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package top.zhixinghe1.money.agg.entity;
  2. import org.apache.commons.text.similarity.CosineSimilarity;
  3. import java.util.ArrayList;
  4. import java.util.BitSet;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Objects;
  10. import java.util.Set;
  11. import java.util.concurrent.LinkedBlockingQueue;
  12. /**
  13. * 计算任务对象
  14. */
  15. public class CalRunable implements Runnable {
  16. private int start;
  17. private int end;
  18. private Map<Integer, Word> wordCache = new HashMap();
  19. private Map<String, Set<Integer>> indexCache = new HashMap();
  20. private BitSet bitmap = null;
  21. private CosineSimilarity cosineSimilarity = new CosineSimilarity();
  22. private Double aggThreshold = 0.8;
  23. private LinkedBlockingQueue<CalResult> queue;
  24. private Set<Integer> indexSet = new HashSet<>();
  25. private List<String> result = new ArrayList<>();
  26. public CalRunable(int start, int end, Map<Integer, Word> wordCache, Map<String, Set<Integer>> indexCache, BitSet bitmap, LinkedBlockingQueue<CalResult> queue) {
  27. this.start = start;
  28. this.end = end;
  29. this.wordCache = wordCache;
  30. this.indexCache = indexCache;
  31. this.bitmap = bitmap;
  32. this.queue = queue;
  33. }
  34. @Override
  35. public void run() {
  36. try {
  37. for (int i = start; i <= end; i++) {
  38. CalResult calResult = null;
  39. if (cal(i)) {
  40. calResult = new CalResult(true, new ArrayList<>(result));
  41. } else {
  42. calResult = new CalResult(false, null);
  43. }
  44. calResult.setEndStatus(i == end);
  45. queue.put(calResult);
  46. }
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. private boolean cal(int i) {
  52. // 判断是否已进行计算
  53. if (bitmap.get(i)) {
  54. return false;
  55. }
  56. // 清除上一轮的数据
  57. indexSet.clear();
  58. result.clear();
  59. Word word = wordCache.get(i);
  60. if (Objects.isNull(word.getStemMap()) || word.getStemMap().size() == 0) {
  61. return false;
  62. }
  63. bitmap.set(i, true);
  64. result.add(word.getKey());
  65. for (CharSequence stem : word.getStemMap().keySet()) {
  66. Set<Integer> positions = indexCache.get(stem);
  67. for (Integer position : positions) {
  68. if (bitmap.get(position)) {
  69. positions.remove(position);
  70. } else {
  71. indexSet.add(position);
  72. }
  73. }
  74. }
  75. for (Integer index : indexSet) {
  76. Word candicateWord = wordCache.get(index);
  77. if (Objects.isNull(candicateWord.getStemMap())) {
  78. continue;
  79. }
  80. Double v = cosineSimilarity.cosineSimilarity(word.getStemMap(), candicateWord.getStemMap());
  81. if (v < aggThreshold) {
  82. continue;
  83. }
  84. result.add(candicateWord.getKey());
  85. }
  86. // 输出计算结果
  87. return result.size() > 1;
  88. }
  89. }