extractPassengersData method

dynamic extractPassengersData(
  1. List<Line> lines,
  2. List<String> inputNames
)

new function used to extract flight passenger data using input names for a dcs passenger list.

Implementation

extractPassengersData(List<Line> lines, List<String> inputNames) async {
  try {
    lines.sort((a, b) => a.cornerList![0].x.compareTo(b.cornerList![0].x));
    List<List<Line>> sortedLines = [[]];
    List<List<Line>> nameLines = [[]];
    for (int i = 0; i < lines.length; i++) {
      Line e = lines[i];
      // Line next = i < lines.length - 1 ? lines[i + 1] : Line(text: "");
      Line next = findClosetLine(e, [...lines]);
      if (e.text!.contains(" ") &&
          e.text!.split(" ")[0].length == 1 &&
          isAlpha(e.text!.split(" ")[1])) {
        e.text = e.text!.substring(2).trim();
      }
      List<Line> missedLines = [];
      List<dynamic> r1 = doesNamesContainString(e.text, inputNames);
      List<dynamic> r2 = doesNamesContainString(next.text, inputNames);
      List<dynamic> r3 =
          doesNamesContain2String(e.text, next.text, inputNames);
      if (r1[0]) {
        for (int i = sortedLines.last.length - 1; i >= 0; i--) {
          Line l = sortedLines.last[i];
          if (e.cornerList![0].x < l.cornerList![3].x) {
            missedLines.add(l);
            sortedLines.last.removeAt(i);
          }
        }
        if (!e.text!.toLowerCase().contains(r1[1].split(' ').first)) {
          e.text = r1[1];
        } else {
          e.text = e.text!.substring(
              e.text!.toLowerCase().indexOf(r1[1].split(' ').first));
        }
        sortedLines.add([]);
        nameLines.add([e]);
        //end of (r1[0]) condition
      } else if (!r2[0] && r3[0]) {
        for (int i = sortedLines.last.length - 1; i >= 0; i--) {
          Line l = sortedLines.last[i];
          if (e.cornerList![0].x < l.cornerList![3].x) {
            missedLines.add(l);
            sortedLines.last.removeAt(i);
          }
        }
        String f0 = r3[1].split(' ').first;
        String f1 = r3[1].split(' ').last;
        if (r3[1].trim().split(' ').length > 2) {
          List<String> tempL = r3[1].trim().split(' ');
          tempL.removeLast();
          f0 = tempL.join(" ");
        }
        if (!e.text!.toLowerCase().contains(r3[1].split(' ').first)) {
          e.text = f0;
        } else {
          e.text = e.text!.substring(
              e.text!.toLowerCase().indexOf(r3[1].split(' ').first));
        }
        if (!next.text!.toLowerCase().contains(f1)) {
          next.text = f1;
        } else {
          next.text = next.text!.substring(
              0, (next.text!.toLowerCase().indexOf(f1) + f1.length));
        }
        sortedLines.add([]);
        nameLines.add([e, next]);
        //end of (!r2[0] && r3[0]) condition
      }
      sortedLines.last.add(e);
      for (Line l in missedLines) {
        sortedLines.last.add(l);
      }
    }
    if (sortedLines.length > 1) {
      sortedLines.remove(sortedLines.first);
      nameLines.remove(nameLines.first);
    } else {
      return <Map<String, dynamic>>[];
    }
    int maxNameCount = 0;
    int maxListCount = 0;
    List<Line> maxList = [];

    ///sorting method!
    sortedResult = '';
    waitComplete = false;
    List<Map<String, dynamic>> passengers = <Map<String, dynamic>>[];
    for (int k = 0; k < sortedLines.length; k++) {
      List<Line> lineList = sortedLines[k];
      sortedLines[k] = await exclusiveLineSortFaster([...lineList],
          removeLines: nameLines[k]);
      lineList = sortedLines[k];
      //these codes are to set maxNameCount and maxListCount
      int i1 = nameLines[k].length;
      if (i1 > maxNameCount) maxNameCount = i1;
      for (Line l in lineList) {
        bool b = true;
        for (Line line in maxList) {
          if (l.cornerList![0].y > line.cornerList![2].y &&
              line.cornerList![0].y > l.cornerList![2].y) {
            b = false;
          }
        }
        if (b && !nameLines[k].contains(l)) {
          maxList.add(Line(text: l.text, cornerList: l.cornerList));
          maxListCount++;
        }
      }
      sortedLines[k] = nameLines[k] + sortedLines[k];
      if (k == (sortedLines.length - 1)) waitComplete = true;
    }
    await waitWhile(() => waitComplete);
    // sortedResult = sortedLines.join("\n\n");

    ///we will add temp Line to empty spaces of the table!
    ///then we recognize the type of every column.
    waitComplete = false;
    for (int i = 0; i < sortedLines.length; i++) {
      List<Line> lineList = sortedLines[i];
      List<Line> nameList = nameLines[i];
      sortedLines[i] = await exclusiveLineFiller(
          lineList, nameList, maxList, maxNameCount);
      if (i == (sortedLines.length - 1)) waitComplete = true;
    }
    await waitWhile(() => waitComplete);
    int seatIndex = -1;
    int seqIndex = -1;
    int bagIndex = -1;
    int seatCount = 0;
    int seqCount = 0;
    int bagCount = 0;
    bool isSeqOrganized = true;
    for (int i = 0; i < maxListCount; i++) {
      int seat0 = 0;
      int seq0 = 0;
      int bag0 = 0;
      for (List<Line> lineList in sortedLines) {
        Line l = lineList[i + maxNameCount];
        if (isPassengerSeat(l.text ?? '')) seat0++;
        if (isPassengerSequence(l.text ?? '')) seq0++;
        if (isPassengerBag(l.text ?? '')) bag0++;
      }
      if (seat0 > bag0 && seat0 > seq0 && seat0 > seatCount) {
        seatIndex = i;
        seatCount = seat0;
      }
      if (seq0 > seat0 && seq0 > bag0 && seq0 > seqCount) {
        seqIndex = i;
        seqCount = seq0;
        isSeqOrganized = true;
        for (int j = 1; j < sortedLines.length; j++) {
          String bef = sortedLines[j - 1][i + maxNameCount].text ?? '';
          String aft = sortedLines[j][i + maxNameCount].text ?? '';
          int a0 = int.tryParse(bef) ?? -1;
          int a1 = int.tryParse(aft) ?? -1;
          if (a0 >= 0 && a1 >= 0 && (a0 - a1) != 1 && (a1 - a0) != 1) {
            isSeqOrganized = false;
          }
        }
      }
      if (bag0 > seat0 && bag0 > seq0 && bag0 > bagCount) {
        bagIndex = i;
        bagCount = seq0;
      }
    }
    for (int i = 0; i < sortedLines.length; i++) {
      List<Line> lineList = sortedLines[i];
      String fullName = lineList.sublist(0, maxNameCount).join(" ").trim();
      String seat = seatIndex >= 0
          ? lineList[maxNameCount + seatIndex].text?.trim() ?? ''
          : '';
      String seq = seqIndex >= 0
          ? lineList[maxNameCount + seqIndex].text?.trim() ?? ''
          : '';
      String bag = bagIndex >= 0
          ? lineList[maxNameCount + bagIndex]
                  .text
                  ?.trim()
                  .toUpperCase()
                  .replaceAll("O", "0") ??
              ''
          : '';
      if (seat.isNotEmpty && !isPassengerSeat(seat)) {
        seat = seat.toLowerCase().replaceAll(" ", "");
        if (seat.length > 2 &&
            isAlpha(seat.substring(seat.length - 2, seat.length))) {
          seat = seat.substring(0, seat.length - 1);
        }
        if (seat.length > 3) {
          seat = seat.substring(seat.length - 3);
        }
        String lastS = seat.substring(seat.length - 1);
        if (!isAlpha(lastS)) {
          lastS = lastS
              .replaceAll('8', 'b')
              .replaceAll("0", "o")
              .replaceAll('2', 'z')
              .replaceAll("9", "g")
              .replaceAll(")", "j")
              .replaceAll("]", "j");
          seat = seat.substring(0, seat.length - 1) + lastS;
        }
      }
      if (!isPassengerSequence(seq)) {
        seq = seq
            .toUpperCase()
            .replaceAll(" ", "")
            .replaceAll("O", "0")
            .replaceAll("L", "1")
            .replaceAll("I", "1")
            .replaceAll("T", "1")
            .replaceAll('Z', '2')
            .replaceAll("G", "9");
      }
      if (isSeqOrganized && seq.isEmpty && seqIndex >= 0) {
        if (i == 0) {
          if (sortedLines.length > 2) {
            int a1 = int.tryParse(
                    sortedLines[i + 1][seqIndex + maxNameCount].text ?? '') ??
                -1;
            int a2 = int.tryParse(
                    sortedLines[i + 2][seqIndex + maxNameCount].text ?? '') ??
                -1;
            if (a1 >= 0 && a2 >= 0 && a1 > a2) {
              int a0 = a1 + 1;
              seq = a0.toString();
            } else if (a1 >= 0 && a2 >= 0) {
              int a0 = a1 - 1;
              if (a0 > 0) seq = a0.toString();
            }
          }
        } else if (i > 0 && i < sortedLines.length - 1) {
          int a0 = int.tryParse(
                  sortedLines[i - 1][seqIndex + maxNameCount].text ?? '') ??
              -1;
          int a1 = int.tryParse(
                  sortedLines[i + 1][seqIndex + maxNameCount].text ?? '') ??
              -1;
          if ((a0 - a1 == 2 || a1 - a0 == 2) && a0 > 0 && a1 > 0) {
            int a2 = (a0 + a1) ~/ 2;
            seq = a2.toString();
          }
        } else {
          int a0 = int.tryParse(
                  sortedLines[i - 2][seqIndex + maxNameCount].text ?? '') ??
              -1;
          int a1 = int.tryParse(
                  sortedLines[i - 1][seqIndex + maxNameCount].text ?? '') ??
              -1;
          if (a0 >= 0 && a1 >= 0 && a0 > a1) {
            int a2 = a1 - 1;
            if (a2 > 0) seq = a2.toString();
          }
        }
      }
      if (!isPassengerBag(bag)) bag = '';
      OCRPassenger p =
          OCRPassenger(name: fullName, seat: seat, seq: seq, bag: bag);
      if (seat.isNotEmpty || seq.isNotEmpty) passengers.add(p.toJson());
    }
    return passengers;
  } catch (e, stacktrace) {
    print("extractPassengersData: " + e.toString());
    print('Stacktrace: ' + stacktrace.toString());
    return <Map<String, dynamic>>[];
  }
}