private LyricItemCollection InternalBuildLyricObject(LyricItemCollection lyric, string sourceText)
        {
            var regex = new Regex(@"\[\d+:\d+.\d+\].+\n?");

            foreach (Match match in regex.Matches(sourceText))
            {
                lyric.Add(new LyricItem(match.Value));
            }

            return(lyric);
        }
        public LyricItemCollection Build(string sourceLyric)
        {
            var lyric = new LyricItemCollection(_options.Provider.Lyric.Config);

            if (string.IsNullOrEmpty(sourceLyric))
            {
                return(lyric);
            }

            InternalBuildLyricObject(lyric, sourceLyric);

            return(lyric);
        }
        public LyricItemCollection Build(string sourceLyric, string translationLyric)
        {
            var lyric = new LyricItemCollection(_options.Provider.Lyric.Config);

            if (string.IsNullOrEmpty(sourceLyric))
            {
                return(lyric);
            }

            lyric = InternalBuildLyricObject(lyric, sourceLyric);

            if (_options.Provider.Lyric.Config.IsEnableTranslation && !string.IsNullOrEmpty(translationLyric))
            {
                var translatedLyric = InternalBuildLyricObject(new LyricItemCollection(_options.Provider.Lyric.Config), translationLyric);
                return(lyric + translatedLyric);
            }

            return(lyric);
        }
Exemple #4
0
 /// <summary>
 /// 根据歌词集合构建一个索引状态字典。
 /// </summary>
 /// <remarks>
 /// 这个索引字典用于标识每个索引的歌词是否被处理,为 True 则为已处理,为 False 为未处理。
 /// </remarks>
 /// <param name="items">等待构建的歌词集合实例。</param>
 private static Dictionary <int, bool> BuildMarkDictionary(LyricItemCollection items)
 {
     return(items
            .Select((item, index) => new { index, item })
            .ToDictionary(item => item.index, item => false));
 }
Exemple #5
0
        public static LyricItemCollection operator +(LyricItemCollection left, LyricItemCollection right)
        {
            if (right.IsPruneMusic)
            {
                return(left);
            }

            var option        = left.Option;
            var newCollection = new LyricItemCollection(option);
            var indexDiff     = left.Count - right.Count;

            if (!option.IsOneLine)
            {
                left.ForEach(item => newCollection.Add(item));
                right.ForEach(item => newCollection.Add(item));

                newCollection.Sort();
                return(newCollection);
            }

            // 如果索引相等,直接根据索引快速匹配构建。
            if (indexDiff == 0)
            {
                newCollection.AddRange(left.Select((t, index) => t + right[index]));

                return(newCollection);
            }

            // 首先按照时间轴进行合并。
            var leftMarkDict  = BuildMarkDictionary(left);
            var rightMarkDict = BuildMarkDictionary(right);

            for (var leftIndex = 0; leftIndex < left.Count; leftIndex++)
            {
                var rightItem = right.Find(lyric => Math.Abs(lyric.SortScore - left[leftIndex].SortScore) < 0.001);
                if (rightItem != null)
                {
                    newCollection.Add(left[leftIndex] + rightItem);
                    var rightIndex = right.FindIndex(item => item == rightItem);
                    rightMarkDict[rightIndex] = true;
                }
                else
                {
                    newCollection.Add(left[leftIndex]);
                }

                leftMarkDict[leftIndex] = true;
            }

            // 遍历未处理的歌词项,将其添加到返回集合当中。
            var leftWaitProcessIndex = leftMarkDict
                                       .Where(item => !item.Value)
                                       .Select(pair => pair.Key);
            var rightWaitProcessIndex = rightMarkDict
                                        .Where(item => !item.Value)
                                        .Select(pair => pair.Key);

            leftWaitProcessIndex.Foreach(index => newCollection.Add(left[index]));
            rightWaitProcessIndex.Foreach(index => newCollection.Add(right[index]));

            newCollection.Sort();
            return(newCollection);
        }