一个关键字标红的通用类

来源:互联网 发布:淘宝宝贝手机排名查询 编辑:程序博客网 时间:2024/05/01 15:22
Java代码 复制代码 收藏代码
  1. import java.lang.reflect.Array;   
  2. import java.util.Arrays;   
  3. import java.util.HashSet;   
  4. import java.util.Iterator;   
  5. import java.util.Set;   
  6.   
  7. /**  
  8. * 文本坐标记用的  
  9. * tagBegin是开始标记  
  10. * tagEnd 是结束标记  
  11. * 用了二分法查找来确定单词  
  12. * content 是传入的正文  
  13. * 正文可以传多次  
  14. * 标记词语也可以传多次  
  15. * @author Ansj  
  16. *  
  17. */  
  18. public class TagWord {   
  19.         private String tagBegin;   
  20.         private String tagEnd;   
  21.         Branch frontbegin = null;   
  22.         Set<String> keyWords = new HashSet<String>();   
  23.   
  24.         public TagWord(String begin, String end) {   
  25.                 this.tagBegin = begin;   
  26.                 this.tagEnd = end;   
  27.         }   
  28.   
  29.         public TagWord addKeyWords(String[] keyWord) {   
  30.                 if (keyWord.length > 0) {   
  31.                         for (int i = 0; i < keyWord.length; i++) {   
  32.                                 this.keyWords.add(keyWord[i].trim());   
  33.                         }   
  34.                 }   
  35.                 return this;   
  36.         }   
  37.   
  38.         // 是否发现词   
  39.         boolean findWord = false;   
  40.   
  41.         public String getTagContent(String content) {   
  42.                 if (content == null || content.trim().length() == 0  
  43.                                 || keyWords.size() == 0) {   
  44.                         return content;   
  45.                 }   
  46.                 this.frontbegin = new MakeLibrary().getStringTree(this.keyWords);   
  47.                 if(frontbegin==null){   
  48.                         return content ;   
  49.                 }   
  50.                 char[] chars = content.toCharArray();   
  51.                 // 正文   
  52.                 StringBuilder sb = new StringBuilder();   
  53.   
  54.                 WoodInterface head = this.frontbegin;   
  55.                 int start = 0;   
  56.                 int end = 1;   
  57.                 int index = 0;   
  58.                 boolean isBack = false ;   
  59.                 int length = chars.length ;   
  60.                 // 此处是正向最大匹配   
  61.                 for (int i = 0; i < length; i++) {   
  62.                         index++ ;   
  63.                                 head = head.get(chars[i]) ;   
  64.                                 if(head==null){   
  65.                                         if(isBack){   
  66.                                                 sb.append(tagBegin).append(chars,start,end).append(tagEnd) ;   
  67.                                                 start = start+end ;   
  68.                                                 i = start-1  ;   
  69.                                                 isBack = false ;   
  70.                                         }else{   
  71.                                                 sb.append(chars,start,end) ;   
  72.                                                 i = start ;   
  73.                                                 start++ ;   
  74.                                         }   
  75.                                         head = this.frontbegin ;   
  76.                                         index = 0  ;   
  77.                                         end = 1  ;   
  78.                                         continue ;   
  79.                                 }   
  80.                                 switch (head.getStatus()) {   
  81.                                 case 1:   
  82.                                         break ;   
  83.                                 case 2:   
  84.                                         end = index ;   
  85.                                         isBack = true ;   
  86.                                         break ;   
  87.                                 case 3:   
  88.                                         sb.append(tagBegin).append(chars,start,index).append(tagEnd) ;   
  89.                                         start = start+index ;   
  90.                                         index= 0  ;   
  91.                                         end = 1 ;   
  92.                                         isBack = false ;   
  93.                                         head = this.frontbegin;   
  94.                                         break ;   
  95.                                 }   
  96.                         }   
  97.   
  98.                 return sb.toString();   
  99.         }   
  100.   
  101.            
  102.   
  103.         public static void main(String[] args) {   
  104.                 String[] keyWords = {"中华人民共和国","孙健","伟大","人民""中华","万岁" };   
  105.                 long start = System.currentTimeMillis();   
  106.                 for (int i = 0; i < 1; i++) {   
  107.                         String str = new TagWord("<begin>""<end>").addKeyWords(keyWords)   
  108.                                         .getTagContent(   
  109.                                                         "中华人民共和国是一个伟大的民族我们有振兴民族的需要孙健万岁 . 中 国 万万岁哈哈  。");   
  110.                         System.out.println(str);   
  111.                 }   
  112.                 System.out.println(System.currentTimeMillis() - start);   
  113.         }   
  114.   
  115. }   
  116.   
  117. class MakeLibrary {   
  118.   
  119.         public MakeLibrary() {   
  120.         }   
  121.   
  122.         // 是否有下一个   
  123.         private static boolean hasNext = true;   
  124.         // 是否是一个词   
  125.         private static boolean isWords = true;   
  126.   
  127.   
  128.         Iterator<String> it = null;   
  129.   
  130.         public Branch getStringTree(Set<String> keyWords) {   
  131.                 it = keyWords.iterator();   
  132.                 Branch head = new Branch('h'00);   
  133.                 Branch branch = head ;   
  134.                    
  135.                 while (it.hasNext()) {   
  136.                         char[] chars = it.next().toCharArray();   
  137.                         for (int i = 0; i < chars.length; i++) {   
  138.                                 if (chars.length == (i + 1)) {   
  139.                                         isWords = true;   
  140.                                         hasNext = false;   
  141.                                 } else {   
  142.                                         isWords = false;   
  143.                                         hasNext = true;   
  144.                                 }   
  145.                                 int status = 1;   
  146.                                 if (isWords && hasNext) {   
  147.                                         status = 2;   
  148.                                 }   
  149.   
  150.                                 if (!isWords && hasNext) {   
  151.                                         status = 1;   
  152.                                 }   
  153.   
  154.                                 if (isWords && !hasNext) {   
  155.                                         status = 3;   
  156.                                 }   
  157.                                 branch.add(new Branch(chars[i], status, 0));   
  158.                                 branch = (Branch) branch.get(chars[i]);   
  159.                         }   
  160.                         branch = head ;   
  161.                 }   
  162.                 return head;   
  163.         }   
  164. }   
  165. interface WoodInterface {   
  166.         public WoodInterface add(WoodInterface branch) ;   
  167.         public WoodInterface get(char c) ;   
  168.         public boolean contains(char c) ;   
  169.         public int compareTo(char c) ;   
  170.         public boolean equals(char c) ;   
  171.         public byte getStatus() ;   
  172.         public char getC() ;   
  173.         public void setStatus(int status) ;   
  174.         public byte getNature() ;   
  175.         public void setNature(byte nature) ;   
  176. }   
  177.   
  178.   
  179. class Branch implements WoodInterface {   
  180.         /**  
  181.          * status 此字的状态1,继续  2,是个词语但是还可以继续 ,3确定  
  182.          * nature 词语性质  
  183.          * 0.未知 . 1是姓 . 2 是职位名称  3 是数量级的词 . 4 是数字词语 5 是标点 
  184.          */  
  185.         WoodInterface[] branches = new WoodInterface[0];   
  186.         private char c;   
  187.         // 状态   
  188.         private byte status = 1;   
  189.         // 索引   
  190.         private short index = -1;   
  191.         // 词性   
  192.         private byte nature = 0;   
  193.         // 单独查找出来的对象   
  194.         WoodInterface branch = null;   
  195.   
  196.         public WoodInterface add(WoodInterface branch) {   
  197.                 if ((this.branch=this.get(branch.getC()))!=null) {   
  198.                         switch (branch.getStatus()) {   
  199.                         case 1:   
  200.                                 if(this.branch.getStatus()==2){   
  201.                                         this.branch.setStatus(2) ;   
  202.                                 }   
  203.                                 if(this.branch.getStatus()==3){   
  204.                                         this.branch.setStatus(2) ;   
  205.                                 }   
  206.                                 break;   
  207.                         case 2:   
  208.                                 this.branch.setStatus(2) ;   
  209.                         case 3:   
  210.                                 if(this.branch.getStatus()==2){   
  211.                                         this.branch.setStatus(2) ;   
  212.                                 }   
  213.                                 if(this.branch.getStatus()==1){   
  214.                                         this.branch.setStatus(2) ;   
  215.                                 }   
  216.                         }   
  217.                         this.branch.setNature(branch.getNature())   ;   
  218.                         return this.branch;   
  219.                 }   
  220.                 index++;   
  221.                 if ((index + 1) > branches.length) {   
  222.                         branches = Arrays.copyOf(branches, index + 1);   
  223.                 }   
  224.                 branches[index] = branch;   
  225.                 AnsjArrays.sort(branches);   
  226.                 return branch;   
  227.         }   
  228.   
  229.         public Branch(char c, int status, int nature) {   
  230.                 this.c = c;   
  231.                 this.status = (byte) status;   
  232.                 this.nature = (byte) nature;   
  233.         }   
  234.   
  235.         int i = 0;   
  236.   
  237.         public WoodInterface get(char c) {   
  238.                 int i = AnsjArrays.binarySearch(branches, c);   
  239.                 if (i > -1) {   
  240.                         return branches[i];   
  241.                 }   
  242.                 return null;   
  243.         }   
  244.   
  245.         public boolean contains(char c) {   
  246.                 if (AnsjArrays.binarySearch(branches, c) > -1) {   
  247.                         return true;   
  248.                 } else {   
  249.                         return false;   
  250.                 }   
  251.         }   
  252.   
  253.         public int compareTo(char c) {   
  254.                 if (this.c > c) {   
  255.                         return 1;   
  256.                 }else if (this.c < c) {   
  257.                         return -1;   
  258.                 }else  
  259.                 return 0 ;   
  260.         }   
  261.   
  262.         public boolean equals(char c) {   
  263.                 if (this.c == c) {   
  264.                         return true;   
  265.                 } else {   
  266.                         return false;   
  267.                 }   
  268.         }   
  269.   
  270.         @Override  
  271.         public int hashCode() {   
  272.                 // TODO Auto-generated method stub  
  273.                 return c;   
  274.         }   
  275.   
  276.         public byte getStatus() {   
  277.                 return status;   
  278.         }   
  279.   
  280.         public void setStatus(int status) {   
  281.                 this.status = (byte) status;   
  282.         }   
  283.   
  284.         public char getC() {   
  285.                 return this.c;   
  286.         }   
  287.   
  288.         public byte getNature() {   
  289.                 return nature;   
  290.         }   
  291.   
  292.         public void setNature(byte nature) {   
  293.                 this.nature = nature;   
  294.         }   
  295.   
  296. }   
  297. class AnsjArrays {   
  298.         private static final int INSERTIONSORT_THRESHOLD = 7;   
  299.   
  300.         /**  
  301.          * 二分法查找.摘抄了jdk的东西..只不过把他的自动装箱功能给去掉了  
  302.          *   
  303.          * @param branches  
  304.          * @param c  
  305.          * @return  
  306.          */  
  307.         public static int binarySearch(WoodInterface[] branches, char c) {   
  308.                 int high = branches.length - 1;   
  309.                 if (branches.length < 1) {   
  310.                         return high;   
  311.                 }   
  312.                 int low = 0;   
  313.                 while (low <= high) {   
  314.                         int mid = (low + high) >>> 1;   
  315.                         int cmp = branches[mid].compareTo(c);   
  316.   
  317.                         if (cmp < 0)   
  318.                                 low = mid + 1;   
  319.                         else if (cmp > 0)   
  320.                                 high = mid - 1;   
  321.                         else  
  322.                                 return mid; // key found  
  323.                 }   
  324.                 return -1// key not found.  
  325.         }   
  326.   
  327.         public static void sort(WoodInterface[] a) {   
  328.                 WoodInterface[] aux = (WoodInterface[])a.clone();   
  329.         mergeSort(aux, a, 0, a.length, 0);   
  330.     }   
  331.            
  332.         public static void sort(WoodInterface[] a, int fromIndex, int toIndex) {   
  333.                 rangeCheck(a.length, fromIndex, toIndex);   
  334.                 WoodInterface[] aux = copyOfRange(a, fromIndex, toIndex);   
  335.                 mergeSort(aux, a, fromIndex, toIndex, -fromIndex);   
  336.         }   
  337.   
  338.         private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {   
  339.                 if (fromIndex > toIndex)   
  340.                         throw new IllegalArgumentException("fromIndex(" + fromIndex   
  341.                                         + ") > toIndex(" + toIndex + ")");   
  342.                 if (fromIndex < 0)   
  343.                         throw new ArrayIndexOutOfBoundsException(fromIndex);   
  344.                 if (toIndex > arrayLen)   
  345.                         throw new ArrayIndexOutOfBoundsException(toIndex);   
  346.         }   
  347.   
  348.         private static void mergeSort(WoodInterface[] src, WoodInterface[] dest, int low,   
  349.                         int high, int off) {   
  350.                 int length = high - low;   
  351.   
  352.                 // Insertion sort on smallest arrays  
  353.                 if (length < INSERTIONSORT_THRESHOLD) {   
  354.                         for (int i = low; i < high; i++)   
  355.                                 for (int j = i; j > low   
  356.                                                 && (dest[j - 1]).compareTo(dest[j].getC()) > 0; j--)   
  357.                                         swap(dest, j, j - 1);   
  358.                         return;   
  359.                 }   
  360.   
  361.                 // Recursively sort halves of dest into src  
  362.                 int destLow = low;   
  363.                 int destHigh = high;   
  364.                 low += off;   
  365.                 high += off;   
  366.                 int mid = (low + high) >>> 1;   
  367.                 mergeSort(dest, src, low, mid, -off);   
  368.                 mergeSort(dest, src, mid, high, -off);   
  369.   
  370.                 // If list is already sorted, just copy from src to dest. This is an  
  371.                 // optimization that results in faster sorts for nearly ordered lists.  
  372.                 if (src[mid - 1].compareTo(src[mid].getC()) <= 0) {   
  373.                         System.arraycopy(src, low, dest, destLow, length);   
  374.                         return;   
  375.                 }   
  376.   
  377.                 // Merge sorted halves (now in src) into dest  
  378.                 for (int i = destLow, p = low, q = mid; i < destHigh; i++) {   
  379.                         if (q >= high || p < mid   
  380.                                         &&  src[p].compareTo(src[q].getC()) <= 0)   
  381.                                 dest[i] = src[p++];   
  382.                         else  
  383.                                 dest[i] = src[q++];   
  384.                 }   
  385.         }   
  386.   
  387.         /**  
  388.          * Swaps x[a] with x[b].  
  389.          */  
  390.         private static void swap(WoodInterface[] x, int a, int b) {   
  391.                 WoodInterface t = x[a];   
  392.                 x[a] = x[b];   
  393.                 x[b] = t;   
  394.         }   
  395.   
  396.         public static <T> T[] copyOfRange(T[] original, int from, int to) {   
  397.                 return copyOfRange(original, from, to, (Class<T[]>) original.getClass());   
  398.         }   
  399.   
  400.         public static <T, U> T[] copyOfRange(U[] original, int from, int to,   
  401.                         Class<? extends T[]> newType) {   
  402.                 int newLength = to - from;   
  403.                 if (newLength < 0)   
  404.                         throw new IllegalArgumentException(from + " > " + to);   
  405.                 T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength]   
  406.                                 : (T[]) Array   
  407.                                                 .newInstance(newType.getComponentType(), newLength);   
  408.                 System.arraycopy(original, from, copy, 0, Math.min(original.length   
  409.                                 - from, newLength));   
  410.                 return copy;   
  411.         }   
原创粉丝点击