在找到一个已知的单词后向后遍历一个字符数组

本文关键字:一个 数组 字符 单词 遍历 | 更新日期: 2023-09-27 18:02:37

我有一个项目,我在c#工作。我有两个字符数组。一个是句子,一个是单词。我必须遍历句子数组直到找到一个与转化为单词数组的单词相匹配的单词我想知道的是,一旦我找到了这个单词,我该如何在句子数组中向后迭代到我找到的这个单词的长度与单词数组相同的地方?

代码:

String wordString = "(Four)";
String sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
char[] wordArray = wordString.ToCharArray();
List<String> words = sentenceString.Split(' ').ToList<string>();
//This would be the part where I iterate through sentence
foreach (string sentence in sentArray)
{
     //Here I would need to find where the string of (Four) and trim it and see if it equals the wordString. 
     if (sentence.Contains(wordString)
     {
         //At this point I would need to go back the length of wordString which happens to be four places but I'm not sure how to do this.  And for each word I go back in the sentence I need to capture that in another string array.
      }

我不知道我是否说得足够清楚,但如果我不清楚,请随时问…提前谢谢你。这还应该归的是"乌拉圭队的家禽"。所以基本上用例是对于括号中的字母数逻辑应该返回括号中单词前的相同单词数

在找到一个已知的单词后向后遍历一个字符数组

我们是你。对于这次演习,我没有什么问题要问。如果Word (4)在开头,它不应该返回?还是全部返回字符串?由于 4的长度等于4想象一下,如果该单词作为句子中的第二个单词出现,它应该只返回第一个单词还是返回4个单词,甚至包括(四个)单词。

我的解决方案是最懒的,我只是看到你的问题,决定帮助你。

  • 我的解决方案假设它返回(四)之前的所有单词,如果长度大于(四)单词之前的单词。
  • 我的解决方案返回空字符串,如果单词(四)在开头。
  • 我的解返回(四个)(4)个字在(四个)字之前的长度。

这不是我最好的方法。

我看到下面的代码:
        string wordString = "(Four)";
        string sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
        //Additionally you can add splitoption to remove the empty word on split function bellow
        //Just in case there are more space in sentence.
        string[] splitedword = sentenceString.Split(' ');
        int tempBackposition = 0;
        int finalposition = 0;
        for (int i = 0; i < splitedword.Length; i++)
        {
            if (splitedword[i].Contains(wordString))
            {
                finalposition = i;
                break;
            }
        }
        tempBackposition = finalposition - wordString.Replace("(","").Replace(")","").Length;
        string output = "";
        tempBackposition= tempBackposition<0?0:tempBackposition;
        for (int i = tempBackposition; i < finalposition; i++)
        {
            output += splitedword[i] + " ";
        }
        Console.WriteLine(output);
        Console.ReadLine();

如果不是你想要的,你能在上面回答我的问题吗?或者帮助我理解这是错的

int i ;
string outputString = (i=sentenceString.IndexOf(wordString))<0 ?     
                          sentenceString : sentenceString.Substring(0,i) ;
var wordString = "(Four)";
            int wordStringInt = 4; // Just do switch case to convert your string to int
            var sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
            var sentenceStringArray = sentenceString.Split(' ').ToList();
            int wordStringIndexInArray = sentenceStringArray.IndexOf(wordString) - 1;
            var stringOutPut = "";
            if (wordStringIndexInArray > 0 && wordStringIndexInArray > wordStringInt)
            {
                stringOutPut = "";
                while (wordStringInt > 0)
                {
                    stringOutPut = sentenceStringArray[wordStringInt] + " " + stringOutPut;
                    wordStringInt--;
                }
            }

您匹配的内容有点复杂,因此对于更通用的解决方案,您可以使用正则表达式。

首先声明要搜索的对象:

string word = "(Four)";
string sentence = "Maybe the fowl of Uruguay repeaters (Four) will be found";
然后,我们将使用正则表达式搜索该字符串中的单词。因为我们不想匹配空格,我们需要知道每个匹配实际上从哪里开始,我们需要知道括号内的单词,我们告诉它我们可以选择使用开始和结束括号,但我们也希望它们的内容匹配:
var words = Regex.Matches(sentence, @"['p{Ps}]*(?<Content>['w]+)['p{Pe}]*").Cast<Match>().ToList();

['p{Ps}]表示我们想要打开标点符号([{等,而*表示零或更多。

后面是一个名为Content的子捕获(由?<Content>指定),包含一个或多个单词字符。在结尾,我们指定我们想要零个或多个结束标点符号。

然后我们需要在匹配列表中找到这个单词:

var item = words.Single(x => x.Value == word);

然后我们需要找到这个项目的索引:

int index = words.IndexOf(item);

现在我们只需要知道内容的长度:

var length = item.Groups["Content"].Length;

这个长度用于返回字符串4 words

var start = words.Skip(index - length).First();

现在我们需要的都有了:

var result = sentence.Substring(start.Index, item.Index - start.Index);

结果应该包含fowl of Uruguay repeaters

edit:从单词中计算计数可能比从内容中计算计数要简单得多。在这种情况下,完整的代码应该如下:

string word = "(Four)";
string sentence = "Maybe the fowl of Uruguay repeaters (Four) will be found";
var wordMatch = Regex.Match(word, @"['p{Ps}]*(?<Content>['w]+)['p{Pe}]*");
var length = wordMatch.Groups["Content"].Length;
var words = Regex.Matches(sentence, @"'S+").Cast<Match>().ToList();
var item = words.Single(x => x.Value == word);
int index = words.IndexOf(item);

var start = words.Skip(index - length).First();
var result = sentence.Substring(start.Index, item.Index - start.Index);

'S+在本例中表示"匹配一个或多个非空白字符"。

您应该尝试如下操作,在找到数字单词后使用Array.Copy。您仍然需要正确地实现ConvertToNum函数(现在它是硬编码的),但这应该是一个快速而简单的解决方案。

string[] GetWords()
{
    string sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
    string[] words = sentenceString.Split();
    int num = 0;
    int i; // scope of i should remain outside the for loop
    for (i = 0; i < words.Length; i++)
    {
        string word = words[i];
        if (word.StartsWith("(") && word.EndsWith(")"))
        {
            num = ConvertToNum(word.Substring(1, word.Length - 1));
            // converted the number word we found, so we break
            break;
        }
    }
    if (num == 0)
    {
        // no number word was found in the string - return empty array
        return new string[0];
    }
        // do some extra checking if number word exceeds number of previous words
        int startIndex = i - num;
        // if it does - just start from index 0
        startIndex = startIndex < 0 ? 0 : startIndex;
        int length = i - startIndex;
        string[] output = new string[length];
        Array.Copy(words, startIndex, output, 0, length);
        return output;
}

// Convert the number word to an integer
int ConvertToNum(string numberStr)
{
    return 4; // you should implement this method correctly
}

参见-将单词(字符串)转换为Int,以获得实现ConvertToNum解决方案的帮助。显然,它可以根据您希望处理的数字范围进行简化。

这是我的解决方案,我根本不使用正则表达式,为了便于理解:

        static void Main() {
            var wordString = "(Four)";
            int wordStringLength = wordString.Replace("(","").Replace(")","").Length; 
            //4, because i'm assuming '(' and ')' doesn't count. 
            var sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
            //Transform into a list of words, ToList() to future use of IndexOf Method
            var sentenceStringWords = sentenceString.Split(' ').ToList();
            //Find the index of the word in the list of words
            int wordIndex = sentenceStringWords.IndexOf(wordString);
            //Get a subrange from the original list of words, going back x Times the legnth of word (in this case 4),
            var wordsToConcat = sentenceStringWords.GetRange(wordIndex-wordStringLength, wordStringLength);
            //Finally concat the output;
            var outPut = string.Join(" ", wordsToConcat);
            //Output: fowl of Uruguay repeaters
        }

我有一个解决方案:

string wordToMatch = "(Four)";
            string sentence = "Maybe the fowl of Uruguay repeaters (Four) will be found";
            if (sentence.Contains(wordToMatch))
            {
                int length = wordToMatch.Trim(new[] { '(', ')' }).Length;
                int indexOfMatchedWord = sentence.IndexOf(wordToMatch);
                string subString1 = sentence.Substring(0, indexOfMatchedWord);
                string[] words = subString1.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var reversed = words.Reverse().Take(length);
                string result = string.Join(" ", reversed.Reverse());
                Console.WriteLine(result);
                Console.ReadLine();
            }

很可能这可以在性能方面得到改进,但我有一种感觉,你并不关心。确保你使用的是"系统"。Linq的

当输入不完整时,我假设空返回,请随时纠正我。在你的帖子中不是100%清楚应该如何处理这个问题。

    private string getPartialSentence(string sentence, string word)
    {
        if (string.IsNullOrEmpty(sentence) || string.IsNullOrEmpty(word))
            return string.Empty;
        int locationInSentence = sentence.IndexOf(word, StringComparison.Ordinal);
        if (locationInSentence == -1)
            return string.Empty;
        string partialSentence = sentence.Substring(0, locationInSentence);
        string[] words = partialSentence.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
        int nbWordsRequired = word.Replace("(", "").Replace(")", "").Length;
        if (words.Count() >= nbWordsRequired)
            return String.Join(" ", words.Skip(words.Count() - nbWordsRequired));
        return String.Join(" ", words);
    }

我使用枚举和关联字典将"(Four)"类型字符串与其整数值配对。您可以使用

轻松地(也许更简单)使用switch语句。
case "(Four)": { currentNumber = 4; };

我觉得enum允许更多的灵活性。

public enum NumberVerb
    {
        one = 1,
        two = 2,
        three = 3,
        four = 4,
        five = 5,
        six = 6,
        seven = 7,
        eight = 8,
        nine = 9,
        ten = 10,
    };
    public static Dictionary<string, NumberVerb> m_Dictionary
    {
        get
        {
            Dictionary<string, NumberVerb> temp = new Dictionary<string, NumberVerb>();
            temp.Add("(one)", NumberVerb.one);
            temp.Add("(two)", NumberVerb.two);
            temp.Add("(three)", NumberVerb.three);
            temp.Add("(four)", NumberVerb.four);
            temp.Add("(five)", NumberVerb.five);
            temp.Add("(six)", NumberVerb.six);
            temp.Add("(seven)", NumberVerb.seven);
            temp.Add("(eight)", NumberVerb.eight);
            temp.Add("(nine)", NumberVerb.nine);
            temp.Add("(ten)", NumberVerb.ten);
            return temp;
        }
    }
    static void Main(string[] args)
    {
        string resultPhrase = "";
        // Get the sentance that will be searched.
        Console.WriteLine("Please enter the starting sentance:");
        Console.WriteLine("(don't forget your keyword: ie '(Four)')");
        string sentance = Console.ReadLine();
        // Get the search word.
        Console.WriteLine("Please enter the search keyword:");
        string keyword = Console.ReadLine();
        // Set the associated number of words to backwards-iterate.
        int currentNumber = -1;
        try 
        {
            currentNumber = (int)m_Dictionary[keyword.ToLower()];
        }
        catch(KeyNotFoundException ex)
        {
            Console.WriteLine("The provided keyword was not found in the dictionary.");
        }
        // Search the sentance string for the keyword, and get the starting index.
        Console.WriteLine("Searching for phrase...");
        string[] words = sentance.Split(' ');
        int searchResultIndex = -1;
        for (int i = 0; (searchResultIndex == -1 && i < words.Length); i++)
        {
            if (words[i].Equals(keyword))
            {
                searchResultIndex = i;
            }
        }
        // Handle the search results.
        if (searchResultIndex == -1)
        {
            resultPhrase = "The keyword was not found.";
        }
        else if (searchResultIndex < currentNumber)
        {
            // Check the array boundaries with the given indexes.
            resultPhrase = "Error: Out of bounds!";
        }
        else
        {
            // Get the preceding words.
            for (int i = 0; i < currentNumber; i++)
            {
                resultPhrase = string.Format(" {0}{1}", words[searchResultIndex - 1 - i], resultPhrase);
            }
        }
        // Display the preceding words.
        Console.WriteLine(resultPhrase.Trim());
        // Exit.
        Console.ReadLine();
    }