Java Scanner Usage transformTXTandANNtoRQS(String TXT, String ANN)

Here you can find the source of transformTXTandANNtoRQS(String TXT, String ANN)

Description

Transforms a txt and an ann string to a string complying to the rqs format.

License

Apache License

Parameter

Parameter Description
TXT the txt string to be transformed.
ANN the txt string to be transformed.

Return

a string in rqs format.

Declaration

public static String transformTXTandANNtoRQS(String TXT, String ANN) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    /**//from w w w. j a  va  2 s  .  c  om
     * Transforms a txt and an ann string to a string complying to the rqs format.
     * 
     * @param TXT the txt string to be transformed.
     * @param ANN the txt string to be transformed.
     * @return a string in rqs format.
     */
    public static String transformTXTandANNtoRQS(String TXT, String ANN) {
        ArrayList<String> requirements = new ArrayList<String>();
        Scanner txtScanner = new Scanner(TXT);
        while (txtScanner.hasNextLine()) {
            requirements.add(txtScanner.nextLine());
        }
        txtScanner.close();

        ArrayList<String> annotations = new ArrayList<String>();
        Scanner annScanner = new Scanner(ANN);
        while (annScanner.hasNextLine()) {
            annotations.add(annScanner.nextLine());
        }
        annScanner.close();

        HashMap<Integer, Integer> startChars = new HashMap<Integer, Integer>();
        int i = 0;
        int c = 0;
        String txtRequirements = "";
        for (String requirement : requirements) {
            i++;
            startChars.put(i, c);
            c += requirement.length() + 1;
            txtRequirements += requirement + "\n";
        }

        HashMap<String, String> idmap = new HashMap<String, String>();
        int[] currentTid = new int[requirements.size()];
        Arrays.fill(currentTid, 0);
        int[] currentRid = new int[requirements.size()];
        Arrays.fill(currentRid, 0);
        for (String annotation : annotations) {
            String Id = annotation.split("\t")[0];
            String type = Id.substring(0, 1);
            if (type.equals("T")) {
                int leftLim = Integer.parseInt(annotation.split("\t")[1]
                        .split(" ")[1]);
                for (i = 1; i < requirements.size() + 1; i++) {
                    if (leftLim < startChars.get(i))
                        break;
                }
                int reqId = i - 1;
                idmap.put(Id, reqId + ":T" + (++currentTid[reqId - 1]));
            }
        }
        for (String annotation : annotations) {
            String Id = annotation.split("\t")[0];
            String type = Id.substring(0, 1);
            if (type.equals("R")) {
                String leftTId = annotation.split("\t")[1].split(" ")[1]
                        .split(":")[1];
                int reqId = Integer
                        .parseInt(idmap.get(leftTId).split(":")[0]);
                idmap.put(Id, reqId + ":R" + (++currentRid[reqId - 1]));
            }
        }

        String annAnnotations = "";
        for (String annotation : annotations) {
            String Id = annotation.split("\t")[0];
            String type = Id.substring(0, 1);
            int reqId = Integer.parseInt(idmap.get(Id).split(":")[0]);
            if (type.equals("T")) {
                String anntype = annotation.split("\t")[1];
                String annword = annotation.split("\t")[2];
                int leftlim = Integer.parseInt(anntype.split(" ")[1])
                        - startChars.get(reqId);
                int rightlim = Integer.parseInt(anntype.split(" ")[2])
                        - startChars.get(reqId);
                anntype = anntype.split(" ")[0] + " " + leftlim + " "
                        + rightlim;
                annAnnotations += idmap.get(Id) + "\t" + anntype + "\t"
                        + annword + "\n";
            } else if (type.equals("R")) {
                String anntype = annotation.split("\t")[1];
                anntype = anntype.split(" ")[0] + " "
                        + idmap.get(anntype.split(" ")[1].split(":")[1])
                        + " "
                        + idmap.get(anntype.split(" ")[2].split(":")[1]);
                annAnnotations += idmap.get(Id) + "\t" + anntype + "\n";
            }
        }

        String ntext = "REQUIREMENTS\n------------\n";
        ntext += txtRequirements;
        ntext += "------------\n\nANNOTATIONS\n------------\n";
        ntext += annAnnotations;
        ntext += "------------\n";

        return ntext;
    }
}

Related

  1. swapElements(int length, int maxValue)
  2. termInput()
  3. Tokenize(String s, String s2, String s3)
  4. toNumeric(String ip)
  5. toWords(String content)
  6. trim(String s)
  7. validateInt(Scanner keyboard, String message, String error)
  8. waitForEnter()
  9. waitForStopSignal()