示例#1
0
            /// <summary>
            /// Process one line in the CEDICT format: parse, and store/index in dictionary.
            /// !! Does not check against dupes; cannot be used to update.
            /// </summary>
            public void AddEntry(string line)
            {
                ++lineNum;
                // Cycle through transactions
                if (lineNum % 3000 == 0)
                {
                    tr.Commit(); tr.Dispose(); tr = null;
                    tr = conn.BeginTransaction();
                }
                // Parse line from CEDICT format
                CedictEntry entry = CedictCompiler.ParseEntry(line, lineNum, swLog, swDropped);

                if (entry == null)
                {
                    return;
                }
                string head, trg;

                entry.GetCedict(out head, out trg);
                // Check restrictions - can end up dropped entry
                try { checkRestrictions(entry.ChSimpl, trg); }
                catch { return; }

                // Serialize, store in DB, index
                int binId = indexEntry(entry);
                // Populate entries table
                int entryId = storeEntry(entry.ChSimpl, head, trg, binId);

                // Folding history: mark new entry as affected by this bulk operation
                if (foldHistory)
                {
                    cmdInsBulkModif.Parameters["@modif_id"].Value = modifId;
                    cmdInsBulkModif.Parameters["@entry_id"].Value = entryId;
                    cmdInsBulkModif.ExecuteNonQuery();
                }
                // Verbose (per-entry) history
                else
                {
                    // Record change
                    cmdInsModifNew.Parameters["@timestamp"].Value = DateTime.UtcNow;
                    cmdInsModifNew.Parameters["@user_id"].Value   = userId;
                    cmdInsModifNew.Parameters["@note"].Value      = note;
                    cmdInsModifNew.Parameters["@entry_id"].Value  = entryId;
                    cmdInsModifNew.ExecuteNonQuery();
                    int modifId = (int)cmdInsModifNew.LastInsertedId;
                    // Also link from entry
                    cmdUpdLastModif.Parameters["@entry_id"].Value      = entryId;
                    cmdUpdLastModif.Parameters["@last_modif_id"].Value = modifId;
                    cmdUpdLastModif.ExecuteNonQuery();
                }
            }
示例#2
0
        public void DictLine(string line, bool cedict, BinWriter bw)
        {
            // Parse entry
            CedictEntry entry = CedictCompiler.ParseEntry(line);

            // Verify that simp, trad and pinyin are equal length
            if (entry != null)
            {
                if (entry.ChSimpl.Length != entry.ChTrad.Length || entry.ChSimpl.Length != entry.PinyinCount)
                {
                    entry = null;
                }
            }
            // Just count if failed to parse
            if (entry == null)
            {
                if (cedict)
                {
                    ++cedictDropped;
                }
                else
                {
                    ++hddDropped;
                }
                return;
            }
            // Serialize
            int fpos = bw.Position;

            // First: hash chain: next entry in file with same hash. Will fill later.
            bw.WriteInt(0);
            // Then, entry itself
            entry.Serialize(bw);
            // Hash simplified and remember file position
            int        hash = CedictEntry.Hash(entry.ChSimpl);
            List <int> poss;
            Dictionary <int, List <int> > hashPoss = cedict ? cedictHashPoss : hddHashPoss;

            if (!hashPoss.ContainsKey(hash))
            {
                poss           = new List <int>();
                hashPoss[hash] = poss;
            }
            else
            {
                poss = hashPoss[hash];
            }
            poss.Add(fpos);
        }