我有这样的结构:
================
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”的同义词 结果可能是“束,集,套件”
我首先使用Entity框架代码。
如何进行查询以提取这些同义词?
答案 0 :(得分:1)
单个查询无法完成。但以下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");