【问题标题】:Similarity between the text and the name of a file. need to be more imprecise文本和文件名之间的相似性。需要更不精确
【发布时间】:2019-10-24 16:36:03
【问题描述】:

我正在创建一个软件,它允许我通过获取网站上的前 100 名以及通过我的 PC 上的路径数据库检索标题来自动按前 100 名排行榜对音乐进行排序

我已经设法在我的 datagridview 中检索到前 100 名的艺术家姓名和标题。 (https://prnt.sc/pnndw1)

我还设法在我的电脑上恢复了大部分本地音乐的本地化,但不是全部,因为敏感的案例阻止了我......

我想知道是否有人有一个解决方案,以便它比较网站给出的值和我在我的电脑上拥有的值,而不使用可能干扰示例的特殊字符:“, . | ()”等等。

我想只比较字符和/或如果可能的话比较字符串是否包含标题的所有单词。

我想解决的最大问题是:“thank u, next”这个标题的逗号在比较中没有带

例如,如果我的文件位置包含:“Ramenez la coupe à la maison” 但无论如何都不能以相同的顺序服用。

wordResponse = song name.

string wordResponse = item_text.Replace("'", "’").Replace("&", "&");

using (StreamReader myFile = File.OpenText(@"C:\MUSIQUE.txt"))
{
    int count = 0; //counts the number of times wordResponse is found.
    int lineNumber = 0;

    while (!myFile.EndOfStream)
    {
        string line = myFile.ReadLine();
        lineNumber++;
        int position = line.IndexOf(wordResponse, StringComparison.CurrentCultureIgnoreCase);
        if (position != -1)
        {
            count++;
            (dataGridView1.Rows[i].Cells[2] as DataGridViewComboBoxCell).Items.Add(line);
        }
    }

    if (count == 0)
    {
        Console.WriteLine("your word was not found!");
    }
    else
    {
        (dataGridView1.Rows[i].Cells[2] as DataGridViewComboBoxCell).Value = (dataGridView1.Rows[i].Cells[2] as DataGridViewComboBoxCell).Items[0];
    }
    Console.ReadLine();
}

【问题讨论】:

  • “我如何确定一个句子是否包含另一个句子的所有单词,忽略特定字符”?还是我误解了这个问题?
  • 简而言之,我希望在包含我音乐的所有访问路径的文件中搜索网站给出的标题,并返回所有匹配的潜在标题,但逗号等字符,括号...我希望它不计入比较中
  • 但顺序也不重要,对吗?您说,“如果我的文件位置包含:“Ramenez la coupe à la maison”,但顺序不一样,无论如何都要接受它。” 这是否意味着如果搜索词是 “maison la coupe",上面的标题会被返回吗?
  • 是的,如果名称只是“coupe à la maison”,它会在我的组合框选项中返回包含此文本的所有文件,同时返回

标签: c# file-search


【解决方案1】:

如果我理解正确,这个问题可以归结为:

“如何判断一个句子是否包含另一句的所有单词,忽略特定字符”

我能想到的最简单的方法是:

  1. 从源字符串和搜索字符串中删除特殊字符
  2. 从每个中获取所有“单词”(按空格字符拆分)
  3. 查看搜索字符串中的所有单词是否都包含在源字符串的单词列表中

如果是这样,那么这里有一些辅助方法可以做到这一点:

public static bool ContainsWords(string source, string search, 
    List<char> charsToIgnore = null, bool ignoreCase = true)
{
    var comparer = ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
    var sourceWords = GetWords(RemoveChars(source, charsToIgnore));
    var searchWords = GetWords(RemoveChars(search, charsToIgnore));

    return searchWords.All(searchWord => sourceWords.Contains(searchWord, comparer));
}

public static string RemoveChars(string input, List<char> charsToRemove)
{
    // If input or charsToIgnore are null or empty, return input
    if (string.IsNullOrEmpty(input) || charsToRemove?.Any() != true) return input;

    return string.Concat(input.Where(chr => !charsToRemove.Contains(chr)));
}

public static List<string> GetWords(string input)
{
    return input?.Split(' ').ToList() ?? new List<string>();
}

在使用中可能如下所示:

public static void Main(string[] args)
{
    var tracks = new List<string>
    {
        "Thank You, Next.",
        "Sweet But Psycho (One of Six Hardstyle Remix)",
    };

    // Note that this prevents someone from searching
    // for all titles that have a comma in the name
    var charsToIgnore = new List<char>
    {
        ',', '.', '!', ';', ':', '(', ')'
    };

    while (true)
    {
        Console.Write("Enter a parital title to search for: ");
        var search = Console.ReadLine();

        var matches = tracks.Where(track => ContainsWords(track, search, charsToIgnore));

        Console.WriteLine($"Matches found: {0}",
            matches.Any() ? string.Join(", ", matches) : "[None]");
    }

    GetKeyFromUser("\nDone! Press any key to exit...");
}

输出

请注意,这将阻止用户搜索包含我们“忽略”列表中的字符之一的所有标题,例如逗号或括号:

【讨论】:

  • 好的,我虽然测试了几次!谢谢 !我的文件名由路径(E:\xxxx\xxxx\filename.mp3)组成,但我认为这没有任何问题......
  • 如果你正在搜索像"E:\xxxx\xxxx\filename.mp3"这样的完整路径字符串,但你真的只想比较字符串"filename"(文件名,没有扩展名或路径),那么你可以使用:Path.GetFileNameWithoutExtension("E:\xxxx\xxxx\filename.mp3")
  • 好的,非常感谢您!我完美地适应了我的软件它的工作,我是一个初学者,但我没有使用太多的指针,但我侥幸逃脱了它啊哈。 60秒在线查找标题并找到它们的本地路径!我认为它肯定可以改进!但总是比用手拿起每首音乐更快:-P
猜你喜欢
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多