[写代码]处理一组lrc歌词文件

看到博客http://wwj718.github.io/regex-demo2.html,又想用C++写文本处理了。

将如下文件:

[00:00.12]section 2.
[00:02.68]you will part of a radio programme about the opening of a new local sports shop.
[00:09.32]first you have some time to look at questions 11 to 16.
[00:39.64]now listen carefully and answer questions 11 to 16.
[00:48.24]now we go to Jane who is going to tell us about what‘s happening in town this weekend.
[00:52.24]right,thanks Andrew,
[00:53.92]and now on to what‘s new,
[00:56.48]and do we really need yet another sports shop in Bradcaster?
[01:01.24]well,most of you probably know Sports World-
[01:04.44]the branch of a Danish sports goods company that opened a few years ago-
[01:09.04]it‘s attracted a lot of custom,
[01:11.36]and so the company has now decided to open another branch in the area.
[01:16.60]it‘s going to be in the shopping centre to the west of Bradcaster,
[01:20.44]so that will be good news for all of you who‘ve found the original shop in the north of the town hard to get to.
[01:27.12]i was invited to a special preview
[01:29.60]and i can promise you,this is the ultimate in sports retailing.
[211:29.60]dnasdasdjasiodjaoid

转换成:

   0.0     0.12  section 2.
  0.12     2.68  you will part of a radio programme about the opening of a new local sports shop.
  2.68     9.32  first you have some time to look at questions 11 to 16.
  9.32    39.64  now listen carefully and answer questions 11 to 16.
 39.64    48.24  now we go to Jane who is going to tell us about what‘s happening in town this weekend.
 48.24    52.24  right,thanks Andrew,
 52.24    53.92  and now on to what‘s new,
 53.92    56.48  and do we really need yet another sports shop in Bradcaster?
 56.48    61.24  well,most of you probably know Sports World-
 61.24    64.44  the branch of a Danish sports goods company that opened a few years ago-
 64.44    69.04  it‘s attracted a lot of custom,
 69.04    71.36  and so the company has now decided to open another branch in the area.
 71.36    76.60  it‘s going to be in the shopping centre to the west of Bradcaster,
 76.60    80.44  so that will be good news for all of you who‘ve found the original shop in the north of the town hard to get to.
 80.44    87.12  i was invited to a special preview
 87.12    89.60  and i can promise you,this is the ultimate in sports retailing.
 89.60 12689.60  dnasdasdjasiodjaoid

在C++用正则的库进行匹配要扫好几遍,所以放弃使用正则了。

提供解决方案的代码如下:

  1 #include <iostream>
  2 #include <deque>
  3 #include <iomanip>
  4 #include <string>
  5 #include <cstring>
  6 #include <sstream>
  7 #include <fstream>
  8 #include <vector>
  9
 10 using namespace std;
 11
 12 typedef struct LINE {
 13     string time;
 14     string sentence;
 15 }LINE;
 16
 17 typedef struct CLINE {
 18     string begin;
 19     string end;
 20     string sentence;
 21 }CLINE;
 22
 23 string htom(string time) {
 24     string temp[3];    //h:m:s
 25     deque<int> sign;
 26     for (auto i = 0; i != time.length(); i++) {
 27         if (time[i] == ‘.‘ || time[i] == ‘:‘) {
 28             sign.push_back(i);    //example: 01:27.12
 29         }
 30     }
 31     sign.push_back(time.size());    //end.
 32     int i = 0, t = 0, num[3];
 33     while (!sign.empty()) {
 34         for (; i != sign.front(); i++) {
 35             temp[t] += time[i];
 36         }
 37         t = t + 1;
 38         i = sign.front() + 1;
 39         sign.pop_front();
 40     }
 41     t = 0;    //set 0
 42     for (auto i = 0; i < 3; i++) {
 43         num[t++] = atoi(temp[i].data());    //convert
 44     }
 45     num[1] = num[0] * 60 + num[1];    //minute
 46     stringstream stoi;
 47     string last;
 48     if (num[2] > 10) {
 49         stoi << num[1] << "." << num[2];
 50     }
 51     else {
 52         stoi << num[1] << ".0" << num[2];
 53     }
 54     stoi >> last;
 55     return last;
 56 }
 57
 58 void input(vector<LINE> &lrc) {
 59     ifstream fileRead("lrc.txt");
 60     while (!fileRead.eof()) {
 61         char stmp[1024];
 62         LINE ltmp;
 63         fileRead.getline(stmp, sizeof(stmp));
 64         int LEN = strlen(stmp);
 65         bool sflag = false;
 66
 67         for (auto i = 1; i < LEN; i++) {
 68             if (stmp[i] == ‘]‘) {
 69                 sflag = true;
 70             }
 71             if (!sflag) {
 72                 ltmp.time += stmp[i];
 73                 continue;
 74             }
 75             if (stmp[i] != ‘]‘) {
 76                 ltmp.sentence += stmp[i];
 77             }
 78         }
 79         lrc.push_back(ltmp);
 80     }
 81     fileRead.close();
 82 }
 83
 84 void convert(vector<LINE> &lrc, vector<CLINE> &cvt) {
 85     int LEN = lrc.size() - 1;
 86     for (auto i = 0; i != LEN; i++) {
 87         CLINE temp;
 88         if (i == 0) {
 89             temp.begin = "0.0";
 90         }
 91         else {
 92             temp.begin = htom(lrc[i - 1].time);
 93         }
 94         temp.end = htom(lrc[i].time);
 95         temp.sentence = lrc[i].sentence;
 96         cvt.push_back(temp);
 97     }
 98 }
 99
