Ignore:
Timestamp:
Mar 11, 2009, 10:43:49 PM (13 years ago)
Author:
Thomas Schmid
Message:
  • Dictionary plugin supports multiple dictionaries by reading text files in plugin directory
  • updated project samples
Location:
trunk/CrypPlugins/Dictionary
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/Dictionary/Dictionary.cs

    r136 r236  
    215215using Cryptool.PluginBase.Miscellaneous;
    216216using System.Diagnostics;
     217using System.Collections.ObjectModel;
    217218
    218219namespace Dictionary
     
    227228    private CryptoolDictionarySettings settings = new CryptoolDictionarySettings();
    228229    private Dictionary<string, string> dicValues;
     230    private readonly string path;
     231    private const string DIC_MARKER = "081C0520-0062-49f2-9832-5F043682F45E";
     232    private List<FileInfo> dicFiles = new List<FileInfo>();
    229233    # endregion private_variables
    230234
     235    public List<FileInfo> DicFiles
     236    {
     237      get { return dicFiles; }
     238      set { dicFiles = value; }
     239    }
     240
     241    public string CurrentDicFilename
     242    {
     243      get
     244      {
     245        try
     246        {
     247          return DicFiles[settings.Dictionary].Name;
     248        }
     249        catch (Exception)
     250        {
     251          return null;
     252        }
     253      }
     254    }
     255
    231256    public CryptoolDictionary()
    232257    {
    233258      settings.PropertyChanged += settings_PropertyChanged;
     259      path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PluginDirecory);
    234260    }
    235261
     
    253279      get
    254280      {
    255         if (dicValues != null && dicValues.ContainsKey(settings.CurrentDicName))
    256           return dicValues[settings.CurrentDicName];
     281        if (dicValues != null && dicValues.ContainsKey(CurrentDicFilename))
     282          return dicValues[CurrentDicFilename];
    257283        else if (dicValues != null)
    258284        {
    259285          GetCurrentDictionary();
    260           if (dicValues.ContainsKey(settings.CurrentDicName))
    261             return dicValues[settings.CurrentDicName];
     286          if (dicValues.ContainsKey(CurrentDicFilename))
     287            return dicValues[CurrentDicFilename];
    262288        }
    263289        return null;
     
    273299                public event PluginProgressChangedEventHandler OnPluginProgressChanged;
    274300#pragma warning restore
     301
     302    private void GuiLogMessage(string message, NotificationLevel logLevel)
     303    {
     304      EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(message, this, logLevel));
     305    }
    275306
    276307    public ISettings Settings
     
    330361
    331362    [MethodImpl(MethodImplOptions.Synchronized)]
    332     private static void LoadContent(string dicName, string fileName, Dictionary<string, string> dic, char delimiter)
     363    private static void LoadContent(string dicKey, string fileName, Dictionary<string, string> dic, char delimiter)
    333364    {     
    334       if (!dic.ContainsKey(dicName))
     365      if (!dic.ContainsKey(dicKey))
    335366      {       
    336         string[] theWords = System.IO.File.ReadAllLines(fileName);
    337         dic.Add(dicName, string.Join(delimiter.ToString(), theWords));
     367        // string[] theWords = System.IO.File.ReadAllLines(fileName);
     368        StreamReader sr = new StreamReader(File.OpenRead(fileName));
     369        List<string> list = new List<string>();
     370        sr.ReadLine();
     371        sr.ReadLine();
     372        while (!sr.EndOfStream)
     373        {
     374          list.Add(sr.ReadLine());
     375        }       
     376        dic.Add(dicKey, string.Join(delimiter.ToString(), list.ToArray()));
    338377      }
    339378    }
     
    347386      {
    348387        dicValues = GetDictionary();
    349         if (dicValues != null && !dicValues.ContainsKey(settings.CurrentDicName))
    350         {
    351           string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine(PluginDirecory, settings.CurrentDicName));
     388        if (dicValues != null && !dicValues.ContainsKey(CurrentDicFilename))
     389        {
     390          string file = Path.Combine(path, CurrentDicFilename);
    352391          if (File.Exists(file))
    353392          {
    354             //
    355393            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(string.Format(Properties.Resources.loading_dic_now, new object[] { settings.CurrentDicName }), this, NotificationLevel.Info));
    356394            Stopwatch stopWatch = new Stopwatch();
    357395            stopWatch.Start();
    358             LoadContent(settings.CurrentDicName, file, dicValues, settings.Delimiter[0]);
     396            LoadContent(CurrentDicFilename, file, dicValues, settings.Delimiter[0]);
    359397            stopWatch.Stop();
    360398            // This log msg is shown on init after first using this plugin, even if event subscription
     
    374412    public void Initialize()
    375413    {
    376       GetCurrentDictionary();
     414      try
     415      {
     416        settings.Collection = new ObservableCollection<string>();
     417        // load dics
     418        DirectoryInfo directory = new DirectoryInfo(path);
     419        foreach (FileInfo fileInfo in directory.GetFiles("*.txt"))
     420        {
     421          try
     422          {
     423            StreamReader sr = new StreamReader(File.OpenRead(fileInfo.FullName));
     424            if (sr.ReadLine().Contains(DIC_MARKER))
     425            {
     426              // seems to be a dic that we want do use...
     427              // if dic description is provided we will use it...
     428              string[] dicName = sr.ReadLine().Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
     429              if (dicName.Length == 2)
     430              {
     431                settings.Collection.Add(dicName[1]);
     432                DicFiles.Add(fileInfo);
     433              }
     434            }
     435            sr.Close();
     436          }
     437          catch (Exception ex)
     438          {
     439            GuiLogMessage(ex.Message, NotificationLevel.Warning);
     440          }
     441        }
     442
     443        GetCurrentDictionary();
     444      }
     445      catch (Exception exception)
     446      {
     447        GuiLogMessage(exception.Message, NotificationLevel.Error);
     448      }
    377449    }
    378450
  • trunk/CrypPlugins/Dictionary/DictionarySettings.cs

    r4 r236  
    209209using Cryptool.PluginBase;
    210210using System.ComponentModel;
     211using System.Collections.ObjectModel;
     212using System.IO;
    211213
    212214namespace Dictionary
     
    214216  public class CryptoolDictionarySettings : ISettings
    215217  {
    216     private const string GermanDic = "Dictionary_wordsGER.txt";
    217     private const string EnglishDic = "Dictionary_2of12.txt";
    218    
    219     private WordDics currentDictionary;
     218    # region private_variables
     219    private int currentDictionary;
     220    private ObservableCollection<string> collection = new ObservableCollection<string>();
     221    # endregion private_variables
    220222
    221223    public string CurrentDicName
     
    223225      get
    224226      {
    225         switch (currentDictionary)
    226         {
    227           case WordDics.german:
    228             return GermanDic;
    229           case WordDics.english:
    230             return EnglishDic;
    231           default:
    232             return null;
    233         }
    234       }
    235     }
    236 
    237     public enum WordDics
    238     {
    239       german, english
    240     }
    241 
    242     [ContextMenu("Dictionary", "Select dictionary with keywords.", 0, DisplayLevel.Beginner, ContextMenuControlType.ComboBox, null, new string[] { "German", "English" })]
    243     [TaskPane("Dictionary", "Select dictionary with keywords.", "", 0, false, DisplayLevel.Beginner, ControlType.ComboBox, new string[] { "German", "English" })]
     227        try
     228        {
     229          return Collection[currentDictionary];
     230        }
     231        catch (Exception)
     232        {
     233          return null;
     234        }
     235      }
     236    }
     237
     238    [TaskPane("Dictionary", "Select dictionary with keywords.", "", 0, false, DisplayLevel.Beginner, ControlType.DynamicComboBox, new string[] { "Collection" })]
    244239    public int Dictionary
    245240    {
    246       get { return (int)currentDictionary; }
     241      get { return currentDictionary; }
    247242      set
    248243      {
    249         if (value != (int)currentDictionary)
     244        if (value != currentDictionary)
    250245        {
    251246          HasChanges = true;
    252           this.currentDictionary = (WordDics)value;
     247          this.currentDictionary = value;
    253248          OnPropertyChanged("Dictionary");
    254249        }
     250      }
     251    }
     252
     253    public ObservableCollection<string> Collection
     254    {
     255      get { return collection; }
     256      set
     257      {
     258        if (value != collection)
     259        {
     260          collection = value;
     261        }
     262        OnPropertyChanged("Collection");
    255263      }
    256264    }
  • trunk/CrypPlugins/Dictionary/Dictionary_2of12.txt

    r4 r236  
     1dictionary marker:{081C0520-0062-49f2-9832-5F043682F45E}
     2dictionary name:english
    13a
    24aardvark
  • trunk/CrypPlugins/Dictionary/Dictionary_wordsGER.txt

    r4 r236  
     1dictionary marker:{081C0520-0062-49f2-9832-5F043682F45E}
     2dictionary name:german
    13ABC
    24ABM
Note: See TracChangeset for help on using the changeset viewer.