Ejemplo n.º 1
0
        public void OnPostParallelize(string documentPicked)
        {
            var dirTexts = Path.Combine(_environment.ContentRootPath, "database", "parallelizedDocuments");

            Directory.CreateDirectory(dirTexts);
            Document docToParallelize = new Document();

            using (StreamReader r = new StreamReader(new FileStream(Path.Combine(_environment.ContentRootPath, "database", "documents", documentPicked + ".json"), FileMode.Open)))
            {
                docToParallelize = JsonConvert.DeserializeObject <Document>(r.ReadToEnd());
            }
            DirectoryInfo    directoryTextsInfo = new DirectoryInfo(dirTexts);
            ParallelDocument parallelDocument   = new ParallelDocument();

            parallelDocument.id               = directoryTextsInfo.GetFiles().Length.ToString();
            parallelDocument.name             = docToParallelize.documentName;
            parallelDocument.documentMetaData = docToParallelize.documentMetaData;
            int maxClausesNumber = docToParallelize.texts.Select(t => t.clauses.Count).Max();

            ParallelClause[,] parallelMatrix = new ParallelClause[maxClausesNumber, docToParallelize.texts.Count];
            for (int i = 0; i < maxClausesNumber; i++)
            {
                for (int j = 0; j < docToParallelize.texts.Count; j++)
                {
                    if (docToParallelize.texts[j].clauses.Count > i)
                    {
                        parallelMatrix[i, j] = new ParallelClause
                        {
                            textName     = docToParallelize.texts[j].textName,
                            textMetaData = docToParallelize.texts[j].textMetaData,
                            clause       = docToParallelize.texts[j].clauses[i]
                        };
                        continue;
                    }
                    parallelMatrix[i, j] = new ParallelClause
                    {
                        textName     = docToParallelize.texts[j].textName,
                        textMetaData = docToParallelize.texts[j].textMetaData,
                        clause       = null
                    };
                }
            }
            parallelDocument.parallelClauses = parallelMatrix;
            string     documentInJSON = JsonConvert.SerializeObject(parallelDocument, Formatting.Indented);
            var        documentDBFile = Path.Combine(dirTexts, directoryTextsInfo.GetFiles().Length.ToString() + "_" + docToParallelize.documentName + ".json");
            FileStream fs             = new FileStream(documentDBFile, FileMode.Create);

            using (StreamWriter w = new StreamWriter(fs))
            {
                w.Write(documentInJSON);
            }
        }
Ejemplo n.º 2
0
        public IActionResult OnPostTag(string sequenceOfParallelTokens)
        {
            var parallels = sequenceOfParallelTokens.Trim().Split("[[Delete]]").Where(parallel => parallel != "").ToList();
            ParallelDocument parallelSubcorpus         = null;
            string           parallelSubcorpusFilePath = null;

            foreach (var parallel in parallels)
            {
                var sequencesSplit = parallel.Split("};").Where(token => token != "").ToList();
                var sequencesIDs   = sequencesSplit.Select(x => x.Trim().Trim('{').Split(" - ")[0]).ToList();
                if (parallelSubcorpus is null)
                {
                    string        documentID                  = sequencesIDs[0].Split('|')[0];
                    var           dirParallelCorpus           = Path.Combine(_environment.ContentRootPath, "database", "parallelizedDocuments");
                    DirectoryInfo directoryParallelCorpusInfo = new DirectoryInfo(dirParallelCorpus);
                    parallelSubcorpusFilePath = directoryParallelCorpusInfo.GetFiles().Where(f => f.Name.Split('_')[0] == documentID).FirstOrDefault().FullName;
                    using (StreamReader r = new StreamReader(new FileStream(parallelSubcorpusFilePath, FileMode.Open)))
                    {
                        parallelSubcorpus = JsonConvert.DeserializeObject <ParallelDocument>(r.ReadToEnd());
                    }
                    parallelSubcorpus.parallelTokens = new List <ParallelToken>();
                }
                var token2Add = new ParallelToken();
                foreach (var token in sequencesIDs)
                {
                    Console.WriteLine(token);
                    var splitID = token.Split('|').Where(x => x != "").ToList();
                    RealizationGroup currentGroup;
                    if (token2Add.Where(g => g.documentID == splitID[0] && g.textID == splitID[2] && g.clauseID == splitID[1]).ToList().Count == 0)
                    {
                        currentGroup            = new RealizationGroup();
                        currentGroup.documentID = splitID[0];
                        currentGroup.textID     = splitID[2];
                        currentGroup.clauseID   = splitID[1];
                        token2Add.Add(currentGroup);
                    }
                    currentGroup = token2Add.Where(g => g.documentID == splitID[0] && g.textID == splitID[2] && g.clauseID == splitID[1]).FirstOrDefault();

                    var singleToken = parallelSubcorpus.parallelClauses[Convert.ToInt32(splitID[2]), Convert.ToInt32(splitID[1])].clause.realizations.Where(r => r.realizationID == splitID[3]).FirstOrDefault();
                    singleToken.documentID = splitID[0];
                    singleToken.textID     = splitID[2];
                    singleToken.clauseID   = splitID[1];
                    if (!currentGroup.Contains(singleToken))
                    {
                        currentGroup.Add(singleToken);
                    }
                }
                if (!parallelSubcorpus.parallelTokens.Contains(token2Add))
                {
                    parallelSubcorpus.parallelTokens.Add(token2Add);
                }
            }
            if (!(parallelSubcorpus is null) && !(parallelSubcorpusFilePath is null))
            {
                var parallelSubcorpusInJSON = parallelSubcorpus.Jsonize();
                using (StreamWriter w = new StreamWriter(new FileStream(parallelSubcorpusFilePath, FileMode.Create)))
                {
                    w.Write(parallelSubcorpusInJSON);
                }
            }
            return(RedirectToPage());
        }