100 void output(vector<CLINE> &cvt) {
101     ofstream fileWrite("cvtlrc.txt");
102     for (auto clrc : cvt) {
103         fileWrite << setw(6) << clrc.begin.data() << " "
104                   << setw(8) << clrc.end.data()
105                   << "  " << clrc.sentence.data() << endl;
106     }
107     fileWrite.close();
108 }
109
110 int main() {
111     vector<LINE> lrc;
112     vector<CLINE> cvt;
113     input(lrc);
114     convert(lrc, cvt);
115     //for (auto clrc : cvt) {
116     //    cout << setw(6) << clrc.begin.data() << " "
117     //        << setw(8) << clrc.end.data()
118     //        << "  " << clrc.sentence.data() << endl;
119     //}
120     output(cvt);
121     return EXIT_SUCCESS;
122 }
时间: 2024-11-08 20:45:16

[写代码]处理一组lrc歌词文件的相关文章

Java使用正则表达式解析LRC歌词文件

LRC歌词是一种应用广泛的歌词文件,对其进行解析时 标准格式: [分钟:秒.毫秒] 歌词 1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.InputStreamReader; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.Li

如何写代码向ftp服务器传文件?

.Net提供了FtpWebRequest类,代码如下: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using UnityEngine; /// <summary> /// 与 ftp 服务器通信的类 /// </summary> public static class FtpHelper { const int MaxRe

c++下lrc歌词文件检索(自己写的检索歌词文件,记录点滴)

贴上一个文档,是写这个程序的基本的思路,当然具体的程序和这个有一些出入,但是大体上一样.求批评指正. /*目标:在一个文件夹下吧所有的lrc歌词文件用程序导入,进行分析,最后的结果用TXT表示出来.*/ /*分析:只要建立了倒排索引就可以很容易把歌词的索引文件输入到il.txt中,所以应当有一个函数实现输入倒排索引,而后建立文件输出txt: 0.bool Lyricsindex_out(Lyric_index_list index_list[],int m ){} //相关的有: struct

Lrc歌词-开发标准

LRC歌词是在其程序当中实现的专门用于MP3等歌词同步显示的标签式的纯文本文件,如今已经得到了广泛的运用.现就LRC歌词文件的格式规定详细说明,已供程序开发人员参考. LRC文件是纯文本文件,可以用记事本等文本编辑工具查看和编辑.LRC文件中大量使用这类似[*:*]以及[*:*.*]这样的标签,而且标签是无须排序的,除此之外的全是文字. 首先是一些说明性的标签,这些是一般都在文件的开头的,标签的标识是不区分大小写的,形式如下: [ar:艺人名] [ti:曲名] [al:专辑名] [by:编者](

我的Android进阶之旅------&gt;Android自定义View来实现解析lrc歌词并同步滚动、上下拖动、缩放歌词的功能

前言 一LRC歌词文件简介 1什么是LRC歌词文件 2LRC歌词文件的格式 LRC歌词文件的标签类型 1标识标签 2时间标签 二解析LRC歌词 1读取出歌词文件 2解析得到的歌词内容 1表示每行歌词内容的实体类LrcRow 2解析歌词的构造器 ILrcBuilder接口 DefaultLrcBuilder歌词解析构造器 lrc歌词原始内容 lrc歌词解析后的内容 三显示LRC歌词内容 1定义一个ILrcViewListener接口 2定义一个ILrcView接口 3自定义一个LrcView 同步

C#下载歌词文件

前段时间写了一篇c#解析Lrc歌词文件,对lrc文件进行解析,支持多个时间段合并.本文借下载歌词文件来探讨一下同步和异步方法. Lrc文件在网络上随处可见,我们可以通过一些方法获取,最简单的就是别人的接口,如: http://geci.me/api/lyric/不得不爱 返回下面的json,这样我们就很容易得到歌词文件了. { "count": 2, "code": 0, "result": [ { "aid": 272779

歌词文件解析(一):LRC格式文件的解析

LRC是英文lyric(歌词)的缩写,被用做歌词文件的扩展名.以lrc为扩展名的歌词文件可以在各类数码播放器中同步显示.LRC 歌词是一种包含着“*:*”形式的“标签(tag)”的基于纯文本的歌词专用格式. 1.标识标签(ID-tags) [ar:艺人名] [ti:曲名] [al:专辑名] [by:编者(指编辑LRC歌词的人)] [offset:时间补偿值] 其单位是毫秒,正值表示整体提前,负值相反.这是用于总体调整显示快慢的. [ti:山丘] [ar:李宗盛] [offset:0] 2.时间标

在类里面写代码,代替xml文件

就是这个,以前还真没有做过,这不,这次就见识过了.然后希望给自己一份记忆,给你们一份快捷而已... /** * 代码中设置一般selector * * @param context * @param idNormal * @param idSelected * @param idFocused * @param idUnable * @return */ public static StateListDrawable newSelector(Context context, Drawable i

不需要写代码,文件夹右键cmd定位指定目录

引子 这篇文章其实本来不是这样的,因为我用C#的代码实现了一个程序,后面才突然发现,我太傻太天真了,明明不需要写程序和写代码的,结果自己把自己二住了. 我们来看看效果图. 由于,我自己的原因,这个功能,最开始我使用的居然是自己写的程序来实现,太傻了. 参考了.两个网站,就可用搞定的,其实. 注册表路径: 图片最小面有的哦... 1. 如何对文件添加右键菜单. 2.如何使用cmd定位到指定目录. 参考内容: http://jingyan.baidu.com/article/295430f12371