如何从DB(EF, linq)中获取单词的同义词

本文关键字:获取 单词 同义词 linq DB EF | 更新日期: 2023-09-27 18:19:19

我有一个这样的结构:

================
tranlation
================
langid1
originalword
langid2
translationword

originalword和translationword在翻译langid == langid时是同义词。

我想找到单词的所有近义词。

例如:我们有一个翻译表(在所有这些翻译langid1 == langid 2中):

1. group bunch
2. group set
3. suite group

我需要找到"suite"的同义词结果可能是"group, bunch, set"或者我需要找到group的同义词结果可能是"bunch, set, suite"

我首先使用实体框架代码。

如何做一个查询来提取这些同义词?

如何从DB(EF, linq)中获取单词的同义词

单凭一个查询是做不到的。但是下面EF查询和客户端处理的组合将完成这项工作:

static IEnumerable<string> GetSynonyms(IQueryable<Translation> translation, string word)
{
    var baseQuery = translation.Where(t => t.langid == t.langid2);
    var filter = new HashSet<string> { word };
    var query = (from t in baseQuery where filter.Contains(t.originalword) && !filter.Contains(t.translationword) select t.translationword)
        .Union(from t in baseQuery where filter.Contains(t.translationword) && !filter.Contains(t.originalword) select t.originalword);
    var processed = new HashSet<string> { word };
    var next = new List<string>();
    while (true)
    {
        foreach (var item in query)
        {
            if (processed.Add(item))
            {
                yield return item;
                next.Add(item);
            }
        }
        if (next.Count == 0) break;
        filter.Clear();
        foreach (var item in next)
            filter.Add(item);
        next.Clear();
    }
}

样本检验:

class Translation
{
    public int langid { get; set; }
    public string originalword { get; set; }
    public int langid2 { get; set; }
    public string translationword { get; set; }
}
static void Test()
{
    var translation = new List<Translation>
    {
        new Translation { originalword = "group", translationword = "bunch" },
        new Translation { originalword = "group", translationword = "set" },
        new Translation { originalword = "suite", translationword = "group" },
    }
    .AsQueryable();
    var s1 = GetSynonyms(translation, "group").ToList();
    var s2 = GetSynonyms(translation, "suite").ToList();
    var s3 = GetSynonyms(translation, "bunch").ToList();
    var s4 = GetSynonyms(translation, "set").ToList();
    var s5 = GetSynonyms(translation, "blah").ToList();
}

你的用法可以像这样

YourDbContext db = ...;
var suiteSynonyms = GetSynonyms(db.Translation, "suite");