Exemplo n.º 1
0
        public static Phrase STAInsert(FulltextContext ctx, string newPhraseText /*NullOrEmpty => delete, else update or insert*/, int?phraseId /*==null => insert else update or delete*/, Dict dict /*my dict for insert*/, PhraseSide?phraseSide /*for Insert: dict and its side, e.g. czech part of English-Czech dict*/, int?srcSideId /*for inserting Destination side*/)
        {
            if (string.IsNullOrEmpty(newPhraseText))               //DELETE
            {
                if (phraseId == null || phraseSide != null || dict != null)
                {
                    throw new Exception("phraseId == null || phraseSide!=null || dict!=null");
                }
                var delPh = ctx.Phrases.Include(p => p.Dests).First(p => p.Id == phraseId);

                ctx.Phrases.RemoveRange(delPh.Dests);
                ctx.Phrases.Remove(delPh);
                return(null);
            }

            Phrase dbPhrase; PhraseWords oldPhrase = null; PhraseSide ps;

            if (phraseId != null)               //UPDATE
            {
                if (srcSideId != null || dict != null)
                {
                    throw new Exception("srcSideId != null || dict!=null");
                }
                dbPhrase  = ctx.Phrases.Include(p => p.Words).First(p => p.Id == phraseId);
                oldPhrase = new PhraseWords(dbPhrase.Text, TPosLen.fromBytes(dbPhrase.TextIdxs));
                ps        = new PhraseSide {
                    src = (Langs)dbPhrase.Dict.SrcLang, dest = (Langs)dbPhrase.DestLang
                };
            }
            else                 //INSERT
            {
                if (phraseSide == null || dict == null)
                {
                    throw new Exception("phraseSide == null || dict == null");
                }
                ps = (PhraseSide)phraseSide;
                if (ps.src != ps.dest && srcSideId == null)
                {
                    throw new Exception("ps.src!=ps.dest && srcSideId == null");
                }
                ctx.Phrases.Add(dbPhrase = new Phrase {
                    SrcLang = (byte)ps.src, DestLang = (byte)ps.dest, SrcRef = srcSideId, Dict = dict
                });
            }

            var lang = ps.langOfText(); var newPhrase = new PhraseWords(newPhraseText);

            Action <SelectedWord[]> spellCheckAndDBInsert = wordIdxs => {
                STASpellCheck(lang, wordIdxs, newPhrase);                 //low level spell check
                for (var i = 0; i < wordIdxs.Length; i++)
                {
                    if (newPhrase.Idxs[wordIdxs[i].idx].Len > 0)                                                       //new correct words to fulltext DB
                    {
                        ctx.PhraseWords.Add(new PhraseWord()
                        {
                            SrcLang = (byte)ps.src, DestLang = (byte)ps.dest, Word = wordIdxs[i].ftxWord, Phrase = dbPhrase
                        });
                    }
                }
            };

            //Word breaking
            STAWordBreak(lang, newPhrase);

            var newWordIdx = getCorrectPhraseWords(newPhrase);

            if (oldPhrase == null)                 //INSERT
            {
                spellCheckAndDBInsert(newWordIdx); //Spell check and add to DB
            }
            else                                   //UPDATE

            //Delete olds from DB
            {
                var oldWordIdx = getCorrectPhraseWords(oldPhrase);
                var dbOldWords = dbPhrase.Words;
                foreach (var w in oldWordIdx.Except(newWordIdx))
                {
                    ctx.PhraseWords.Remove(dbOldWords.First(db => db.Word == w.ftxWord));
                }

                //Spell check and add to DB
                var newWithoutOldWordIdx = newWordIdx.Except(oldWordIdx).ToArray();
                spellCheckAndDBInsert(newWithoutOldWordIdx);
            }

            dbPhrase.Text     = newPhrase.Text;
            dbPhrase.TextIdxs = TPosLen.toBytes(newPhrase.Idxs);
            dbPhrase.Base     = newPhrase.Idxs.Select(idx => newPhrase.Text.Substring(idx.Pos, Math.Abs(idx.Len)).ToLower()).DefaultIfEmpty().Aggregate((r, i) => r + "|" + i);
            if (string.IsNullOrEmpty(dbPhrase.Base))
            {
                dbPhrase.Base = "";                                                  //error
            }
            if (dbPhrase.Base.Length > Phrase.maxPhraseBaseLen)
            {
                dbPhrase.Base = dbPhrase.Base.Substring(0, Phrase.maxPhraseBaseLen);
            }

            return(dbPhrase);
        }
Exemplo n.º 2
0
        public static spellCheckResult STAExtendCSVDict(Meta meta, CVSDictItem[] items)
        {
            Action <PhraseWords, HashSet <string>, bool> addWords = (phr, res, isWrong) => { foreach (var w in phr.Idxs.Where(idx => isWrong == idx.Len < 0).Select(idx => phr.Text.Substring(idx.Pos, Math.Abs(idx.Len))))
                                                                                             {
                                                                                                 res.Add(w);
                                                                                             }
            };
            spellCheckResult data = new spellCheckResult();

            //Break and Spell check
            foreach (var row in items)
            {
                var txt = Fulltext.FtxLib.STABreakAndCheck(meta.src, row.Src); row.SrcLog = TPosLen.fromString(txt.Idxs);
                addWords(txt, data.getWords(true, true), true); addWords(txt, data.getWords(true, false), false);
                foreach (var bp in Fulltext.FtxLib.BracketParse(row.Src))
                {
                    data.getBracketsWords(true, bp.Br).Add(bp.Text);
                }
                txt = Fulltext.FtxLib.STABreakAndCheck(meta.dest, row.Dest); row.DestLog = TPosLen.fromString(txt.Idxs);
                addWords(txt, data.getWords(false, true), true); addWords(txt, data.getWords(false, false), false);
                foreach (var bp in Fulltext.FtxLib.BracketParse(row.Dest))
                {
                    data.getBracketsWords(false, bp.Br).Add(bp.Text);
                }
            }
            exportLogs(meta, items);
            data.save(meta);
            return(data);
        }