示例#1
0
        private string Hanzi2Pinyin(RequestModel request)
        {
            var result = new StringBuilder();

            // 解析从客户端来的输出格式设置
            PinyinFormat format = PinyinUtil.ParseFormat(request.ToneType) |
                                  PinyinUtil.ParseFormat(request.CaseType) |
                                  PinyinUtil.ParseFormat(request.VType);

            foreach (char ch in request.Key)
            {
                if (!PinyinUtil.IsHanzi(ch))
                {// 不是汉字直接追加
                    result.Append(ch);
                    continue;
                }

                // 是汉字才处理

                // 是否只取第一个拼音
                if (request.Multi.Equals("first", StringComparison.OrdinalIgnoreCase))
                {
                    // 拼音间追加一个空格,这里如果是多间字,拼音可能不准确
                    result.AppendFormat("{0} ", Pinyin4Net.GetFirstPinyin(ch, format));
                    continue;
                }

                string[] py = Pinyin4Net.GetPinyin(ch, format);
                result.AppendFormat("({0}) ", string.Join(",", py));
            }

            return(result.ToString());
        }
示例#2
0
        public void GetSimplifiedPinYin()
        {
            string chineseStr = "我是中国人";

            string str = PinyinUtil.GetSimplifiedPinYin(chineseStr);

            Assert.True(true);
        }
示例#3
0
        public void GetPinYinTest()
        {
            string chineseStr = "我是中国人";

            string str = PinyinUtil.GetPinYin(chineseStr);

            Assert.True(true);
        }
        /// <summary>
        /// 获取省信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetProvinceData(string startTime, string endTime, int typeID)
        {
            var      result = new JsonResultObject();
            DateTime sTime  = DateTime.Now;
            DateTime eTime  = DateTime.Now;

            //<bei_jing,北京> 使用 TempData 进行存储
            Dictionary <string, string> dist = new Dictionary <string, string>();

            //获取出所有省的人数
            GetTime(startTime, endTime, typeID, ref sTime, ref eTime);
            OperationResult <IList <RegionDailyInfo> > rdResult =
                RegionDailyBLL.Instance.RegionDaily_GetProvince(sTime, eTime);
            IList <RegionDailyInfo> rdInfoList = rdResult.AppendData;

            //将北京市 转换成 bei_jing
            for (int i = 0; i < rdInfoList.Count; i++)
            {
                //去除特定的字符串
                string chineseProvince = rdInfoList[i].Province; //中文省
                string pinYinProvince  = string.Empty;           //拼音省
                rdInfoList[i].Province =
                    System.Text.RegularExpressions.Regex.Replace(rdInfoList[i].Province, "[省市区街道]", "");
                if (rdInfoList[i].Province.IndexOf("山西") != -1)
                {
                    rdInfoList[i].Province = "shan_xi_2";
                    dist.Add(rdInfoList[i].Province, chineseProvince);
                }
                else if (rdInfoList[i].Province.IndexOf("陕西") != -1)
                {
                    rdInfoList[i].Province = "shan_xi_1";
                    dist.Add(rdInfoList[i].Province, chineseProvince);
                }
                else
                {
                    //将  中文 转换为 zhong_wen
                    char[] province = rdInfoList[i].Province.ToCharArray();
                    rdInfoList[i].Province = string.Empty;
                    for (int j = 0; j < province.Length; j++)
                    {
                        rdInfoList[i].Province += PinyinUtil.Get(province[j]).ToLower();
                        if (province.Length != (j + 1))
                        {
                            rdInfoList[i].Province += "_";
                        }
                    }
                }
                pinYinProvince = rdInfoList[i].Province;
                dist.Add(pinYinProvince, chineseProvince);
            }
            System.Web.HttpContext.Current.Session["Province"] = dist;
            result.data   = JsonHelper.ConvertToJson(rdInfoList);
            result.status = true;
            return(Json(result));
        }
