ASS形式のカラオケタグを読み込む

RhythmicaLylicsでtxt2ass出力したカラオケタグを読み込むプログラム。 この後aviutlで読み込めるexo形式での出力を予定。

  • 正規表現とテキスト処理難しい。
public class ass2exoJ {
    private static List<Tag> wordList;
    private static List<Tag> rubyStrList;



    public static void main(String args[]){
        String fileName="aaaaaaa.kra";
        File file = new File(fileName);

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"MS932"));
            String str;
            wordList=new ArrayList<Tag>();
            rubyStrList=new ArrayList<Tag>();
            str=br.readLine();
            while(str != null){
                //()括りのルビ部を抜き出す
                Pattern pattern = Pattern.compile("\\(.+?\\)");
                Matcher matcher = pattern.matcher(str);
                List<String> rubyList = new ArrayList<String>();
                List<String> charList = new ArrayList<String>();
                String rubyStr;
                while (matcher.find()) {
                    rubyStr=matcher.group().replace("(","");
                    rubyStr=rubyStr.replace(")","");
                    rubyList.add(rubyStr);
                }
                for (int i=0;i<rubyList.size();i++) {
                    //ルビをタイムコードを含めて除去;
                    //System.out.println(rubyList.get(i));
                    str=str.replace(rubyList.get(i), "");
                }
                //全半角スペースを除去
                str=str.replaceAll(" ", "");
                str=str.replaceAll(" ", "");

                Pattern pattern2 = Pattern.compile("\\[.+?\\]..?\\[.+?\\]");
                Matcher matcher2 = pattern2.matcher(str);
                while (matcher2.find()) {
                    charList.add(matcher2.group());
                }
                rubyStrList=addTagList(rubyList, rubyStrList);
                wordList = addTagList(charList,wordList);
                str = br.readLine();
            }
            for(int i=0;i<wordList.size();i++){
                if(wordList.get(i).lineEndFlg==0){
                    System.out.print("["+wordList.get(i).beginTime+"]"+""+wordList.get(i).word+"["+wordList.get(i).endTime+"]");
                }else{
                    System.out.println("["+wordList.get(i).beginTime+"]"+""+wordList.get(i).word+"["+wordList.get(i).endTime+"]");
                }

            }
            for(int i=0;i<rubyStrList.size();i++){
                if(rubyStrList.get(i).lineEndFlg==0){
                    System.out.print("["+rubyStrList.get(i).beginTime+"]"+""+rubyStrList.get(i).word+"["+rubyStrList.get(i).endTime+"]");
                }else{
                    System.out.println("["+rubyStrList.get(i).beginTime+"]"+""+rubyStrList.get(i).word+"["+rubyStrList.get(i).endTime+"]");
                }

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    public static List<Tag> addTagList(List<String> list,List<Tag> wordList){
        for (int i=0;i<list.size();i++) {
            Tag tag = new Tag();
            String str;
            //タイムコードが3個以上含まれている場合
            if(list.get(i).split("\\[").length - 1>2){
                int index=list.get(i).indexOf("]");
                //文字列を抜き出して要素入れ替え
                str = list.get(i).substring(index+1);
                tag.beginTime=str.substring(str.indexOf("[")+1, str.indexOf("]"));
                tag.word=str.substring(str.indexOf("]")+1,str.lastIndexOf("["));
                tag.endTime=str.substring(str.lastIndexOf("[")+1, str.lastIndexOf("]"));
            }else{
                str = list.get(i);
                tag.beginTime=str.substring(str.indexOf("[")+1, str.indexOf("]"));
                tag.word=str.substring(str.indexOf("]")+1,str.lastIndexOf("["));
                tag.endTime=str.substring(str.lastIndexOf("[")+1, str.lastIndexOf("]"));
            }
            if(i==list.size()-1){
                tag.lineEndFlg=1;
            }else{
                tag.lineEndFlg=0;
            }
            wordList.add(tag);
        }
        return wordList;
    }
}

public class Tag {
    public String beginTime;
    public String endTime;
    public String word;
    public int lineEndFlg;

}