Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c在用户生成的数组句子中单词的位置_C#_.net - Fatal编程技术网

C# c在用户生成的数组句子中单词的位置

C# c在用户生成的数组句子中单词的位置,c#,.net,C#,.net,新的网站,所以抱歉打破任何协议等,我已经阅读了指南 我花了很长时间四处寻找解决我遇到的一个小问题的办法。例如,我只想返回一个句子中所有单词的位置。由于我希望用户生成一个句子,所以正则表达式匹配是没有帮助的。我试过IndexOf,但没能取得多大进展 Stackoverflow帮助开发人员完成开发任务,是一种有用的开发人员资源 1234567891011213315 返回位置,因为开发者是一个重复的单词,所以它引用回单词的原始位置 int wordPosition = 0; string userS

新的网站,所以抱歉打破任何协议等,我已经阅读了指南

我花了很长时间四处寻找解决我遇到的一个小问题的办法。例如,我只想返回一个句子中所有单词的位置。由于我希望用户生成一个句子,所以正则表达式匹配是没有帮助的。我试过IndexOf,但没能取得多大进展

Stackoverflow帮助开发人员完成开发任务,是一种有用的开发人员资源

1234567891011213315

返回位置,因为开发者是一个重复的单词,所以它引用回单词的原始位置

int wordPosition = 0;
string userSentence;

Console.Write("Input sentence");
userSentence = Console.ReadLine();

Console.Write(String.IndexOf(wordPosition));
Console.WriteLine(userSentence.Substring(userSentence.IndexOf(wordPosition)));

最简单的方法可能是为迄今为止遇到的单词创建词典。比如:

Dictionary<string, int> words = new Dictionary<string, int>();
我假设您可以编写代码来迭代语句中的单词?此外,此解决方案将具有不同大小写的单词视为不同的单词。如果希望它不区分大小写,则可以在搜索和插入字典时将所有内容转换为小写,尽管更好的方法是在创建字典时使用不区分大小写的比较器:

var words = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);

这是我想出的一个非常重要的解决方案。不过,我做了一些假设:

我们不关心尾随标点符号 我们不在乎数字词 我不确定调用Substring的值是多少,因为我们跟踪的是单词索引,而不是字符索引。 另外,在您的示例中,“帮助”也是一个重复的单词

void Main()
{
    var sentence = "Stackoverflow helps developers, as it helps with development tasks, and is a useful developers resource. A non-trivial solution involves edge cases.";

    var wordTracker = new WordTracker(sentence);

    wordTracker.WriteAll();
    Console.WriteLine(wordTracker.IndexOf("developers")); //Writes 3
    Console.WriteLine(wordTracker.IndexOf("notfound")); //Writes -1
    Console.WriteLine(wordTracker.IndexOf("non-trivial")); //Writes 17
    Console.WriteLine(wordTracker.WordAt(13)); //Writes "useful"
}

public class WordTracker {
    private static readonly Regex _wordRegex = new Regex(@"[\S]*\w");
    private readonly Dictionary<string, int> _wordPositions = new Dictionary<string, int>();
    private List<WordPosition> _words;

    public WordTracker(string input) {
        BuildWordPositions(input);
    }

    public int IndexOf(string word) {
        int index;
        return _wordPositions.TryGetValue(word, out index)
            ? index + 1 //From the example, we're 1-indexed
            : -1;
    }

    public string WordAt(int index)
    {
        index = index - 1; //From the example, we're 1-indexed
        if (index < 0) throw new ArgumentException("Index must be positive");
        if (index >= _words.Count) throw new ArgumentOutOfRangeException("index");

        return _words[index].Word;
    }

    public void WriteAll() {
        foreach (var kvp in _wordPositions) {
            Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
        }
    }

    private void BuildWordPositions(string input)
    {
        _words = input.Split(' ')
            .Select((m, i) => new WordPosition(i, m)).ToList();
        var wordGroups = _words.GroupBy(m => m.Word);

        foreach (var wordGroup in wordGroups) {
            _wordPositions.Add(wordGroup.Key, wordGroup.First().Index);
        }
    }

    private class WordPosition
    {
        public WordPosition(int index, string word) {
            Index = index;
            Word = _wordRegex.Match(word).Value.ToLower();
        }

        public int Index { get; private set; }
        public string Word { get; private set; }
    }
}

我会认为,既然你说了单词,你就想忽略空白

using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

int placement = 1;
Regex.Split("your string here", @"\s+").ToList().ForEach
(x => { Console.WriteLine(x + "[" + placement + "]"); placement++; });

据我所知,没有一个库方法可以做到这一点。你有没有试过这样做的代码?一般来说,你更喜欢转换成大写。有很多种语言。G希腊文,其中一个和同一个字母希腊文:Sigma可以有多个小写表示。一般来说,我更喜欢使用不区分大小写的比较器,因为它可以处理所有这些,而不必知道哪些语言有这些怪癖,以及它们是否双向。但我提供这个是为了简单。事实上,这才是真正的方向。你为什么不在你的答案中写上呢?但我提供这个是为了简单。当人们提出问题表示他们可能不太熟悉编程时,我尽量不通过提供学术上完美的解决方案来混淆手头的问题。我不知道用户是否希望区分案例,我不希望混淆问题。一旦他掌握了算法,就可以在其他语言中使用它。但我可以把它作为一个评论。在我看来,更好的解决方案甚至是更简单的一个。。。但重点仍然是:如果转换,您应该根据给定的原因转换为大写。很抱歉被渗透,但我更喜欢从一开始就向人们暗示正确的方法…非常感谢你花时间和精力提供代码,我现在需要出去,所以我明天会提供一些进一步的反馈,并分配一个正确的答案等。再次感谢。对于删除空格和标点符号,我有一些非常相似的东西,谢谢你的评论。
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

int placement = 1;
Regex.Split("your string here", @"\s+").ToList().ForEach
(x => { Console.WriteLine(x + "[" + placement + "]"); placement++; });