示例#5
0
        // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
        public void ConfigureAuthentication(IAppBuilder app)
        {
            var appConfig    = new AppConfig();
            var configExpire = appConfig["expireMinutes"];

            double expireMinutes = 0;

            if (!double.TryParse(configExpire, out expireMinutes))
            {
                expireMinutes = 30;
            }

            OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {
                //For Dev enviroment only (on production should be AllowInsecureHttp = false)
                AllowInsecureHttp         = true,
                TokenEndpointPath         = new PathString("/oauth2/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(expireMinutes),
                Provider          = new CustomOAuthProvider(),
                AccessTokenFormat = new CustomJwtFormat(appConfig["issuer"])
            };

            // OAuth 2.0 Bearer Access Token Generation
            app.UseOAuthAuthorizationServer(OAuthServerOptions);

            var secret = TextEncodings.Base64Url.Decode(appConfig["base64Secret"]);

            // Api controllers with an [Authorize] attribute will be validated with JWT
            app.UseJwtBearerAuthentication(
                new JwtBearerAuthenticationOptions
            {
                AuthenticationMode           = AuthenticationMode.Active,
                AllowedAudiences             = new[] { appConfig["clientId"] },
                IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
                {
                    new SymmetricKeyIssuerSecurityTokenProvider(appConfig["issuer"], secret)
                },
                Provider = new OAuthBearerAuthenticationProvider
                {
                    OnValidateIdentity = context =>
                    {
                        return(Task.FromResult <object>(null));
                    }
                }
            });

            PinyinUtil.Initialize();
        }
示例#6
0
        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="info">索引信息</param>
        /// <returns></returns>
        private Document CreateKeywordDocument(DataRow info)
        {
            Document doc = new Document();
            //  名称
            Field field = new Field("ChineseName", info["ChineseName"].ToString(), Field.Store.YES, Field.Index.ANALYZED);

            field.SetBoost(30f);
            doc.Add(field);

            //  拼音
            field = new Field("PinyinName", PinyinUtil.ConvertToPinyin(info["ChineseName"].ToString()), Field.Store.NO, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);

            //  其他索引信息
            doc.Add(new Field("ProductCount", info["ProductCount"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("TypeID", info["TypeID"].ToString(), Field.Store.YES, Field.Index.ANALYZED));

            //  商品基本信息
            doc.Add(new Field("KeywordID", info["KeywordID"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            return(doc);
        }
示例#7
0
            protected override List <object> _Filter(List <object> _unfilteredData, string _displayMember, string userInput)
            {
                int count = userInput.Length - 1;

                List <object> results     = new List <object>();
                string        userLetters = userInput.ToLower();

                foreach (var obj in _unfilteredData)
                {
                    var itemLetters = GetFilterString(obj, _displayMember).ToLower();
                    if (count < itemLetters.Length)
                    {
                        char currentLetter = itemLetters[count];
                        //Match isch = Regex.Match(currentLetter.ToString(), @"[\u4e00-\u9fa5]");
                        // 如果是中文
                        if (CharUtil.IsChineseLetter(currentLetter))
                        {
                            // (首字母)
                            // c = zhong
                            // 注: pinyin 有多种发音的,每一种都可以
                            List <string> pinyins = PinyinUtil.Chinese2Pinyin(currentLetter);

                            foreach (var pinyin in pinyins)
                            {
                                if (pinyin.ToLower()[0] == userLetters[count])
                                {
                                    results.Add(obj);
                                    // 找到匹配的了
                                    break;
                                }
                            }

                            continue;
                        }
                    }
                }
                return(results);
            }
示例#8
0
        public static void UpdateDatabase(SQLiteConnection connection, SQLiteTransaction transaction)
        {
            var path =
                Path.Combine(
                    new[] { typeof(AdsoTransImporter).GetAssemblyPath() }.Concat(
                        typeof(AdsoTransImporter).Namespace.Split('.').Skip(1).Concat(new[] { "files", "adso.db" }))
                    .ToArray());

            var adsoConnection = new SQLiteConnection($"Data Source={path};Version=3");

            adsoConnection.Open();
            var reader =
                new SQLiteCommand(
                    "select chinese_utf8_s, chinese_utf8_c, pinyin2, flag, english from expanded_unified",
                    adsoConnection).ExecuteReader();

            const string Sql = @"INSERT OR REPLACE INTO dictionary
    (
        rowid,
        simplified,
        traditional,
        pinyin,
        definition,
        classifier,
        hsk_level,
        part_of_speech,
        frequency,
        concept,
        topic,
        parent_topic,
        notes
    )
    SELECT
        old.rowid,
        COALESCE(old.simplified, new.simplified),
        COALESCE(old.traditional, new.traditional),
        COALESCE(old.pinyin, new.pinyin),
        COALESCE(old.definition, new.definition),
        old.classifier,
        old.hsk_level,
        COALESCE(old.part_of_speech, 0) | new.part_of_speech,
        old.frequency,
        old.concept,
        old.topic,
        old.parent_topic,
        old.notes
    FROM (SELECT @simplified AS simplified, @traditional AS traditional, @pinyin AS pinyin, @definition AS definition, @part_of_speech as part_of_speech) AS new
    LEFT JOIN (SELECT * FROM dictionary WHERE simplified=@simplified and simplified not null)
    AS old ON new.simplified = old.simplified or new.traditional = old.traditional";

            using (var insert = new SQLiteCommand(Sql, connection, transaction))
            {
                insert.Prepare();

                var entry = new Entry();
                var pinyinStringBuilder = new StringBuilder();
                while (reader.Read())
                {
                    entry.Clear();
                    entry.Simplified  = reader.GetString(0);
                    entry.Traditional = reader.GetString(1);
                    entry.Pinyin      = PinyinUtil.NormalizeNumbered(reader.GetString(2), pinyinStringBuilder);
                    entry.AddPartOfSpeech(reader.GetString(3));
                    entry.Definition = reader.GetString(4);

                    var p = insert.Parameters;
                    p.AddWithValue("simplified", entry.Simplified);
                    p.AddWithValue("traditional", entry.Traditional);
                    p.AddWithValue("pinyin", entry.Pinyin);

                    // The ADSO dictionary is overzealous about nouns, likely because it is designed primarily for machine translation, so we ignore nouns from there.
                    p.AddWithValue("part_of_speech", entry.PartOfSpeech & ~PartOfSpeech.Noun);
                    p.AddWithValue("definition", entry.Definition.ToSentenceCase());
                    insert.ExecuteNonQuery();
                }
            }

            adsoConnection.Close();
        }
示例#9
0
        private static void Main(string[] args)
        {
            const string DatabaseFileName        = "hanbaobao.db";
            const string RelativeOutputDirectory = @"..\..\..\HanBaoBao\app\src\main\assets\databases";

            SQLiteConnection.CreateFile(DatabaseFileName);
            var connection = new SQLiteConnection($"Data Source={DatabaseFileName};Version=3");

            connection.Open();

            using (var transaction = connection.BeginTransaction())
            {
                // Create the database.
                var creationScript = typeof(Program).GetEmbeddedFileContents("create.sql");
                new SQLiteCommand(creationScript, connection, transaction).ExecuteNonQuery();

                // Populate the data.

                // StarDictDictionaries.UpdateDatabase(connection);

                /*TatoebaSentenceImporter.UpdateDatabase(connection)
                +*/
                CcCedictImporter.UpdateDatabase(connection, transaction);
                NtiBuddhistDictionaryImporter.UpdateDatabase(connection, transaction);
                AdsoTransImporter.UpdateDatabase(connection, transaction);
                UnihanImporter.UpdateDatabase(connection, transaction);
                PopupChineseHskWordListImporter.UpdateDatabase(connection, transaction);
                HskHskComWordListImporter.UpdateDatabase(connection, transaction);
                WordFrequencyImporter.UpdateDatabase(connection, transaction);

                // Fix pinyin for missing entries
                PinyinUtil.InsertMissingPinyin(connection, transaction);

                // Remove (almost) useless terms. We don't want to segment into words which don't have any definition,
                // since it doesn't provide much value and we cannot be certain that those words actually exist.
                new SQLiteCommand(
                    "delete from dictionary where definition is null or pinyin is null",
                    connection,
                    transaction).ExecuteNonQuery();

                transaction.Commit();
            }

            // Set the version and clean up the redundant indices.
            var version = (int)(DateTime.UtcNow - new DateTime(2000, 1, 1)).TotalDays + 1;

            new SQLiteCommand(typeof(Program).GetEmbeddedFileContents("finalize.sql"), connection).ExecuteNonQuery();
            new SQLiteCommand($@"PRAGMA user_version='{version}'", connection).ExecuteNonQuery();
            var outputFile    = connection.FileName;
            var includedLines = (long)new SQLiteCommand("select count(*) from dictionary", connection).ExecuteScalar();

            connection.Close();
            Console.WriteLine($"Done! Inserted {includedLines} entries!");

            // Delete existing versions of this database in the output directory.
            var outputDir = Path.Combine(Environment.CurrentDirectory, RelativeOutputDirectory);

            foreach (var fileName in Directory.EnumerateFiles(outputDir))
            {
                var name = Path.GetFileName(fileName);
                if (name == null)
                {
                    continue;
                }

                if (name.StartsWith(DatabaseFileName))
                {
                    File.Delete(fileName);
                }
            }

            // Copy the database to the output directory.
            File.Copy(
                outputFile,
                Path.Combine(Environment.CurrentDirectory, RelativeOutputDirectory, $"{DatabaseFileName}.{version}"));

            Console.ReadKey();
        }
示例#10
0
        /// <summary>
        /// 创建索引文档
        /// </summary>
        /// <param name="info">商品信息</param>
        /// <returns></returns>
        private Document CreateDocument(DataRow info)
        {
            Document doc = new Document();

            //  商品名称
            Field field = new Field("ChineseName", info["ChineseName"].ToString(), Field.Store.YES, Field.Index.ANALYZED);
            float boost = 30f;

            if (info["ProductType"].ToString() == "0" || info["ProductType"].ToString() == "3")
            {
                boost -= 20f;
            }
            field.SetBoost(boost);
            doc.Add(field);

            //  商品标签
            field = new Field("ProductTag", info["ProductTag"].ToString(), Field.Store.NO, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);

            //  功能主治
            doc.Add(new Field("Efficacy", info["Efficacy"].ToString(), Field.Store.NO, Field.Index.ANALYZED));

            //  关键字
            string pattern  = "[“”;。、,. %[ ]()()Ⅱ,*:X+-]";
            string keywords = Regex.Replace(info["Keywords"].ToString(), pattern, " ");

            field = new Field("Keywords", keywords, Field.Store.NO, Field.Index.ANALYZED);
            doc.Add(field);

            //  通用名
            string cadn = Regex.Replace(info["CADN"].ToString(), pattern, " ");

            field = new Field("CADN", cadn, Field.Store.YES, Field.Index.ANALYZED);
            doc.Add(field);

            //  拼音  【商品名称、通用名】
            string chineseName = Regex.Replace(info["ChineseName"].ToString(), pattern, " ");

            pattern = "[a-zA-Z0-9袋盒片剂粒]";
            string combinName = Regex.Replace(string.Format("{0} {1}", chineseName, cadn), pattern, " ");

            field = new Field("PinyinName", PinyinUtil.ConvertToPinyin(GetKeyWordsSplitFilter(combinName, new PanGuTokenizer())), Field.Store.YES, Field.Index.ANALYZED);
            doc.Add(field);

            //  名称截取
            field = new Field("ChineseNameWord", GetWordByChineseName(chineseName), Field.Store.YES, Field.Index.ANALYZED);
            doc.Add(field);

            //  长名称、厂商
            doc.Add(new Field("LongName", info["LongName"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("Manufacturer", StringUtil.FilterManufacter(info["Manufacturer"].ToString()), Field.Store.YES, Field.Index.ANALYZED));

            //  其他索引信息  【评论、上架、上架时间、销量】
            doc.Add(new Field("Favorite", info["Favorite"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("Comments", info["Comments"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("Selling", info["Selling"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("SellingTime", info["SellingTime"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("Stock", (int.Parse(info["Stock"].ToString()) > 0 ? "1" : "0"), Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field("SellCount", info["SellCount"].ToString(), Field.Store.YES, Field.Index.NO));
            //  精品推荐(毛利率)
            decimal tradePrice = decimal.Parse(info["TradePrice"].ToString());
            decimal profit     = tradePrice - decimal.Parse(info["CostPrice"].ToString());

            doc.Add(new Field("Recommend", profit.ToString("0.00"), Field.Store.YES, Field.Index.NO));
            //  优惠推荐(折扣)
            decimal discount = 0;

            if (tradePrice > 0)
            {
                discount = tradePrice / decimal.Parse(info["MarketPrice"].ToString());
            }
            doc.Add(new Field("Preferential", discount.ToString("0.00"), Field.Store.YES, Field.Index.NO));

            //  商品基本信息
            doc.Add(new Field("ProductID", info["ProductID"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ProductCode", info["ProductCode"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ProductType", info["ProductType"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("Specifications", info["Specifications"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("Images", info["Images"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("Actions", info["Actions"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("MarketPrice", info["MarketPrice"].ToString(), Field.Store.YES, Field.Index.NO));
            doc.Add(new NumericField("TradePrice", Field.Store.YES, true).SetDoubleValue(double.Parse(info["TradePrice"].ToString())));

            //  品牌名称
            doc.Add(new Field("BrandID", info["BrandID"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("BrandName", info["BrandName"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("BrandLetter", info["BrandLetter"].ToString().ToUpper(), Field.Store.YES, Field.Index.NO));

            //  一级类目
            doc.Add(new Field("CFID1", info["CFID1"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ParentID1", info["ParentID1"].ToString(), Field.Store.YES, Field.Index.NO));
            field = new Field("CFName1", info["CFName1"].ToString(), Field.Store.YES, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);
            //  二级类目
            doc.Add(new Field("CFID2", info["CFID2"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ParentID2", info["ParentID2"].ToString(), Field.Store.YES, Field.Index.NO));
            field = new Field("CFName2", info["CFName2"].ToString(), Field.Store.YES, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);
            //  三级类目
            doc.Add(new Field("CFID3", info["CFID3"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("ParentID3", info["ParentID3"].ToString(), Field.Store.YES, Field.Index.NO));
            field = new Field("CFName3", info["CFName3"].ToString(), Field.Store.YES, Field.Index.ANALYZED);
            field.SetBoost(10f);
            doc.Add(field);

            return(doc);
        }
 public void ConvertTest()
 {
     Assert.Equal("TianDiXuanHuang YuZhouHongHuang", PinyinUtil.GetFullChars("������� ������"));
     Assert.Equal("RZC XBS XXJ XXY", PinyinUtil.GetFirstChars("��֮�� �Ա��� ����� ϰ��Զ"));
 }
示例#12
0
        public static void UpdateDatabase(SQLiteConnection connection, SQLiteTransaction transaction)
        {
            var lines   = typeof(UnihanImporter).ReadLines("Unihan_Readings.txt", Encoding.UTF8);
            var entries = Entries(lines);

            const string Sql = @"INSERT OR REPLACE INTO dictionary
    (
        rowid,
        simplified,
        traditional,
        pinyin,
        definition,
        classifier,
        hsk_level,
        part_of_speech,
        frequency
    )
    SELECT
        old.rowid,
        COALESCE(old.simplified, new.simplified),
        COALESCE(old.traditional, new.traditional),
        COALESCE(old.pinyin, new.pinyin),
        COALESCE(old.definition, new.definition),
        old.classifier,
        old.hsk_level,
        old.part_of_speech,
        old.frequency
     FROM (SELECT @simplified AS simplified, @traditional AS traditional, @pinyin AS pinyin, @definition AS definition) AS new
     LEFT JOIN(SELECT * FROM dictionary WHERE simplified=@simplified and simplified not null) AS old ON new.simplified = old.simplified and new.pinyin = old.pinyin";

            var insert = new SQLiteCommand(Sql, connection, transaction);

            insert.Prepare();

            var variantMap = GetCharVariantMappings();
            var simplifiedToTraditional = variantMap.Item1;
            var traditionalToSimplified = variantMap.Item2;

            foreach (var entry in entries)
            {
                insert.Parameters.Clear();
                var character  = GetOrDefault(entry, "char");
                var pinyin     = GetOrDefault(entry, "kMandarin");
                var definition = GetOrDefault(entry, "kDefinition");
                if (string.IsNullOrWhiteSpace(character) || string.IsNullOrWhiteSpace(pinyin)
#if ONLY_INCLUDE_ENTRIES_WITH_DEFINITIONS
                    || string.IsNullOrWhiteSpace(definition)
#endif
                    )
                {
                    continue;
                }

                // Find the simplified/traditional variants of this character.
                string simplified, traditional;
                if (simplifiedToTraditional.TryGetValue(character, out traditional))
                {
                    simplified = character;
                }
                else if (traditionalToSimplified.TryGetValue(character, out simplified))
                {
                    traditional = character;
                }

                if (string.IsNullOrWhiteSpace(traditional) && string.IsNullOrWhiteSpace(simplified))
                {
                    simplified = traditional = character;
                }

                // Stuff the values into the database.
                insert.Parameters.AddWithValue("simplified", simplified);
                insert.Parameters.AddWithValue("traditional", traditional);
                insert.Parameters.AddWithValue("pinyin", PinyinUtil.ConvertAccentedToNumbered(pinyin));
                insert.Parameters.AddWithValue("definition", CleanDefinition(definition).ToSentenceCase());

                insert.ExecuteNonQuery();
            }

            insert.Dispose();
        }
 public void ConvertTest()
 {
     Assert.Equal("TianDiXuanHuang YuZhouHongHuang", PinyinUtil.GetFullChars("天地玄黄 宇宙洪荒"));
     Assert.Equal("RZC XBS XXJ XXY", PinyinUtil.GetFirstChars("人之初 性本善 性相近 习相远"));
 }
示例#14
0
            protected override List <object> _Filter(List <object> _unfilteredData, string _displayMember, string userInput)
            {
                int last = userInput.Length - 1;

                List <object> results = new List <object>();

                userInput = userInput.ToLower();

                foreach (var obj in _unfilteredData)
                {
                    var itemLetters = GetFilterString(obj, _displayMember).ToLower();

                    // 一个一个字符比较
                    // e.g. : itemLetters = zz荣智 userInput = zzrongz
                    int ptr_user = 0;

                    bool isInputMatch = true;

                    for (int ptr_sc = 0; ptr_sc < itemLetters.Count(); ptr_sc++)
                    {
                        //if (CharUtil.IsEnglishLetter(itemLetters[ptr_sc]) && userInput[ptr_sc] == itemLetters[ptr_sc])
                        //{
                        //    ptr_user++;
                        //    continue;
                        //}
                        if (ptr_user > last)
                        {
                            break;
                        }
                        if (CharUtil.IsChineseLetter(itemLetters[ptr_sc]))
                        {
                            List <string> pinyins        = PinyinUtil.Chinese2Pinyin(itemLetters[ptr_sc]);
                            bool          anyPinyinMatch = false;
                            foreach (var pinyin in pinyins)
                            {
                                int sub = pinyin.Length < userInput.Length - ptr_user ? pinyin.Length : userInput.Length - ptr_user;
                                if (pinyin.Substring(0, sub) == userInput.Substring(ptr_user, sub))
                                {
                                    ptr_user      += sub;
                                    anyPinyinMatch = true;
                                    break;
                                }
                            }

                            if (!anyPinyinMatch)
                            {
                                isInputMatch = false;
                                break;
                            }
                        }
                        else if (userInput[ptr_user] == itemLetters[ptr_sc])
                        {
                            ptr_user++;
                            continue;
                        }
                        else
                        {
                            isInputMatch = false;
                            ptr_user++;
                            break;
                        }
                    }

                    // 用户输入多余字符, 认为不匹配
                    if (ptr_user <= last)
                    {
                        isInputMatch = false;
                    }

                    if (isInputMatch)
                    {
                        results.Add(obj);
                    }

                    //char currentLetter = itemLetters[last];
                    ////Match isch = Regex.Match(currentLetter.ToString(), @"[\u4e00-\u9fa5]");
                    //// 如果是中文
                    //if (CharUtil.IsChineseLetter(currentLetter))
                    //{
                    //    // (首字母)
                    //    // c = zhong
                    //    // 注: pinyin 有多种发音的,每一种都可以
                    //    List<string> pinyins = PinyinUtil.Chinese2Pinyin(currentLetter);

                    //    foreach (var pinyin in pinyins)
                    //    {
                    //        if (pinyin.ToLower()[0] == userLetters[last])
                    //        {
                    //            results.Add(obj);
                    //            // 找到匹配的了
                    //            break;
                    //        }
                    //    }

                    //    continue;


                    //}
                }
                return(results);
            }
示例#15
0
        public static void UpdateDatabase(SQLiteConnection connection, SQLiteTransaction transaction)
        {
            var          lines = typeof(NtiBuddhistDictionaryImporter).ReadLines("words.txt", Encoding.UTF8);
            const string Sql   = @"INSERT OR REPLACE INTO dictionary
    (
        rowid,
        simplified,
        traditional,
        pinyin,
        definition,
        classifier,
        hsk_level,
        part_of_speech,
        frequency,
        concept,
        topic,
        parent_topic,
        notes
    )
    SELECT
        old.rowid,
        COALESCE(old.simplified, new.simplified),
        COALESCE(old.traditional, new.traditional),
        COALESCE(old.pinyin, new.pinyin),
        COALESCE(old.definition, new.definition),
        old.classifier,
        old.hsk_level,
        COALESCE(old.part_of_speech, 0) | new.part_of_speech,
        old.frequency,
        COALESCE(old.concept, new.concept),
        COALESCE(old.topic, new.topic),
        COALESCE(old.parent_topic, new.parent_topic),
        COALESCE(old.notes, new.notes)
    FROM (SELECT @simplified AS simplified, @traditional AS traditional, @pinyin AS pinyin, @definition AS definition, @part_of_speech as part_of_speech, @concept as concept, @topic as topic, @parent_topic as parent_topic, @notes as notes) AS new
    LEFT JOIN (SELECT * FROM dictionary WHERE simplified=@simplified and simplified not null)
    AS old ON new.simplified = old.simplified or new.traditional = old.traditional";

            var insert = new SQLiteCommand(Sql, connection, transaction);

            insert.Prepare();

            var defs = new Dictionary <string, Entry>();

            foreach (var line in lines)
            {
                if (line.StartsWith("#"))
                {
                    continue;
                }

                var tokens = line.Split('\t');

                var   simplified  = GetOrDefault(tokens, 1);
                var   traditional = GetOrDefault(tokens, 2);
                var   pinyin      = PinyinUtil.ConvertAccentedUnspacedToNumbered(GetOrDefault(tokens, 3));
                var   key         = (simplified ?? traditional) + pinyin;
                Entry entry;
                if (!defs.TryGetValue(key, out entry))
                {
                    entry = defs[key] = new Entry();
                }

                entry.Simplified  = entry.Simplified ?? simplified;
                entry.Traditional = entry.Traditional ?? traditional;
                entry.Pinyin      = entry.Pinyin ?? pinyin;
                var definition = GetOrDefault(tokens, 4);
                if (!string.IsNullOrWhiteSpace(definition))
                {
                    if (!string.IsNullOrWhiteSpace(entry.Definition))
                    {
                        entry.Definition += ", " + definition;
                    }
                    else
                    {
                        entry.Definition = definition.ToSentenceCase();
                    }
                }

                entry.AddPartOfSpeech(GetOrDefault(tokens, 5));
                entry.Concept     = GetOrDefault(tokens, 7);
                entry.Topic       = GetOrDefault(tokens, 9);
                entry.ParentTopic = GetOrDefault(tokens, 11);
                entry.Notes       = GetOrDefault(tokens, 14);
            }

            foreach (var entry in defs.Values)
            {
                var p = insert.Parameters;
                p.Clear();
                p.AddWithValue("simplified", entry.Simplified);
                p.AddWithValue("traditional", entry.Traditional);
                p.AddWithValue("pinyin", entry.Pinyin);
                p.AddWithValue("definition", entry.Definition);

                // TODO: Encode PoS information in an int instead of a string.
                p.AddWithValue("part_of_speech", entry.PartOfSpeech);
                p.AddWithValue("concept", entry.Concept);
                p.AddWithValue("topic", entry.Topic);
                p.AddWithValue("parent_topic", entry.ParentTopic);
                p.AddWithValue("notes", entry.Notes);
                insert.ExecuteNonQuery();
            }

            insert.Dispose();
        }
示例#16
0
        /// <summary>
        /// 关键词多条件搜索
        /// </summary>
        /// <param name="keyword">关键词</param>
        /// <param name="brand">品牌</param>
        /// <param name="cfid">分类</param>
        /// <param name="pricelimit">价格区间</param>
        /// <param name="store">是否有货</param>
        /// <param name="order">排序</param>
        /// <param name="recCount">总记录数</param>
        /// <param name="pageLen">条数</param>
        /// <param name="pageNo">页码</param>
        /// <returns>搜索结果</returns>
        public override JXSearchProductResult Search(RequestForm form, out int recCount)
        {
            recCount = 0;
            JXSearchProductResult result = new JXSearchProductResult();
            IndexSearcher         search = new IndexSearcher(CURR_INDEX_DIR);

            try
            {
                #region 搜索条件
                Query        query1 = null;
                BooleanQuery query  = new BooleanQuery();
                if (StringUtil.IsNatural_Number(form.queryForm.keyword))
                {
                    //  商品ID、金象码、拼音
                    BooleanClause.Occur[] flags = new BooleanClause.Occur[fieldsAbbr.Length];
                    for (int n = 0; n < fieldsAbbr.Length; n++)
                    {
                        flags[n] = BooleanClause.Occur.SHOULD;
                    }
                    query1 = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29, form.queryForm.keyword, fieldsAbbr, flags, new PanGuAnalyzer(true));
                    query.Add(query1, BooleanClause.Occur.MUST);
                }
                else
                {
                    //  关键词检索字段\t\r\n\\t
                    BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.Length];
                    for (int n = 0; n < fields.Length; n++)
                    {
                        flags[n] = BooleanClause.Occur.SHOULD;
                    }
                    //  转换关键词拼音
                    string pinyinName = PinyinUtil.ConvertToPinyin(GetKeyWordsSplitFilter(form.queryForm.keyword, new PanGuTokenizer()));

                    //  关键词检索
                    //  BooleanClause.Occur.MUST 表示 and
                    //  BooleanClause.Occur.MUST_NOT 表示 not
                    //  BooleanClause.Occur.SHOULD 表示 or
                    string q = GetKeyWordsSplitBySpace(string.Format("{0} {1}", form.queryForm.keyword, pinyinName), new PanGuTokenizer());
                    query1 = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29, q, fields, flags, new PanGuAnalyzer(true));
                    query.Add(query1, BooleanClause.Occur.MUST);
                }

                //  品牌
                if (form.queryForm.brandIds != null && form.queryForm.brandIds.Count() > 0)
                {
                    BooleanQuery tmpBQ = new BooleanQuery();
                    foreach (int key in form.queryForm.brandIds)
                    {
                        tmpBQ.Add(new TermQuery(new Term("BrandID", key.ToString())), BooleanClause.Occur.SHOULD);
                    }
                    query.Add(tmpBQ, BooleanClause.Occur.MUST);
                }

                //  分类
                if (form.queryForm.cfid > 0)
                {
                    BooleanQuery tmpBQ = new BooleanQuery();
                    tmpBQ.Add(new TermQuery(new Term("CFID1", form.queryForm.cfid.ToString())), BooleanClause.Occur.SHOULD);
                    tmpBQ.Add(new TermQuery(new Term("CFID2", form.queryForm.cfid.ToString())), BooleanClause.Occur.SHOULD);
                    tmpBQ.Add(new TermQuery(new Term("CFID3", form.queryForm.cfid.ToString())), BooleanClause.Occur.SHOULD);
                    query.Add(tmpBQ, BooleanClause.Occur.MUST);
                }

                //  库存
                if (form.queryForm.stock > 0)
                {
                    query.Add(new TermQuery(new Term("Stock", "1")), BooleanClause.Occur.MUST);
                }

                //  价格区间
                if (!string.IsNullOrEmpty(form.queryForm.pricelimit))
                {
                    string[] price = form.queryForm.pricelimit.Split(",".ToCharArray());
                    if (price.Length > 0 && double.Parse(price[1]) > 0)
                    {
                        query.Add(NumericRangeQuery.NewDoubleRange("TradePrice", double.Parse(price[0]), double.Parse(price[1]), true, false), BooleanClause.Occur.MUST);
                    }
                }

                //  是否有货    Selling 是否在卖 1:在卖 0:下架 2:暂无库存
                if (form.queryForm.store == 1)
                {
                    query.Add(new TermQuery(new Term("Selling", form.queryForm.store.ToString())), BooleanClause.Occur.MUST);
                }

                Hits hits = search.Search(query);
                #endregion

                //  构造商品结果
                result.listProductList = ProductBinding(hits, form.queryForm.keyword, form.queryForm.order, form.pageForm.page, form.pageForm.size, out recCount);

                #region 构造结果参数 品牌/分类
                //  排除搜索条件重新搜索
                if ((form.queryForm.brandIds != null && form.queryForm.brandIds.Count() > 0) || form.queryForm.cfid > 0 || form.queryForm.store == 1 || form.queryForm.order > 0 || !string.IsNullOrEmpty(form.queryForm.pricelimit))
                {
                    BooleanQuery booleanQuery = new BooleanQuery();
                    booleanQuery.Add(query1, BooleanClause.Occur.MUST);
                    hits = search.Search(query);
                }

                //  构造分类、品牌结果
                IList <JXSearchEntity> list = ProductParaList(hits);
                if (list != null)
                {
                    //  品牌
                    result.listBrand = list.Where(g => g.typeID == 5).ToList();

                    //  一级分类
                    IList <JXSearchEntity> categoryList = list.Where(g => g.typeID == 2 && g.parentID == 0).ToList();
                    if (categoryList != null)
                    {
                        foreach (JXSearchEntity item in categoryList)
                        {
                            //  二级分类
                            item.listSubClassification = list.Where(g => g.typeID == 2 && g.parentID == item.id).ToList();
                            foreach (JXSearchEntity subItem in item.listSubClassification)
                            {
                                //  三级分类
                                subItem.listSubClassification = list.Where(g => g.typeID == 2 && g.parentID == subItem.id).ToList();
                            }
                        }
                    }
                    result.listClassification = categoryList;
                }
                #endregion

                //  页数
                result.totalPage  = Convert.ToInt32(Math.Ceiling((double)recCount / form.pageForm.size));
                result.resultCode = "SUCCEED";
                result.resultMsg  = "SUCCEED";
            }
            catch { }
            finally
            {
                search.Close();
                search.Dispose();
            }
            return(result);
        }