Skip to content

Commit

Permalink
2.0.0版本,支持.net core
Browse files Browse the repository at this point in the history
  • Loading branch information
toolgood committed Jan 22, 2018
1 parent 2a1f21a commit 43ab500
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 266 deletions.
51 changes: 34 additions & 17 deletions ToolGood.Words/PinYinSearch/PinYinSearchEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ class ChineseNode : IDisposable

public bool IsEnd
{
get
{
get {
return FirstPinYinNodes.Count == 0;
}
}
Expand Down Expand Up @@ -177,8 +176,7 @@ public void FilterError()

public static TextLine Error
{
get
{
get {
var line = new TextLine();
line.IsError = true;
return line;
Expand Down Expand Up @@ -211,7 +209,7 @@ public static TextLine Error
private ushort[] _word;//节点文字
private int[] _start;//节点对前的排序
private int[] _end;//节点对前的排序
private BitArray _isEnd;//
private bool[] _isEnd;//
private int[] _fpyIndexBase;//下一层首字母拼音的开始基数

private byte[] _fpy;//首字母 为0 失败
Expand Down Expand Up @@ -425,7 +423,11 @@ public void SaveFile(string filePath)
var dir = Path.GetDirectoryName(filePath);
Directory.CreateDirectory(dir);
if (File.Exists(filePath) == false) {
#if NETSTANDARD1_3
File.Create(filePath).Dispose();
#else
File.Create(filePath).Close();
#endif
}
saveFile(filePath);
}
Expand Down Expand Up @@ -476,8 +478,14 @@ private void loadFile(string filePath)
_wordIndexBase = readIntArray(br);
_wordCheck = readIntArray(br);

#if NETSTANDARD1_3
br.Dispose();
fs.Dispose();
#else
br.Close();
fs.Close();
#endif

}
private int[] readIntArray(BinaryReader br)
{
Expand Down Expand Up @@ -527,11 +535,15 @@ private Dictionary<string, ushort> readDictionary(BinaryReader br)
}
return dict;
}
private BitArray readBitArray(BinaryReader br)
private bool[] readBitArray(BinaryReader br)
{
var count = br.ReadInt32();
byte[] array = br.ReadBytes(count);
return new BitArray(array);
bool[] array = new bool[count];
for (int i = 0; i < count; i++) {
array[i] = br.ReadBoolean();
}
return array;

}


Expand Down Expand Up @@ -570,9 +582,14 @@ private void saveFile(string filePath)
write(bw, _wordIndexBase);
write(bw, _wordCheck);


#if NETSTANDARD1_3
bw.Dispose();
fs.Dispose();
#else
bw.Close();
fs.Close();
#endif

}
private void write(BinaryWriter bw, int[] w)
{
Expand Down Expand Up @@ -632,18 +649,18 @@ private void write(BinaryWriter bw, Dictionary<string, ushort> w)
bw.Write(item.Value);
}
}
private void write(BinaryWriter bw, BitArray array)
private void write(BinaryWriter bw, bool[] w)
{
if (array == null || array.Length == 0) {
if (w == null || w.Length == 0) {
bw.Write(0);
return;
}
byte[] a = new byte[(int)Math.Ceiling(array.Length / 8.0)];

array.CopyTo(a, 0);
bw.Write(a.Length);
bw.Write(a);
bw.Write(w.Length);
foreach (var item in w) {
bw.Write(item);
}
}

#endregion

#region Search
Expand Down Expand Up @@ -910,7 +927,7 @@ private void buildNodeInfo(ChineseNode root)
_word = word.ToArray();
_start = start.ToArray();
_end = end.ToArray();
_isEnd = new BitArray(isEnd.ToArray());//.ToArray();
_isEnd = isEnd.ToArray();//.ToArray();
}
private void buildNodeInfo(ChineseNode node, List<ushort> word, List<int> start, List<int> end, List<bool> isEnd, ref int index)
{
Expand Down
36 changes: 0 additions & 36 deletions ToolGood.Words/Properties/AssemblyInfo.cs

This file was deleted.

129 changes: 73 additions & 56 deletions ToolGood.Words/TextSearch/StringSearchEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,50 +123,32 @@ public int Rank(TrieNode[] has)
private void Rank(ref int maxCount, ref int start, bool[] seats, TrieNode[] has)
{
if (maxflag == 0) return;
var keys = m_values.Select(q => q.Key).ToList();
keys.AddRange(merge_values.Select(q => q.Key).ToList());
var keys = m_values.Select(q => (int)q.Key).ToList();
keys.AddRange(merge_values.Select(q => (int)q.Key).ToList());

while (has[start] != null) { start++; }
int index = 0, next = -1;
while (next < 0) {
next = BenchMatch(seats, keys, start + index, (int)minflag) - (int)minflag;
index++;
for (int i = start; i < has.Length; i++) {
if (has[i] == null) {
var next = i - (int)minflag;
if (next < 0) continue;
if (seats[next]) continue;

var isok = true;
foreach (var item in keys) {
if (has[i - minflag + item] != null) { isok = false; break; }
}
if (isok) {
SetSeats(next, ref maxCount, seats, has);
break;
}
}
}
SetSeats(next, ref maxCount, seats, has);

var keys2 = m_values.OrderByDescending(q => q.Value.Count).ThenByDescending(q => q.Value.maxflag - q.Value.minflag);
foreach (var key in keys2) {
key.Value.Rank(ref maxCount, ref start, seats, has);
}
}
private int BenchMatch(bool[] bench, List<char> list, int start, int min)
{
var len = list.Last() - min + 1;
var removeList = new int[list.Count - 1];
for (int i = 1; i < list.Count; i++) {
removeList[i - 1] = list[i] - min;
}
start = start + min;
bool[] bs = new bool[len * 2];
Buffer.BlockCopy(bench, start, bs, 0, bs.Length);

do {
var index = len;
for (int i = 0; i < len; i++) {
if (bs[i]) {
foreach (var rm in removeList) {
if (bs[i + rm] == false)
bs[i + rm] = true;
}
} else if (bs[i + len] == false) {
return (start + i) - min;
}
}
start = start + len;
Buffer.BlockCopy(bench, start, bs, 0, bs.Length);
} while (true);

}


private void SetSeats(int next, ref int maxCount, bool[] seats, TrieNode[] has)
Expand Down Expand Up @@ -348,12 +330,38 @@ public string Replace(string text, char replaceChar = '*')
/// <summary>
/// 保存到文件
/// </summary>
/// <param name="fileName"></param>
public void Save(string fileName)
/// <param name="filePath"></param>
public void Save(string filePath)
{
var fs = File.Open(fileName, FileMode.Create);
var fs = File.Open(filePath, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
Save(bw);
#if NETSTANDARD1_3
bw.Dispose();
fs.Dispose();
#else
bw.Close();
fs.Close();
#endif
}

/// <summary>
/// 保存到Stream
/// </summary>
/// <param name="stream"></param>
public void Save(Stream stream)
{
BinaryWriter bw = new BinaryWriter(stream);
Save(bw);
#if NETSTANDARD1_3
bw.Dispose();
#else
bw.Close();
#endif
}

protected internal virtual void Save(BinaryWriter bw)
{
bw.Write(_keywords.Length);
foreach (var item in _keywords) {
bw.Write(item);
Expand Down Expand Up @@ -386,23 +394,13 @@ public void Save(string fileName)
bs = IntArrToByteArr(_dict);
bw.Write(bs.Length);
bw.Write(bs);

bw.Close();
fs.Close();
}

private byte[] IntArrToByteArr(int[] intArr)
{
int intSize = sizeof(int) * intArr.Length;
byte[] bytArr = new byte[intSize];
//申请一块非托管内存
IntPtr ptr = Marshal.AllocHGlobal(intSize);
//复制int数组到该内存块
Marshal.Copy(intArr, 0, ptr, intArr.Length);
//复制回byte数组
Marshal.Copy(ptr, bytArr, 0, bytArr.Length);
//释放申请的非托管内存
Marshal.FreeHGlobal(ptr);
Buffer.BlockCopy(intArr, 0, bytArr, 0, intSize);
return bytArr;
}

Expand All @@ -417,7 +415,32 @@ public void Load(string filePath)
{
var fs = File.OpenRead(filePath);
BinaryReader br = new BinaryReader(fs);
Load(br);
#if NETSTANDARD1_3
br.Dispose();
fs.Dispose();
#else
br.Close();
fs.Close();
#endif
}
/// <summary>
/// 加载Stream
/// </summary>
/// <param name="stream"></param>
public void Load(Stream stream)
{
BinaryReader br = new BinaryReader(stream);
Load(br);
#if NETSTANDARD1_3
br.Dispose();
#else
br.Close();
#endif
}

protected internal virtual void Load(BinaryReader br)
{
var length = br.ReadInt32();
_keywords = new string[length];
for (int i = 0; i < length; i++) {
Expand Down Expand Up @@ -450,19 +473,13 @@ public void Load(string filePath)

length = br.ReadInt32();
_dict = ByteArrToIntArr(br.ReadBytes(length));

br.Close();
fs.Close();
}

private int[] ByteArrToIntArr(byte[] btArr)
{
int intSize = btArr.Length / sizeof(int);
int[] intArr = new int[intSize];
IntPtr ptr = Marshal.AllocHGlobal(btArr.Length);
Marshal.Copy(btArr, 0, ptr, btArr.Length);
Marshal.Copy(ptr, intArr, 0, intArr.Length);
Marshal.FreeHGlobal(ptr);
Buffer.BlockCopy(btArr, 0, intArr, 0, intSize);
return intArr;
}
#endregion
Expand Down
Loading

0 comments on commit 43ab500

Please sign in to comment.