基本信息
源码名称:lucene搜索 批量添加 自定义字典关键词 分词 实例源码
源码大小:2.28M
文件格式:.zip
开发语言:C#
更新时间:2015-12-13
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PanGu;
using PanGu.Dict;
namespace DictManage
{
public partial class FormMain : Form
{
WordDictionary _WordDict = null;
String m_DictFileName;
string _Version = "";
private int Count
{
get
{
if (_WordDict != null)
{
return _WordDict.Count;
}
else
{
return 0;
}
}
}
public FormMain()
{
InitializeComponent();
}
private void ShowCount()
{
labelCount.Text = Count.ToString();
}
private void openBinDictFile13ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialogDict.RestoreDirectory = true;
openFileDialogDict.FileName = "Dict.dct";
openFileDialogDict.Filter = "Dictionay file|*.dct";
if (openFileDialogDict.ShowDialog() == DialogResult.OK)
{
try
{
DateTime old = DateTime.Now;
_WordDict = new WordDictionary();
_WordDict.Load(openFileDialogDict.FileName, out _Version);
TimeSpan s = DateTime.Now - old;
statusStrip.Items[0].Text = s.TotalMilliseconds.ToString() "ms";
}
catch (Exception e1)
{
MessageBox.Show(String.Format("Can not open dictionary, errmsg:{0}", e1.Message));
return;
}
panelMain.Enabled = true;
m_DictFileName = openFileDialogDict.FileName;
this.Text = "V" _Version " " openFileDialogDict.FileName;
ShowCount();
}
}
private void saveBinDictFile13ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_WordDict == null)
{
return;
}
saveFileDialogDict.RestoreDirectory = true;
saveFileDialogDict.FileName = "Dict.dct";
saveFileDialogDict.Filter = "Dictionay file|*.dct";
if (saveFileDialogDict.ShowDialog() == DialogResult.OK)
{
FormInputDictVersion frmInputDictVersion = new FormInputDictVersion();
frmInputDictVersion.Version = _Version;
if (frmInputDictVersion.ShowDialog() == DialogResult.OK)
{
_WordDict.Save(saveFileDialogDict.FileName,
frmInputDictVersion.Version);
}
}
}
private void buttonSearch_Click(object sender, EventArgs e)
{
if (textBoxSearch.Text.Trim() == "")
{
return;
}
List<SearchWordResult> result = _WordDict.Search(textBoxSearch.Text.Trim());
result.Sort();
listBoxList.Items.Clear();
foreach (SearchWordResult word in result)
{
listBoxList.Items.Add(word);
}
label.Text = "符合條件的記錄數:" result.Count.ToString();
}
private void listBoxList_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listBoxList.SelectedIndex;
if (index < 0)
{
return;
}
object obj = listBoxList.Items[index];
SearchWordResult word = (SearchWordResult)obj;
textBoxWord.Text = word.Word.Word;
numericUpDownFrequency.Value = (decimal)word.Word.Frequency;
posCtrl.Pos = (int)word.Word.Pos;
}
private void textBoxWord_TextChanged(object sender, EventArgs e)
{
String word = textBoxWord.Text.Trim();
if (word == "")
{
buttonUpdate.Enabled = false;
buttonInsert.Enabled = false;
buttonDelete.Enabled = false;
return;
}
WordAttribute selWord = _WordDict.GetWordAttr(word);
if (selWord != null)
{
buttonUpdate.Enabled = true;
buttonInsert.Enabled = false;
buttonDelete.Enabled = true;
numericUpDownFrequency.Value = (decimal)selWord.Frequency;
posCtrl.Pos = (int)selWord.Pos;
}
else
{
buttonUpdate.Enabled = false;
buttonInsert.Enabled = true;
buttonDelete.Enabled = false;
numericUpDownFrequency.Value = 0;
posCtrl.Pos = 0;
}
}
private void buttonInsert_Click(object sender, EventArgs e)
{
_WordDict.InsertWord(textBoxWord.Text.Trim(), (double)numericUpDownFrequency.Value, (POS)posCtrl.Pos);
MessageBox.Show("增加成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowCount();
textBoxWord_TextChanged(sender, e);
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
_WordDict.UpdateWord(textBoxWord.Text.Trim(), (double)numericUpDownFrequency.Value, (POS)posCtrl.Pos);
MessageBox.Show("修改成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowCount();
textBoxWord_TextChanged(sender, e);
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("確認刪除改單詞?", "刪除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
== DialogResult.Yes)
{
_WordDict.DeleteWord(textBoxWord.Text.Trim());
MessageBox.Show("刪除成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
ShowCount();
textBoxWord_TextChanged(sender, e);
}
private void BatchInsert(String fileName, String encoder)
{
String content = PanGu.Framework.File.ReadFileToString(fileName, Encoding.GetEncoding(encoder));
String[] words = PanGu.Framework.Regex.Split(content, @"\r\n");
bool allUse = false;
WordAttribute lstWord = null;
foreach (String word in words)
{
if (word == null)
{
continue;
}
if (word.Trim() == "")
{
continue;
}
string[] strs = word.Split(new char[] { '|' });
if (strs.Length == 3)
{
try
{
POS pos = (POS)int.Parse(strs[1].Substring(2, strs[1].Length - 2),
System.Globalization.NumberStyles.HexNumber);
double frequency = double.Parse(strs[2]);
string w = strs[0].Trim();
_WordDict.InsertWord(w, frequency, pos);
continue;
}
catch
{
}
}
FormBatchInsert frmBatchInsert = new FormBatchInsert();
if (!allUse || lstWord == null)
{
frmBatchInsert.Word.Word = word.Trim();
if (frmBatchInsert.ShowDialog() == DialogResult.OK)
{
lstWord = frmBatchInsert.Word;
allUse = frmBatchInsert.AllUse;
_WordDict.InsertWord(lstWord.Word, lstWord.Frequency, lstWord.Pos);
}
}
else
{
lstWord.Word = word.Trim();
_WordDict.InsertWord(lstWord.Word, lstWord.Frequency, lstWord.Pos);
}
}
}
private void buttonBatchInsert_Click(object sender, EventArgs e)
{
openFileDialogDict.RestoreDirectory = true;
openFileDialogDict.FileName = "*.txt";
openFileDialogDict.Filter = "Text files|*.txt";
if (openFileDialogDict.ShowDialog() == DialogResult.OK)
{
try
{
FormEncoder frmEncoder = new FormEncoder();
if (frmEncoder.ShowDialog() != DialogResult.OK)
{
return;
}
BatchInsert(openFileDialogDict.FileName, frmEncoder.Encoding);
MessageBox.Show("批量增加成功,注意只有保存字典後,導入的單詞才會生效!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowCount();
}
catch(Exception e1)
{
MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ListWordsByPos(int pos)
{
List<SearchWordResult> result = _WordDict.SearchByPos((POS)pos);
result.Sort();
listBoxList.Items.Clear();
foreach (SearchWordResult word in result)
{
listBoxList.Items.Add(word);
}
label.Text = "符合條件的記錄數:" result.Count.ToString();
}
private void ListWordsByLength(int length)
{
List<SearchWordResult> result = _WordDict.SearchByLength(length);
result.Sort();
listBoxList.Items.Clear();
foreach (SearchWordResult word in result)
{
listBoxList.Items.Add(word);
}
label.Text = "符合條件的記錄數:" result.Count.ToString();
}
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
FormFind frmFind = new FormFind();
frmFind.ShowDialog();
switch (frmFind.Mode)
{
case FormFind.SearchMode.None:
listBoxList.Items.Clear();
break;
case FormFind.SearchMode.ByPos:
ListWordsByPos(frmFind.POS);
break;
case FormFind.SearchMode.ByLength:
ListWordsByLength(frmFind.Length);
break;
}
}
private void exportToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialogText.ShowDialog() == DialogResult.OK)
{
StringBuilder str = new StringBuilder();
foreach (object text in listBoxList.Items)
{
str.AppendLine(text.ToString());
}
PanGu.Framework.File.WriteString(saveFileDialogText.FileName, str.ToString(), Encoding.UTF8);
MessageBox.Show("Save OK!");
}
}
private void textBoxSearch_TextChanged(object sender, EventArgs e)
{
buttonSearch_Click(sender, e);
}
private void OpenAsTextToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialogDict.RestoreDirectory = true;
openFileDialogDict.FileName = "Dict.txt";
openFileDialogDict.Filter = "Dictionay file|*.txt";
if (openFileDialogDict.ShowDialog() == DialogResult.OK)
{
_Version = "";
try
{
DateTime old = DateTime.Now;
_WordDict = new WordDictionary();
_WordDict.Load(openFileDialogDict.FileName, true, out _Version);
TimeSpan s = DateTime.Now - old;
statusStrip.Items[0].Text = s.TotalMilliseconds.ToString() "ms";
}
catch (Exception e1)
{
MessageBox.Show(String.Format("Can not open dictionary, errmsg:{0}", e1.Message));
return;
}
panelMain.Enabled = true;
m_DictFileName = openFileDialogDict.FileName;
this.Text = "V" _Version " " openFileDialogDict.FileName;
ShowCount();
}
}
private void SaveAsTextToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_WordDict == null)
{
return;
}
saveFileDialogDict.RestoreDirectory = true;
saveFileDialogDict.FileName = "Dict.txt";
saveFileDialogDict.Filter = "Dictionay file|*.txt";
if (saveFileDialogDict.ShowDialog() == DialogResult.OK)
{
_WordDict.SaveToText(saveFileDialogDict.FileName);
}
}
}
}