SegList.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. namespace Ant.Service.Utilities
  6. {
  7. /// <summary>
  8. /// 分词辅助类
  9. /// </summary>
  10. public class SegList
  11. {
  12. public int MaxLength;
  13. private ArrayList m_seg;
  14. public int Count
  15. {
  16. get
  17. {
  18. return m_seg.Count;
  19. }
  20. }
  21. public SegList()
  22. {
  23. m_seg = new ArrayList();
  24. MaxLength = 0;
  25. }
  26. public void Add(object obj)
  27. {
  28. m_seg.Add(obj);
  29. if (MaxLength < obj.ToString().Length)
  30. {
  31. MaxLength = obj.ToString().Length;
  32. }
  33. }
  34. public object GetElem(int i)
  35. {
  36. if (i < this.Count)
  37. return m_seg[i];
  38. else
  39. return null;
  40. }
  41. public void SetElem(int i, object obj)
  42. {
  43. m_seg[i] = obj;
  44. }
  45. public bool Contains(object obj)
  46. {
  47. return m_seg.Contains(obj);
  48. }
  49. /// <summary>
  50. /// 按长度排序
  51. /// </summary>
  52. public void Sort()
  53. {
  54. Sort(this);
  55. }
  56. /// <summary>
  57. /// 按长度排序
  58. /// </summary>
  59. public void Sort(SegList list)
  60. {
  61. int max = 0;
  62. for (int i = 0; i < list.Count - 1; ++i)
  63. {
  64. max = i;
  65. for (int j = i + 1; j < list.Count; ++j)
  66. {
  67. string str1 = list.GetElem(j).ToString();
  68. string str2 = list.GetElem(max).ToString();
  69. int l1;
  70. int l2;
  71. if (str1 == "null")
  72. l1 = 0;
  73. else
  74. l1 = str1.Length;
  75. if (str2 == "null")
  76. l2 = 0;
  77. else
  78. l2 = str2.Length;
  79. if (l1 > l2)
  80. max = j;
  81. }
  82. object o = list.GetElem(max);
  83. list.SetElem(max, list.GetElem(i));
  84. list.SetElem(i, o);
  85. }
  86. }
  87. }
  88. }