findPassengers method

Future<List<Map<String, dynamic>>> findPassengers(
  1. String horizontalSort,
  2. List<Line> allLines,
  3. List<String> inputNames
)

find passengers

Implementation

Future<List<Map<String, dynamic>>> findPassengers(String horizontalSort,
    List<Line> allLines, List<String> inputNames) async {
  List<String> results = <String>[];
  List<String> searchingNames = <String>[];
  List<String> searchingItems = <String>[];
  List<String> lines = horizontalSort.toLowerCase().split("\n");
  //making lines ready to search
  for (String l in lines) {
    if (l.split(spaceBetweenWords).length > 2) {
      try {
        String s = l.trim();
        //remove the possible icon from the first
        if (s.startsWith("i ") ||
            s.startsWith("↑ ") ||
            s.startsWith("4 ") ||
            s.startsWith("† ") ||
            s.startsWith("İ ") ||
            s.startsWith("é ") ||
            s.startsWith("o ") ||
            s.startsWith("d ") ||
            s.startsWith("+ ")) s = s.substring(2).trim();
        List<String> items = s.split(spaceBetweenWords);
        searchingNames.add(items[0] + ' ' + items[1]);
        searchingItems.add(s);
      } catch (e) {
        //not a valid line
      }
    }
  }
  //without inputNames no item will add
  if (inputNames.isEmpty) return [];
  //searching among input names
  inputNames = inputNames.map((e) => e = e.toLowerCase()).toList();
  for (String n in searchingNames) {
    BestMatch bestMatch = n.bestMatch(inputNames);
    if ((bestMatch.bestMatch.rating ?? 0) > 0.85) {
      int index = searchingNames.indexOf(n);
      String s = searchingItems[index].replaceFirst(
          n.replaceFirst(" ", spaceBetweenWords),
          bestMatch.bestMatch.target!.replaceFirst(" ", spaceBetweenWords));
      results.add(s);
    }
  }
  List<Map<String, dynamic>> passengers = [];
  for (String n in results) {
    String fullName = '';
    String seat = '';
    String seq = '';
    String bag = '';
    List<String> items = n.split(spaceBetweenWords);
    for (int i = 0; i < items.length; i++) {
      // String nextItem = (i == items.length - 1) ? '' : items[i + 1];
      List<dynamic> isSeatFunction = isPassengerSeat(items[i]);
      if (isSeatFunction[0]) {
        seat = isSeatFunction[1];
        seq = items
            .sublist(i + 1)
            .firstWhere((e) => isPassengerSequence(e), orElse: () => '');
        if (i < items.length - 2 &&
            seq.length == 1 &&
            seq == items[i + 1] &&
            isPassengerSequence(items[i + 2])) seq = items[i + 2];
        break;
      } else if (isAlpha(items[i]) && i < 3) {
        fullName = fullName + items[i] + ' ';
      }
    }
    bag = items
        .firstWhere((s) => isPassengerBag(s), orElse: () => '')
        .replaceAll('o', '0');
    fullName = fullName.trim();
    seq = seq.trim();
    OCRPassenger p =
        OCRPassenger(name: fullName, seat: seat, seq: seq, bag: bag);
    if (seat.isNotEmpty) passengers.add(p.toJson());
  }
  //we return processed output here;
  return passengers;
}