有的时候总是需要过滤一些没用的东西,歌词过滤工具V1.0的实现(提供下载)

       刚做完一个手机音乐播放器,来整理一些歌词资源的时候。发现很多歌词里面有些东西是我们不需要的。比较※、[ti:
[ar:、[al:、[by:]、編曲:、]作词:]、作曲:、]演唱:、]QQ、[offset:、]※等等之类,这个时候就想到做一个简单的过滤工具,把包含这些词的行删掉。这样歌词就显的很简洁了。恩,算是说了下背景吧。其实这个工具不难,不过竟然我做好了,就发布处理算是提供给有这方面需要的人一些思路。需要解决的问题:

1、效率 我这歌词达到几个G,虽然是本地,多多少少考虑下效率。用了个多线程。

2、方便 过滤的关键字肯定是需要变化的,所以不能写死。用了个配置文件。

3、如何删除行 歌词是.lrc格式的文件,所以比较难做到像数据库那样直接删除某一行。这样的读取文本,然后修改文本,在写回。

主要也就上面这三点。最后提供下载。

截图先:

 

过滤主要代码:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace lrcOP
{
    public class Filter
    {
        private ArrayList content = new ArrayList();
        private FileIO fileio = new FileIO();
        private string[] patterns;

        public void ReadFile(string path)
        {
            content.Clear();
            fileio.OpenReadFile(path);
            while (!fileio.IsEof())
            {
                content.Add(fileio.ReadLine());
            }
            fileio.CloseReadFile();
        }
        public void Process(string path)
        {
            ReadFile(path);
            FileFilter();
            WriteFile(path);
        }
        public void FileFilter()
        {
            patterns = Manager.patterns;
            for (int j = 0; j < content.Count; j++)
            {
                for (int i = 0; i < patterns.Length; i++)
                {
                    if (content[j].ToString().Contains(patterns[i]))
                    {
                        content.RemoveAt(j);
                    }
                }
            }
        }
        public void WriteFile(string path)
        {
            fileio.OpenWriteFile(path);
            foreach (string str in content)
            {
                fileio.WriteLine(str.ToString());
            }
            fileio.CloseWriteFile();
        }
    }
}

 

工作线程主要代码:

 

using System;
using System.Collections.Generic;
using System.Text;

namespace lrcOP
{
    public class WorkThread : MyThread
    {
        public bool stop = false;  // stop为true,线程退出
        public int threadID;    // 当前线程的threadID,用于区分其他的线程
       
        public override void run()  // 线程运行方法
        {
            LinkedList<string> paths = new LinkedList<string>();
            while (!stop)
            {
                while (!stop && paths.Count == 0)
                {
                    Manager.GetPaths(paths, 1); 
                    if (paths.Count == 0)
                    {
                        Manager.threadWait(threadID);
                        sleep(3000);  // 当前线程休眠3秒
                    }
                }
                foreach (string path in paths)
                {
                    if (stop) break;
                    try
                    {
                        Filter filter = new Filter();
                        filter.Process(path);//过滤处理
                        Manager.Count++;
                    }
                    catch (Exception e)
                    {

                    }
                }
                paths.Clear();
            }
        }
    }
}

 

线程管理主要代码:

 

using System;
using System.Collections.Generic;
using System.Text;

namespace lrcOP
{
    public class Manager
    {
        private static WorkThread[] dts = new WorkThread[10];
        private static List<int> threads = new List<int>();
        public static Queue<string> filepaths = new Queue<string>();
        public static string[] patterns;
        public static int Count;
        private static object syncDir = new object();  // 同步获得保存网页目录的方法

        public static void threadWait(int threadID)
        {
            if (!threads.Contains(threadID))
                threads.Add(threadID);
        }

        public static bool isFinished()  // 如果所有的线程都处于休眠状态,过滤工作结束
        {
            if (dts.Length == threads.Count)
                return true;
            else
                return false;

        }

        public static void Clear()
        {
            Count = 0;
            filepaths.Clear();
            threads.Clear();
        }

        public static void startThreads()  // 创建并运行用于下载网络资源的线程
        {
          
            for (int i = 0; i < dts.Length; i++)
            {
                dts[i] = new WorkThread();
                dts[i].threadID = i;
                dts[i].stop = false;
                dts[i].start();
            }
        }

        public static void stopThreads()  // 停止所有的下载线程
        {
            for (int i = 0; i < dts.Length; i++)
            {
                dts[i].stop = true;
            }
        }

        public static void GetPaths(LinkedList<string> paths, int count)//从队列中得到路径
        {
            lock (syncDir)
            {
                for (int i = 0; i < count; i++)
                {
                    if (filepaths.Count > 0)
                    {
                        paths.AddLast(filepaths.Dequeue().ToString());
                    }
                    else
                        break;
                }
            }
        }
    }
}

 

下载地址:/Files/yueyue_jwfm/lrcOP.rar 

 

 

 

posted @ 2010-04-29 16:54  小军人  阅读(2023)  评论(5编辑  收藏  举报