?? dictionary.cs
字號(hào):
using System;using System.IO;using System.Text;using System.Collections;using System.Diagnostics;using System.Windows.Forms;namespace DLTool{ [Serializable] public class DictionaryDefinition { public string Language = ""; public string Filename = ""; public int CodePage = -1; public DictionaryDefinition() { Language = ""; Filename = ""; CodePage = -1; } public override bool Equals( object o ) { DictionaryDefinition def = o as DictionaryDefinition; if( def == null ) return false; return( def.Filename == this.Filename && def.CodePage == this.CodePage ); } public override int GetHashCode() { return Language.GetHashCode() ^ Filename.GetHashCode() ^ CodePage.GetHashCode(); } } public class Dictionary { public int CodePage = 950;
public class Entry { public string Text, TextTrad, TextSimp; public byte[] Data; public string Pronounciation; public string[] Translations; public bool showBoth = false, searchedSimp; public override string ToString() { if (TextTrad != null && TextSimp != null)
{ if (showBoth == true)
{
if (TextTrad != "" && TextSimp != "" && TextTrad != TextSimp)
{ Text = TextTrad + " " + TextSimp; }
else if (TextTrad != "" && TextSimp != "" && TextTrad == TextSimp)
{ Text = TextSimp; } }
else
{ if (searchedSimp == true) Text = TextSimp; else Text = TextTrad; } } string result = Text + " [" + Pronounciation + "] /"; foreach( string e in Translations ) { result = result + e + "/"; } return result; } }; public enum MatchMode { Loose, Exact, Subchars, Subphrases, ReverseLookup, Pronounciation } public bool showBothChars = false; private bool m_canLookup; public bool CanLookup { get { return m_canLookup; } } public ArrayList m_list; public Dictionary() { m_list = new ArrayList(); } public ArrayList Lookup(string text, MatchMode mode) { if( ! this.CanLookup ) return new ArrayList(); if( text == string.Empty ) return new ArrayList(); ArrayList exactMatches = new ArrayList(); if (this.CodePage == 65001) { switch (mode) { case MatchMode.Exact: foreach( Entry e in m_list ) { e.showBoth = showBothChars; if( e.TextSimp.Length < text.Length ) continue; if( e.TextSimp == text)
{ exactMatches.Add( e ); e.searchedSimp = true; continue; }
else if( e.TextTrad == text)
{ exactMatches.Add( e ); e.searchedSimp = false; } return exactMatches; case MatchMode.Loose: { ArrayList initialMatches = new ArrayList(); ArrayList looseMatches = new ArrayList(); foreach( Entry e in m_list ) { e.showBoth = showBothChars; if( e.TextSimp.Length < text.Length ) continue; if( e.TextSimp == text ){ exactMatches.Add( e ); e.searchedSimp = true; } else if( e.TextTrad == text ){ exactMatches.Add( e ); e.searchedSimp = false; } else if ( e.TextSimp.StartsWith(text) ){ initialMatches.Add(e); e.searchedSimp = true; } else if ( e.TextTrad.StartsWith(text) ){ initialMatches.Add(e); e.searchedSimp = false; } else if( e.TextSimp.IndexOf(text) >= 0 ){ looseMatches.Add(e); e.searchedSimp = true; } else if( e.TextTrad.IndexOf(text) >= 0 ){ looseMatches.Add( e ); e.searchedSimp = false; } } looseMatches.InsertRange(0, initialMatches); looseMatches.InsertRange(0, exactMatches); return looseMatches; } case MatchMode.Pronounciation: { ArrayList initialMatches = new ArrayList(); ArrayList looseMatches = new ArrayList(); foreach( Entry e in m_list ) { if( e.Pronounciation.Length < text.Length ) continue; if( e.Pronounciation == text ) exactMatches.Add( e ); else if ( e.Pronounciation.StartsWith( text ) ) initialMatches.Add( e ); else if( e.Pronounciation.IndexOf( text ) >= 0 ) looseMatches.Add( e ); } looseMatches.InsertRange(0, initialMatches); looseMatches.InsertRange(0, exactMatches); return looseMatches; } case MatchMode.Subchars: for( int i=0; i < text.Length; i++ ) { string s = text.Substring(i,1); foreach( Entry e in m_list ) { e.showBoth = showBothChars; if( e.TextSimp == s ){ exactMatches.Add(e); e.searchedSimp = true; } else if( e.TextTrad == s ){ exactMatches.Add(e); e.searchedSimp = false; } } } return exactMatches; case MatchMode.Subphrases: for( int i=0; i < text.Length; i++ ) { string s = text.Substring(i); int longestMatchLen = 0; ArrayList LongestMatches = new ArrayList(); bool matchedOnce = false; for( int j=1; j <= s.Length; j++ ) { string t = s.Substring(0,j); bool matchedLast = false; foreach( Entry e in m_list ) { e.showBoth = showBothChars; if( e.TextSimp.Length < t.Length) continue; if( e.TextSimp == t) { e.searchedSimp = true; if( j > longestMatchLen ) { LongestMatches.Clear(); longestMatchLen = j; } LongestMatches.Add( e ); matchedOnce = true; matchedLast = true; } else if (e.TextTrad == t) { e.searchedSimp = false; if( j > longestMatchLen ) { LongestMatches.Clear(); longestMatchLen = j; } LongestMatches.Add( e ); matchedOnce = true; matchedLast = true; } } if( matchedOnce && ! matchedLast ) break; } if( LongestMatches.Count > 0 ) { exactMatches.AddRange( LongestMatches ); i += longestMatchLen - 1; } } return exactMatches; case MatchMode.ReverseLookup: text = text.ToLower(); foreach( Entry e in m_list ) { foreach( string s in e.Translations ) { if( s.Length < text.Length ) continue; if( s.ToLower().IndexOf( text ) >= 0 ) exactMatches.Add( e ); } } return exactMatches; } return null; }
else
{ switch (mode)
{
case MatchMode.Exact:
foreach( Entry e in m_list )
{
if( e.Text.Length < text.Length ) continue;
if( e.Text == text ) exactMatches.Add( e );
}
return exactMatches;
case MatchMode.Loose:
{
ArrayList initialMatches = new ArrayList();
ArrayList looseMatches = new ArrayList();
foreach( Entry e in m_list )
{
if( e.Text.Length < text.Length ) continue;
if( e.Text == text ) exactMatches.Add( e );
else if ( e.Text.StartsWith( text ) ) initialMatches.Add( e );
else if( e.Text.IndexOf( text ) >= 0 ) looseMatches.Add( e );
}
looseMatches.InsertRange(0, initialMatches);
looseMatches.InsertRange(0, exactMatches);
return looseMatches;
}
case MatchMode.Pronounciation:
{
ArrayList initialMatches = new ArrayList();
ArrayList looseMatches = new ArrayList();
foreach( Entry e in m_list )
{
if( e.Pronounciation.Length < text.Length ) continue;
if( e.Pronounciation == text ) exactMatches.Add( e );
else if ( e.Pronounciation.StartsWith( text ) ) initialMatches.Add( e );
else if( e.Pronounciation.IndexOf( text ) >= 0 ) looseMatches.Add( e );
}
looseMatches.InsertRange(0, initialMatches);
looseMatches.InsertRange(0, exactMatches);
return looseMatches;
}
case MatchMode.Subchars:
for( int i=0; i < text.Length; i++ )
{
string s = text.Substring(i,1);
foreach( Entry e in m_list )
{
if( e.Text == s ) exactMatches.Add( e );
}
}
return exactMatches;
case MatchMode.Subphrases:
for( int i=0; i < text.Length; i++ )
{
string s = text.Substring(i);
int longestMatchLen = 0;
ArrayList LongestMatches = new ArrayList();
bool matchedOnce = false;
for( int j=1; j <= s.Length; j++ )
{
string t = s.Substring(0,j);
bool matchedLast = false;
foreach( Entry e in m_list )
{
if( e == null ) continue;
if( e.Text.Length < t.Length ) continue;
if( e.Text == t )
{
if( j > longestMatchLen )
{
LongestMatches.Clear();
longestMatchLen = j;
}
LongestMatches.Add( e );
matchedOnce = true;
matchedLast = true;
}
}
if( matchedOnce && ! matchedLast ) break;
}
if( LongestMatches.Count > 0 )
{
exactMatches.AddRange( LongestMatches );
i += longestMatchLen - 1;
}
}
return exactMatches;
case MatchMode.ReverseLookup:
text = text.ToLower();
foreach( Entry e in m_list )
{
foreach( string s in e.Translations )
{
if( s.Length < text.Length ) continue;
if( s.ToLower().IndexOf( text ) >= 0 ) exactMatches.Add( e );
}
}
return exactMatches;
}
return null; } } public void SetBothChars(bool choice) { showBothChars = choice; } public void LoadFile(string file, int codePage) { m_list.Clear(); StreamReader reader; this.CodePage = codePage; Encoding enc = Encoding.GetEncoding(CodePage); try { reader = new StreamReader(file, Encoding.GetEncoding(CodePage)); } catch( FileNotFoundException ) { MessageBox.Show("Couldn't load dictionary file \"" + file + "\"!!"); m_canLookup = false; return; } int numEntries = 0; Trace.WriteLine("Displaying first 20 entries", "Dictionary");
if (this.CodePage == 65001)
{ while(true) { string currentLine = reader.ReadLine(); if( currentLine == null ) break; if( currentLine[0] == '#' ) continue; Entry newEntry = new Entry(); int mark1 = currentLine.IndexOf(" "); if( mark1 < 0 ) mark1 = currentLine.IndexOf("/"); if( mark1 < 0 ) { throw new System.IO.FileLoadException("bad file format"); } int mark2 = currentLine.IndexOf(' ', mark1+1); int mark3 = currentLine.IndexOf(']', mark2+2); newEntry.TextTrad = currentLine.Substring(0, mark1); newEntry.TextSimp = currentLine.Substring(mark1+1, mark1); newEntry.Text = ""; newEntry.Data = enc.GetBytes(newEntry.Text); string defstring; if( mark3 >= 0 ) { newEntry.Pronounciation = currentLine.Substring(mark2 + 2, mark3 - (mark2 + 2)); defstring = currentLine.Substring(mark3+3); } else { newEntry.Pronounciation = ""; defstring = currentLine.Substring( mark2 + 2 ); } defstring = defstring.TrimEnd('/'); defstring = defstring.TrimStart('/'); newEntry.Translations = defstring.Split('/'); m_list.Add( newEntry ); numEntries++; if( numEntries < 20 ) { Trace.WriteLine(newEntry, "Dictionary"); } } }
if (this.CodePage != 65001)
{ while(true)
{
string currentLine = reader.ReadLine();
if( currentLine == null ) break;
if( currentLine[0] == '#' ) continue;
Entry newEntry = new Entry();
int mark1 = currentLine.IndexOf(" ");
if( mark1 < 0 ) mark1 = currentLine.IndexOf("/");
if( mark1 < 0 )
{
throw new System.IO.FileLoadException("bad file format");
}
int mark2 = currentLine.IndexOf(']', mark1+2);
newEntry.Text = currentLine.Substring(0, mark1);
newEntry.Data = enc.GetBytes(newEntry.Text);
string defstring;
if( mark2 >= 0 )
{
newEntry.Pronounciation = currentLine.Substring(mark1 + 2, mark2 - (mark1 + 2));
defstring = currentLine.Substring(mark2+3);
}
else
{
newEntry.Pronounciation = "";
defstring = currentLine.Substring( mark1 + 2 );
}
defstring = defstring.TrimEnd('/');
defstring = defstring.TrimStart('/');
newEntry.Translations = defstring.Split('/');
m_list.Add( newEntry );
numEntries++;
if( numEntries < 20 )
{
Trace.WriteLine(newEntry, "Dictionary");
}
} } reader.Close(); Trace.WriteLine("Scanned " + numEntries + " dictionary entries", "Dictionary"); m_canLookup = true; } }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -