/// <summary> /// Zwiększa "wagę" słowa wybranego z propozycji fraz. /// </summary> public void Match(MatchRequest request) { using (var ctx = new TypeFineContext()) { SaveInfo(request, ctx, RequestType.Match); KeywordPhraseReference reference; try { reference = ctx.KeywordPhraseReferences.Include(x => x.Keyword).Include(x => x.Phrase).Single(x => x.Keyword.Value == request.Keyword && x.Phrase.Value == request.Phrase); } catch (Exception) { throw new FaultException("nie znaleziono złączenia pomiędzy podanym słowem kluczowym i frazą", new FaultCode("Match")); } reference.UserWeight += 0.02; if (reference.CompositeConvergance > 1) { reference.Keyword.Phrase = reference.Phrase; var toRemove = reference.Keyword.WeakReferences.ToList(); ctx.KeywordPhraseReferences.RemoveRange(toRemove); ctx.SaveChanges(); } ctx.SaveChanges(); } }
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { var actionName = request.Headers.Action ?? request.Headers.To.ToString(); actionName = actionName.Substring(actionName.LastIndexOf('/') + 1); RequestType requestType; if (Enum.TryParse(actionName, out requestType)) { if (requestType == RequestType.Setup) { return(null); //probably no database yet, neither do we want to log } var outp = new RequestInfo { RequestType = requestType }; FillDeviceAndAccountId(ref request, ref outp); using (var ctx = new TypeFineContext()) { ctx.RequestInfos.Add(outp); ctx.SaveChanges(); } } return(null); }
public void AddPhrase(AddPhraseRequest request) { using (var ctx = new TypeFineContext()) { SaveInfo(request, ctx, RequestType.AddPhrase); ctx.ProposedPhrases.Add(new ProposedPhrase { Comment = request.Comment, Right = request.Right }); ctx.SaveChanges(); } }
private static void SaveInfo(IBaseRequest request, TypeFineContext ctx, RequestType type, bool save = false) { ctx.RequestInfos.Add(new RequestInfo { RequestType = type, AccountId = request.AccountId, DeviceId = request.DeviceId }); if (save) { ctx.SaveChanges(); } }
public void RegisterOrUpdateUserNotification(NotificationRequest request) { using (var context = new TypeFineContext()) { SaveInfo(request, context, RequestType.RegisterOrUpdateUserNotification); var notificationUser = context.NotificationUsers.SingleOrDefault( x => x.User.DeviceId == request.DeviceId && x.User.AccountId == request.AccountId); if (notificationUser != null) { if (notificationUser.ChannelName != null && notificationUser.ChannelName != request.ChannelUrl) { notificationUser.ChannelName = request.ChannelUrl; } } else { var user = context.Users.SingleOrDefault( x => x.AccountId == request.AccountId && x.DeviceId == request.DeviceId); if (user != null) { AddNotificationUser(request, context, user); } else { var newUser = context.Users.Add(new User { DeviceId = request.DeviceId, AccountId = request.AccountId }); AddNotificationUser(request, context, newUser); } } context.SaveChanges(); } }
//public void Setup() //{ // MultithreadedGenerateServiceDatabase(); //} public static void MultithreadedGenerateServiceDatabase() { const int threshold = 2000; const string path = "Domain.Migration.Data.Phrases2.csv"; using (var ctx = new TypeFineContext()) { Database.SetInitializer(new TypeFineDatabaseInitializer()); if (ctx.Database.Exists()) { ctx.Database.Initialize(true); } } var bc = new BlockingCollection <List <string> >(); var readingTask = Task.Run(() => { var phrases = Assembly.GetAssembly(typeof(TypeFineDatabaseInitializer)) .GetManifestResourceStream(path); if (phrases == null) { throw new Exception(string.Format("There is no ebedded resource under path {0}", path)); } var i = 1; var bulk = new List <string>(); using (var sr = new StreamReader(phrases)) { while (!sr.EndOfStream) { var line = sr.ReadLine(); bulk.Add(line); if (i % threshold == 0) { bc.Add(bulk); bulk = new List <string>(); } i++; } bc.Add(bulk); bc.CompleteAdding(); } }); var savingTask = Task.Run(() => { Parallel.ForEach(bc.GetConsumingEnumerable(), bulk => { using (var ctx = new TypeFineContext()) { var keywords = bulk.Select(x => { var values = x.Split(new[] { "&&" }, StringSplitOptions.None); var phrase = new Phrase { Value = values[0], Comment = values[1], Interesting = int.Parse(values[2]) == 1, Source = (SourceType)int.Parse(values[3]), AddDate = DateTime.Parse(values[4], CultureInfo.InvariantCulture) }; var keyword = new Keyword { Phrase = phrase, Value = phrase.Value }; return(keyword); }); ctx.Keywords.AddRange(keywords); ctx.SaveChanges(); } }); }); Task.WaitAll(readingTask, savingTask); }
public static List <string> GetRanksForString(string key, int take = 5, int skip = 0) { var outp = new HashSet <KeywordPhraseReference>(); using (var ctx = new TypeFineContext()) { var keyword = ctx.Keywords.SingleOrDefault(x => x.Value == key); if (keyword != null) { if (keyword.Phrase != null) { return new List <string> { keyword.Phrase.Value } } ; var loveStories = ctx.KeywordPhraseReferences.Include("Phrase").Where(x => x.Keyword.Value == key); if (!loveStories.Any()) { var phrases = ctx.Phrases.ToList(); var lastRank = 0d; foreach (var phrase in phrases) { var rank = StringMetrics.Levenstein(key, phrase.Value).Value; if (outp.Count() < take) { outp.Add(new KeywordPhraseReference { Convergence = rank, UserWeight = 0, Keyword = keyword, Phrase = phrase }); } else { if (!(rank > lastRank)) { continue; } outp.Add(new KeywordPhraseReference { Convergence = rank, UserWeight = 0, Keyword = keyword, Phrase = phrase }); } var sort2 = outp.OrderByDescending(x => x.Convergence).Skip(skip).Take(take); outp = new HashSet <KeywordPhraseReference>(sort2); lastRank = outp.Last().Convergence; } ctx.KeywordPhraseReferences.AddRange(outp); ctx.SaveChanges(); return(outp.Select(x => x.Phrase.Value).ToList()); } var result = loveStories.ToList().OrderBy(x => x.CompositeConvergance); return(result.Select(x => x.Phrase.Value).ToList()); } throw new Exception("Missing keyword"); } } }