2011-11-08 3 views
0

Ma question pour aujourd'hui est:
Comment puis-je faire une fonction qui pourrait trouver tous les mots correspondant au modèle?Rechercher des mots dans la liste des structures et obtenir tous les mots avec postfix

Par exemple nous avons le mot: duck et nous voulons trouver tous les mots à partir de ce mot canard.

Je suis à la recherche de la meilleure fonction de perfomance, je serais heureux si elle pourrait utiliser LINQ. Jusqu'à présent, je fait quelque chose comme ça (il ne fonctionne pas encore):

public List<List<string>> FindWordsPostfix(List<Parameters.Words> wordsChess, List<string> wordsFromDictionary, int width) 
    { 
     List<List<string>> listPostfixForstructxIndex = new List<List<string>>(); 

     foreach (Parameters.Words structx in wordsChess) 
     { 
      //1for each structx I have some parameters eg. name, length, index 
      //2for each word (name) I need find word from dict. starting that word(name) 

      //list storing lists of words for each of the structx object 
      List<string> list = new List<string>(); 

      foreach (String wordDictionary in wordsFromDictionary) 
      { 
       Match match = Regex.Match(wordDictionary, structx.word, RegexOptions.IgnoreCase); 
       if(match.Success && (match.Length > structx.length)) 
       { 
        list.Add(match.Value); 
       } 

      } 
      //add list of words to main list 
      listPostfixForstructxIndex.Add(list); 
     } 
     throw new NotImplementedException(); 
    } 

Parameters.Words est une struct contenant: string name, int length, etc... Pourquoi ma fonction est mauvaise et ne stocke pas de données?

Pourquoi ma fonction est mauvaise et ne stocke aucune donnée?

PS2. J'ai édité la question. J'ai dû nettoyer ce bordel ce que j'ai fait.

+0

Peut-être _helloing_ a été désactivé. Mais pas besoin, à SO vous pouvez commencer à écrire votre question sans dire bonjour;) – Abel

+0

Je ne suis pas tout à fait sûr de ce que vous essayez de faire même après avoir lu votre code. Je pense que vous voulez trouver des mots qui se terminent par une série de lettres ou un motif, mais je ne comprends pas pourquoi vous avez des listes dans les listes. – Dracorat

+0

@Dracorat Je permets d'éditer ma question. Vous devriez corriger ma question si mon anglais est trop mauvais. Cependant, je vais essayer d'expliquer à nouveau :) Ceci est un exemple: J'ai le mot 'ban' et je veux trouver tous les mots commençant par' ban' donc le résultat sera: nous avons 'List ' stockage: 'banner'' bans '' banquier etc J'ai fait une liste dans la liste parce que ça doit être. L'index de la liste aura besoin d'une autre fonction. Ce doit être. – deadfish

Répondre

2
if(match.Success && (match.Length > struct.dlugosc)) 

La longueur de match ne va jamais être plus longue que la longueur de la struct - la longueur du struct au minimum est celui de la chaîne, ainsi que tous les autres éléments qu'il contient. Quoi d'autre étiez-vous tester après match.Success?

Si vous voulez un code correspondant à ce que je pense que vous demandez, ce qui suit fonctionne un charme:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

using System.Text.RegularExpressions; 

namespace Word_Ending_Finder 
{ 
    public partial class Form1 : Form 
    { 
     private List<string> WordsToFind = new List<string>(); 
     private List<MySpecialStringStruct> PassagesToSearch = new List<MySpecialStringStruct>(); 

     public Form1() 
     { 
      InitializeComponent(); 
      PassagesToSearch.Add(new MySpecialStringStruct("This is a test passage with a test ending.", 0)); 
      PassagesToSearch.Add(new MySpecialStringStruct("This is a second test passage with a test ending.", 0)); 
      PassagesToSearch.Add(new MySpecialStringStruct("This is a third passage that won't match.", 0)); 

      WordsToFind.Add(@"ing\b"); 
      WordsToFind.Add(@"\bsecond\b"); 
      WordsToFind.Add(@"\bgarbage text\b"); 
     } 

     private void bnGo_Click(object sender, EventArgs e) 
     { 
      txtResults.Text = ""; 
      string Separator = "------------------------------------------"; 

      StringBuilder NewText = new StringBuilder(); 
      foreach (string SearchWord in WordsToFind) 
      { 
       NewText.AppendLine(string.Format("Now searching {0}", SearchWord)); 
       List<MatchValue> Results = FindPassages(PassagesToSearch, SearchWord); 
       if (Results.Count == 0) 
       { 
        NewText.AppendLine("No Matches Found"); 
       } 
       else 
       { 
        foreach (MatchValue ThisMatch in Results) 
        { 
         NewText.AppendLine(string.Format("In passage \"{0}\":", ThisMatch.WhichStringStruct.Passage)); 
         foreach (Match M in ThisMatch.MatchesFound) 
         { 
          NewText.AppendLine(string.Format("\t{0}", M.Captures[0].ToString())); 
         } 
        } 
       } 
       NewText.AppendLine(Separator); 
      } 

      txtResults.Text = NewText.ToString(); 
     } 

     private List<MatchValue> FindPassages(List<MySpecialStringStruct> PassageList, string WhatToFind) 
     { 
      Regex MatchPattern = new Regex(WhatToFind); 
      List<MatchValue> ReturnValue = new List<MatchValue>(); 
      foreach (MySpecialStringStruct SearchTarget in PassageList) 
      { 
       MatchCollection MatchList = MatchPattern.Matches(SearchTarget.Passage); 
       if (MatchList.Count > 0) 
       { 
        MatchValue FoundMatchResult = new MatchValue(); 
        FoundMatchResult.WhichStringStruct = SearchTarget; 
        FoundMatchResult.MatchesFound = MatchList; 
        ReturnValue.Add(FoundMatchResult); 
       } 
      } 
      return ReturnValue; 
     } 
    } 

    public class MatchValue 
    { 
     public MySpecialStringStruct WhichStringStruct; 
     public MatchCollection MatchesFound; 
    } 

    public struct MySpecialStringStruct 
    { 
     public string Passage; 
     public int Id; 

     public MySpecialStringStruct(string passage, int id) 
     { 
      Passage = passage; 
      Id = id; 
     } 
    } 
} 

La sortie:

Now searching ing\b 
In passage "This is a test passage with a test ending.": 
ing 
In passage "This is a second test passage with a test ending.": 
ing 
------------------------------------------ 
Now searching \bsecond\b 
In passage "This is a second test passage with a test ending.": 
second 
------------------------------------------ 
Now searching \bgarbage text\b 
No Matches Found 
------------------------------------------ 
+0

struct.dlugosc est la longueur du mot dans struct of index x – deadfish

Questions connexes