Example #1
0
        List <FetchedUser> FetchRemote(string vkLogin, string vkPassword)
        {
            var userIds = new List <long>();

            foreach (var year in Years)
            {
                foreach (int facultyId in FacultiesExtensions.EnumerateReal())
                {
                    var response = vkApi.Users.Search(new UserSearchParams
                    {
                        University        = UniversityId,
                        UniversityFaculty = facultyId,
                        UniversityYear    = year,
                        Count             = 1000
                    });
                    userIds.AddRange(response.Select(u => u.Id));
                }
            }

            var results = new List <FetchedUser>();
            int i       = 0;

            foreach (var userId in userIds)
            {
                // any f****n vk.net error
                try
                {
                    if (UsersLimit != null && i >= UsersLimit)
                    {
                        break;
                    }
                    var infoResponse = vkApi.Call(
                        "execute.getUserInfo",
                        new VkNet.Utils.VkParameters(new Dictionary <string, string>
                    {
                        ["id"] = userId.ToString()
                    }));
                    i++;

                    // try deserialize if wall is not hidden
                    try
                    {
                        var fetchedResponse = JsonConvert.DeserializeObject <FetchedResponse>(infoResponse.RawJson);
                        results.Add(fetchedResponse.Response);
                    }
                    catch (JsonSerializationException)
                    {
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    logger.LogWarning($"User with ID {userId} is skipped during error {ex.Message}");
                    continue;
                }
            }
            return(results);
        }
Example #2
0
        public Dictionary <Faculties, double> Analyze(FetchedUser user)
        {
            if (!fetchingTask.IsCompleted)
            {
                throw new NotImplementedException();
            }

            // form doc-term matrix
            var docs = new List <Faculties>();

            docs.AddRange(FacultiesExtensions.EnumerateReal());
            docs.Add(Faculties.Target);
            var body = users.SelectMany(u => u.GetWords())
                       .Where(w => w != string.Empty && w.Length > 2);

            var terms = users.SelectMany(u => u.GetWords())
                        .Take(300)
                        .Distinct()
                        .ToArray();

            var dtMat = new double[docs.Count, terms.Count()];

            for (int i = 0; i < dtMat.GetLength(0); i++)
            {
                IEnumerable <string> facultyBody;
                if (dtMat.GetLength(0) - 1 == i)                 // target faculty
                {
                    facultyBody = user.GetWords();
                }
                else
                {
                    facultyBody = users
                                  .Where(u => u.Universities != null && u.Universities
                                         .Select(uni => uni.FacultyId)
                                         .Contains((int)docs[i]))
                                  .SelectMany(u => u.GetWords());
                }
                for (int j = 0; j < dtMat.GetLength(1); j++)
                {
                    dtMat[i, j] = facultyBody.Count(w => w == terms[j]);
                }
            }

            svd = new SingularValueDecomposition(dtMat);

            // compute low-ranked matrix
            var lowRankedMat = Matrix.Dot(svd.LeftSingularVectors, svd.DiagonalMatrix);             // did not low-rank here

            // form index model
            var indexModel = new Dictionary <Faculties, double[]>();

            for (int i = 0; i < docs.Count; i++)
            {
                var vector = new double[svd.Rank];
                for (int j = 0; j < vector.Length; j++)
                {
                    vector[j] = lowRankedMat[i, j];
                }
                indexModel.Add(docs[i], vector);
            }

            // calculate ranges
            var target = indexModel[Faculties.Target];
            var facs   = FacultiesExtensions.EnumerateReal();
            var result = new Dictionary <Faculties, double>();

            foreach (var faculty in facs)
            {
                var    vector = indexModel[faculty];
                double sum    = 0;
                for (int i = 0; i < svd.Rank; i++)
                {
                    sum += Math.Pow(vector[i] - target[i], 2);
                }
                var range = Math.Sqrt(sum);
                result.Add(faculty, range);
            }
            return(result.OrderBy(r => r.Value).ToDictionary(ks => ks.Key, vs => vs.Value));
        }