Spaces:
Sleeping
Sleeping
File size: 185,724 Bytes
cbb452b dbf4df0 cbb452b c7c4f68 dbf4df0 cbb452b c7c4f68 cbb452b dbf4df0 cbb452b 8db81fe cbb452b dbf4df0 876f61d cbb452b dbf4df0 cbb452b dbf4df0 cbb452b dbf4df0 cbb452b dbf4df0 cbb452b dbf4df0 cbb452b 876f61d cbb452b 876f61d cbb452b 9d1e68c cbb452b 9d1e68c cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b 876f61d cbb452b 876f61d cbb452b 876f61d cbb452b 876f61d cbb452b 8db81fe cbb452b dbf4df0 cbb452b 8db81fe cbb452b dbf4df0 92ec7b2 dbf4df0 8db81fe cbb452b 8db81fe cbb452b 8db81fe cbb452b 876f61d cbb452b 876f61d cbb452b 876f61d cbb452b 876f61d cbb452b 876f61d cbb452b 8db81fe dbf4df0 8db81fe cbb452b 8db81fe cbb452b 8db81fe cbb452b 8db81fe cbb452b 8db81fe cbb452b 8db81fe cbb452b 8db81fe cbb452b 8db81fe cbb452b 8db81fe cbb452b 8db81fe 48d0918 dbf4df0 cbb452b dbf4df0 cbb452b dbf4df0 cbb452b dbf4df0 cbb452b 8db81fe cbb452b 8db81fe cbb452b d0ed367 cbb452b 48d0918 cbb452b 48d0918 cbb452b 48d0918 cbb452b dbf4df0 cbb452b 8db81fe cbb452b 117074e cbb452b dbf4df0 cbb452b dbf4df0 cbb452b dbf4df0 cbb452b 8db81fe cbb452b dbf4df0 8db81fe cbb452b 8db81fe cbb452b 8db81fe dbf4df0 cbb452b 117074e cbb452b 117074e cbb452b dbf4df0 cbb452b 117074e cbb452b 117074e cbb452b 117074e cbb452b dbf4df0 cbb452b dbf4df0 cbb452b dbf4df0 cbb452b dbf4df0 cbb452b 8c86465 cbb452b a971496 cbb452b c7c4f68 cbb452b c7c4f68 cbb452b c7c4f68 cbb452b a971496 cbb452b a971496 cbb452b a971496 cbb452b 11d643b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 |
# Standard library imports
import io
import os
import re
import json
import time
import textwrap
import logging
import traceback
from datetime import datetime
from enum import Enum, auto
from typing import Dict, List, Any, Tuple, Optional, Pattern, Callable, Set
from dataclasses import dataclass
from functools import wraps
from abc import ABC, abstractmethod
# import tempfile # For creating temporary files
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
# Third-party imports
import gradio as gr
from docx import Document
from colorama import init, Fore, Style
# from weasyprint import HTML # PDF generation related import
# Constants
DEFAULT_PORT = 7860
DEFAULT_HOST = "0.0.0.0"
DEFAULT_LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
DEFAULT_LOG_LEVEL = "INFO"
# 1. Base Exception Classes
class DocumentCheckError(Exception):
"""Base exception for document checker errors."""
pass
class ConfigurationError(DocumentCheckError):
"""Raised when configuration is invalid."""
pass
class DocumentTypeError(DocumentCheckError):
"""Raised when document type is invalid."""
pass
# 2. Configuration Classes
@dataclass
class PatternConfig:
"""Configuration for pattern matching."""
pattern: str
description: str
is_error: bool
replacement: Optional[str] = None
keep_together: bool = False
def compile(self) -> Pattern:
"""Compile the pattern."""
try:
return re.compile(self.pattern)
except re.error as e:
raise ConfigurationError(f"Invalid pattern '{self.pattern}': {e}")
class DocumentType(Enum):
"""Enumeration of supported document types."""
ADVISORY_CIRCULAR = auto()
AIRWORTHINESS_CRITERIA = auto()
DEVIATION_MEMO = auto()
EXEMPTION = auto()
FEDERAL_REGISTER_NOTICE = auto()
ORDER = auto()
POLICY_STATEMENT = auto()
RULE = auto()
SPECIAL_CONDITION = auto()
TECHNICAL_STANDARD_ORDER = auto()
OTHER = auto()
@classmethod
def from_string(cls, doc_type: str) -> 'DocumentType':
"""Convert string to DocumentType, case-insensitive."""
try:
return cls[doc_type.upper().replace(" ", "_")]
except KeyError:
raise DocumentTypeError(f"Unsupported document type: {doc_type}")
# 3. Utility Classes
@dataclass
class TextNormalization:
"""Text normalization utilities."""
@staticmethod
def normalize_heading(text: str) -> str:
"""Normalize heading text for consistent comparison."""
# Remove excess whitespace
text = ' '.join(text.split())
# Normalize periods (convert multiple periods to single period)
text = re.sub(r'\.+$', '.', text.strip())
# Remove any whitespace before the period
text = re.sub(r'\s+\.$', '.', text)
return text
@staticmethod
def normalize_document_type(doc_type: str) -> str:
"""Normalize document type string."""
return ' '.join(word.capitalize() for word in doc_type.lower().split())
# 4. Result Class
@dataclass
class DocumentCheckResult:
success: bool
issues: List[Dict[str, Any]]
details: Optional[Dict[str, Any]] = None
# 5. Base Document Checker
class DocumentChecker:
def __init__(self, config_path: Optional[str] = None):
self.config_manager = DocumentCheckerConfig(config_path)
self.logger = self.config_manager.logger
@classmethod
def extract_paragraphs(cls, doc_path: str) -> List[str]:
try:
doc = Document(doc_path)
return [para.text for para in doc.paragraphs if para.text.strip()]
except Exception as e:
logging.error(f"Error extracting paragraphs: {e}")
return []
@staticmethod
def validate_input(doc: List[str]) -> bool:
return doc is not None and isinstance(doc, list) and len(doc) > 0
# 6. Configuration Manager
class DocumentCheckerConfig:
REQUIRED_CONFIG_KEYS = {'logging', 'checks', 'document_types'}
REQUIRED_LOGGING_KEYS = {'level', 'format'}
REQUIRED_CHECKS_KEYS = {'acronyms', 'terminology_check', 'headings'}
def __init__(self, config_path: Optional[str] = None):
"""Initialize configuration with optional config file."""
self.default_config = {
"logging": {
"level": "INFO",
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
},
"checks": {
"acronyms": True,
"terminology_check": True,
"headings": True
},
"document_types": {
"Advisory Circular": {
"required_headings": [
"Purpose.",
"Applicability.",
"Cancellation.",
"Related Material.",
"Definition of Key Terms."
],
"skip_title_check": False
},
"Federal Register Notice": {
"required_headings": [
"Purpose of This Notice",
"Audience",
"Where can I Find This Notice"
],
"skip_title_check": False
},
"Order": {
"required_headings": [
"Purpose of This Order.",
"Audience.",
"Where to Find This Order."
],
"skip_title_check": False
},
"Policy Statement": {
"required_headings": [
"SUMMARY",
"CURRENT REGULATORY AND ADVISORY MATERIAL",
"RELEVANT PAST PRACTICE",
"POLICY",
"EFFECT OF POLICY",
"CONCLUSION"
],
"skip_title_check": False
},
"Technical Standard Order": {
"required_headings": [
"PURPOSE.",
"APPLICABILITY.",
"REQUIREMENTS.",
"MARKING.",
"APPLICATION DATA REQUIREMENTS.",
"MANUFACTURER DATA REQUIREMENTS.",
"FURNISHED DATA REQUIREMENTS.",
"HOW TO GET REFERENCED DOCUMENTS."
],
"skip_title_check": False
},
"Airworthiness Criteria": {
"required_headings": [],
"skip_title_check": True
},
"Deviation Memo": {
"required_headings": [],
"skip_title_check": True
},
"Exemption": {
"required_headings": [],
"skip_title_check": True
},
"Rule": {
"required_headings": [],
"skip_title_check": True
},
"Special Condition": {
"required_headings": [],
"skip_title_check": True
},
"Other": {
"required_headings": [],
"skip_title_check": True
}
}
}
self.config = self._load_config(config_path)
self._validate_config(self.config)
self.logger = self._setup_logger()
self.pattern_registry = self._setup_patterns()
def _load_config(self, config_path: Optional[str] = None) -> Dict[str, Any]:
"""
Load configuration from JSON file or use default settings.
Args:
config_path (str, optional): Path to configuration file.
Returns:
Dict[str, Any]: Loaded configuration dictionary.
"""
if config_path and os.path.exists(config_path):
try:
with open(config_path, 'r') as f:
user_config = json.load(f)
# Deep merge default and user config
return self._deep_merge(self.default_config.copy(), user_config)
except (json.JSONDecodeError, IOError) as e:
logging.warning(f"Error loading config: {e}. Using default config.")
return self.default_config.copy()
return self.default_config.copy()
def _validate_config(self, config: Dict[str, Any]) -> None:
"""Validate configuration structure."""
# Check required top-level keys
missing_keys = self.REQUIRED_CONFIG_KEYS - set(config.keys())
if missing_keys:
raise ConfigurationError(f"Missing required configuration keys: {missing_keys}")
# Validate logging configuration
missing_logging = self.REQUIRED_LOGGING_KEYS - set(config['logging'].keys())
if missing_logging:
raise ConfigurationError(f"Missing required logging keys: {missing_logging}")
# Validate checks configuration
missing_checks = self.REQUIRED_CHECKS_KEYS - set(config['checks'].keys())
if missing_checks:
raise ConfigurationError(f"Missing required checks keys: {missing_checks}")
# Validate document types
if not isinstance(config['document_types'], dict):
raise ConfigurationError("Document types must be a dictionary")
# Validate each document type's configuration
for doc_type, type_config in config['document_types'].items():
if not isinstance(type_config, dict):
raise ConfigurationError(f"Invalid configuration for document type {doc_type}")
# Check for required keys in each document type
required_keys = {'required_headings', 'skip_title_check'}
missing_type_keys = required_keys - set(type_config.keys())
if missing_type_keys:
raise ConfigurationError(
f"Missing required keys {missing_type_keys} for document type {doc_type}"
)
# Validate required_headings is a list
if not isinstance(type_config['required_headings'], list):
raise ConfigurationError(
f"required_headings must be a list for document type {doc_type}"
)
# Validate skip_title_check is boolean
if not isinstance(type_config['skip_title_check'], bool):
raise ConfigurationError(
f"skip_title_check must be a boolean for document type {doc_type}"
)
def _deep_merge(self, base: Dict[str, Any], update: Dict[str, Any]) -> Dict[str, Any]:
"""
Recursively merge two dictionaries.
Args:
base (Dict): Base dictionary to merge into.
update (Dict): Dictionary to merge from.
Returns:
Dict: Merged dictionary.
"""
for key, value in update.items():
if isinstance(value, dict) and key in base and isinstance(base[key], dict):
self._deep_merge(base[key], value)
else:
base[key] = value
return base
def _setup_logger(self) -> logging.Logger:
"""
Set up and configure logging based on configuration.
Returns:
logging.Logger: Configured logger instance.
"""
logger = logging.getLogger(__name__)
log_level = getattr(logging, self.config['logging']['level'].upper())
formatter = logging.Formatter(self.config['logging']['format'])
# Console Handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
console_handler.setLevel(log_level)
logger.addHandler(console_handler)
logger.setLevel(log_level)
return logger
def _setup_patterns(self) -> Dict[str, List[PatternConfig]]:
"""
Set up comprehensive pattern registry for all document checks.
Returns:
Dict[str, List[PatternConfig]]: Dictionary of pattern configurations by category
"""
try:
# Get the directory containing the current file
current_dir = os.path.dirname(os.path.abspath(__file__))
patterns_file = os.path.join(current_dir, 'patterns.json')
# Load patterns from JSON file
with open(patterns_file, 'r') as f:
patterns_data = json.load(f)
# Convert JSON data to PatternConfig objects
patterns = {}
for category, pattern_list in patterns_data.items():
patterns[category] = [
PatternConfig(
pattern=p['pattern'],
description=p['description'],
is_error=p['is_error'],
replacement=p.get('replacement'),
keep_together=p.get('keep_together', False)
) for p in pattern_list
]
return patterns
except Exception as e:
self.logger.error(f"Error loading patterns: {e}")
# Return empty patterns dictionary if file loading fails
return {}
def profile_performance(func):
"""Decorator to profile function performance."""
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
# Get logger from the class instance (first argument)
logger = args[0].logger
logger.info(
f"Performance: {func.__name__} took {end_time - start_time:.4f} seconds"
)
return result
return wrapper
# 8. FAA Document Checker
class FAADocumentChecker(DocumentChecker):
"""Document checker implementation for FAA documents."""
# Class Constants
PERIOD_REQUIRED = {
DocumentType.ADVISORY_CIRCULAR: True,
DocumentType.AIRWORTHINESS_CRITERIA: False,
DocumentType.DEVIATION_MEMO: False,
DocumentType.EXEMPTION: False,
DocumentType.FEDERAL_REGISTER_NOTICE: False,
DocumentType.ORDER: True,
DocumentType.POLICY_STATEMENT: False,
DocumentType.RULE: False,
DocumentType.SPECIAL_CONDITION: False,
DocumentType.TECHNICAL_STANDARD_ORDER: True,
DocumentType.OTHER: False
}
HEADING_WORDS = {
'APPLICABILITY', 'APPENDIX', 'AUTHORITY', 'BACKGROUND', 'CANCELLATION', 'CAUTION',
'CHAPTER', 'CONCLUSION', 'DEPARTMENT', 'DEFINITION', 'DEFINITIONS', 'DISCUSSION',
'DISTRIBUTION', 'EXCEPTION', 'EXPLANATION', 'FIGURE', 'GENERAL', 'GROUPS',
'INFORMATION', 'INSERT', 'INTRODUCTION', 'MATERIAL', 'NOTE', 'PARTS', 'PAST',
'POLICY', 'PRACTICE', 'PROCEDURES', 'PURPOSE', 'RELEVANT', 'RELATED',
'REQUIREMENTS', 'REPORT', 'SCOPE', 'SECTION', 'SUMMARY', 'TABLE', 'WARNING'
}
PREDEFINED_ACRONYMS = {
'AGC', 'AIR', 'CFR', 'DC', 'DOT', 'FAA IR-M', 'FAQ', 'i.e.', 'e.g.', 'MA',
'MD', 'MIL', 'MO', 'No.', 'PDF', 'SAE', 'SSN', 'TX', 'U.S.', 'U.S.C.', 'USA', 'US',
'WA', 'XX', 'ZIP', 'ACO' # Added ACO to ignore in acronym checks
}
# Constructor
def __init__(self, config_path: Optional[str] = None):
super().__init__(config_path)
def _get_doc_type_config(self, doc_type: str) -> Tuple[Dict[str, Any], bool]:
"""
Get document type configuration and validate document type.
Args:
doc_type: Type of document being checked
Returns:
Tuple containing:
- Document type configuration dictionary
- Boolean indicating if document type is valid
Raises:
DocumentTypeError: If document type is invalid
"""
# Validate document type
doc_type_config = self.config_manager.config['document_types'].get(doc_type)
if not doc_type_config:
self.logger.error(f"Unsupported document type: {doc_type}")
raise DocumentTypeError(f'Unsupported document type: {doc_type}')
return doc_type_config, True
@profile_performance
def heading_title_check(self, doc: List[str], doc_type: str) -> DocumentCheckResult:
if not self.validate_input(doc):
self.logger.error("Invalid document input for heading check")
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
try:
# Use the new helper method
doc_type_config, _ = self._get_doc_type_config(doc_type)
except DocumentTypeError as e:
return DocumentCheckResult(success=False, issues=[{'error': str(e)}])
# Get configuration for document-specific headings
required_headings = doc_type_config.get('required_headings', [])
if not required_headings:
return DocumentCheckResult(
success=True,
issues=[],
details={'message': f'No required headings defined for {doc_type}'}
)
headings_found = []
required_headings_set = set(required_headings)
# Precompile a regex pattern to match headings at the start of the paragraph
# Escape special characters in headings and allow for optional spaces and periods
heading_patterns = []
for heading in required_headings:
escaped_heading = re.escape(heading.rstrip('.'))
pattern = rf'^\s*{escaped_heading}\.?\s*'
heading_patterns.append(pattern)
combined_pattern = re.compile('|'.join(heading_patterns), re.IGNORECASE)
for para in doc:
para_strip = para.strip()
# Check if paragraph starts with any of the required headings
match = combined_pattern.match(para_strip)
if match:
# Extract the matched heading
matched_heading = match.group().strip()
# Normalize the matched heading to compare with required headings
matched_heading_base = matched_heading.rstrip('.').strip()
# Find the exact heading from required headings (case-insensitive)
for required_heading in required_headings:
if matched_heading_base.lower() == required_heading.rstrip('.').lower():
headings_found.append(required_heading)
break
# Check if all required headings are found
found_headings_set = set(headings_found)
missing_headings = required_headings_set - found_headings_set
unexpected_headings = found_headings_set - required_headings_set
success = len(missing_headings) == 0
issues = []
if not success:
issues.append({
'type': 'missing_headings',
'missing': list(missing_headings)
})
if unexpected_headings:
issues.append({
'type': 'unexpected_headings',
'unexpected': list(unexpected_headings)
})
details = {
'found_headings': list(found_headings_set),
'required_headings': required_headings,
'document_type': doc_type,
'missing_count': len(missing_headings),
'unexpected_count': len(unexpected_headings)
}
return DocumentCheckResult(success=success, issues=issues, details=details)
@profile_performance
def heading_title_period_check(self, doc: List[str], doc_type: str) -> DocumentCheckResult:
"""
Check if headings end with periods according to document type requirements.
Enforces both required periods and no periods based on document type.
Args:
doc (List[str]): List of document paragraphs
doc_type (str): Type of document being checked
Returns:
DocumentCheckResult: Result of the heading period check including:
- success: Boolean indicating if all headings follow period rules
- issues: List of dicts with heading format issues
- details: Additional information about the check
"""
if not self.validate_input(doc):
self.logger.error("Invalid document input for period check")
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
# Validate document type exists in configuration
doc_type_config = self.config_manager.config['document_types'].get(doc_type)
if not doc_type_config:
self.logger.error(f"Unsupported document type: {doc_type}")
return DocumentCheckResult(
success=False,
issues=[{'error': f'Unsupported document type: {doc_type}'}]
)
# Define document types requiring periods in headings
period_required = {
"Advisory Circular": True,
"Airworthiness Criteria": False,
"Deviation Memo": False,
"Exemption": False,
"Federal Register Notice": False,
"Order": True,
"Policy Statement": False,
"Rule": False,
"Special Condition": False,
"Technical Standard Order": True,
"Other": False
}
should_have_period = period_required.get(doc_type)
if should_have_period is None:
self.logger.error(f"Period requirement not defined for document type: {doc_type}")
return DocumentCheckResult(
success=False,
issues=[{'error': f'Period requirement not defined for document type: {doc_type}'}]
)
# Get the headings configuration for this document type
required_headings = doc_type_config.get('required_headings', [])
if not required_headings:
return DocumentCheckResult(
success=True,
issues=[],
details={'message': f'No required headings defined for {doc_type}'}
)
issues = []
checked_headings = []
# Create a set of normalized required headings (without periods)
# Strip periods from the required headings to allow for flexible matching
required_headings_set = {h.rstrip('.') for h in required_headings}
for para in doc:
para_strip = para.strip()
para_base = para_strip.rstrip('.')
# Check only if paragraph is a heading (comparing without periods)
if para_base in required_headings_set:
ends_with_period = para_strip.endswith('.')
# Check for both cases:
# 1. Should have period but doesn't
# 2. Shouldn't have period but does
if should_have_period and not ends_with_period:
issues.append({
'heading': para_strip,
'issue': 'missing_period',
'message': f"Heading should end with a period: '{para_strip}'"
})
elif not should_have_period and ends_with_period:
issues.append({
'heading': para_strip,
'issue': 'unexpected_period',
'message': f"Heading should not have a period: '{para_strip}'"
})
checked_headings.append({
'heading': para_strip,
'has_period': ends_with_period,
'needs_period': should_have_period
})
# Calculate statistics for the details
total_checked = len(checked_headings)
total_issues = len(issues)
incorrect_period_count = sum(1 for h in checked_headings
if h['has_period'] != h['needs_period'])
# Detailed results for debugging and reporting
details = {
'document_type': doc_type,
'periods_required': should_have_period,
'checked_headings': checked_headings,
'total_checked': total_checked,
'total_issues': total_issues,
'incorrect_period_count': incorrect_period_count
}
success = len(issues) == 0
# Log summary for debugging
self.logger.debug(f"Period check for {doc_type}: "
f"checked {total_checked} headings, "
f"found {total_issues} issues")
return DocumentCheckResult(success=success, issues=issues, details=details)
@profile_performance
def acronym_check(self, doc: List[str]) -> DocumentCheckResult:
"""Check for acronyms and their definitions."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
# Load valid words
valid_words = self._load_valid_words()
# Common words that might appear in uppercase but aren't acronyms
heading_words = self.config_manager.config.get('heading_words', self.HEADING_WORDS)
# Standard acronyms that don't need to be defined
predefined_acronyms = self.config_manager.config.get('predefined_acronyms', self.PREDEFINED_ACRONYMS)
# Patterns for references that contain acronyms but should be ignored
ignore_patterns = [
r'FAA-\d{4}-\d+', # FAA docket numbers
r'\d{2}-\d{2}-\d{2}-SC', # Special condition numbers
r'AC\s*\d+(?:[-.]\d+)*[A-Z]*', # Advisory circular numbers
r'AD\s*\d{4}-\d{2}-\d{2}', # Airworthiness directive numbers
r'\d{2}-[A-Z]{2,}', # Other reference numbers with acronyms
r'[A-Z]+-\d+', # Generic reference numbers
r'Β§\s*[A-Z]+\.\d+', # Section references
r'Part\s*[A-Z]+', # Part references
]
# Combine ignore patterns
ignore_regex = '|'.join(f'(?:{pattern})' for pattern in ignore_patterns)
ignore_pattern = re.compile(ignore_regex)
# Tracking structures
defined_acronyms = {} # Stores definition info
used_acronyms = set() # Stores acronyms used after definition
reported_acronyms = set() # Stores acronyms that have already been noted as issues
# Patterns
defined_pattern = re.compile(r'\b([\w\s&]+?)\s*\((\b[A-Z]{2,}\b)\)')
acronym_pattern = re.compile(r'(?<!\()\b[A-Z]{2,}\b(?!\s*[:.]\s*)')
issues = []
for paragraph in doc:
# Skip lines that appear to be headings
words = paragraph.strip().split()
if all(word.isupper() for word in words) and any(word in heading_words for word in words):
continue
# First, find all text that should be ignored
ignored_spans = []
for match in ignore_pattern.finditer(paragraph):
ignored_spans.append(match.span())
# Check for acronym definitions first
defined_matches = defined_pattern.finditer(paragraph)
for match in defined_matches:
full_term, acronym = match.groups()
# Skip if the acronym is in an ignored span
if not any(start <= match.start(2) <= end for start, end in ignored_spans):
if acronym not in predefined_acronyms:
if acronym not in defined_acronyms:
defined_acronyms[acronym] = {
'full_term': full_term.strip(),
'defined_at': paragraph.strip(),
'used': False
}
# Check for acronym usage
usage_matches = acronym_pattern.finditer(paragraph)
for match in usage_matches:
acronym = match.group()
start_pos = match.start()
# Skip if the acronym is in an ignored span
if any(start <= start_pos <= end for start, end in ignored_spans):
continue
# Skip predefined acronyms, valid words, and other checks
if (acronym in predefined_acronyms or
acronym in heading_words or
acronym.lower() in valid_words or # Check against valid words list
any(not c.isalpha() for c in acronym) or
len(acronym) > 10):
continue
if acronym not in defined_acronyms and acronym not in reported_acronyms:
# Undefined acronym used; report only once
issues.append(f"Confirm '{acronym}' was defined at its first use.")
reported_acronyms.add(acronym)
elif acronym in defined_acronyms:
defined_acronyms[acronym]['used'] = True
used_acronyms.add(acronym)
return DocumentCheckResult(success=len(issues) == 0, issues=issues)
@profile_performance
def acronym_usage_check(self, doc: List[str]) -> DocumentCheckResult:
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
# Pattern to find acronym definitions (e.g., "Environmental Protection Agency (EPA)")
defined_pattern = re.compile(r'\b([\w\s&]+?)\s*\((\b[A-Z]{2,}\b)\)')
# Pattern to find acronym usage (e.g., "FAA", "EPA")
acronym_pattern = re.compile(r'\b[A-Z]{2,}\b')
# Tracking structures
defined_acronyms = {}
used_acronyms = set()
# Step 1: Extract all defined acronyms
for paragraph in doc:
defined_matches = defined_pattern.findall(paragraph)
for full_term, acronym in defined_matches:
if acronym not in defined_acronyms:
defined_acronyms[acronym] = {
'full_term': full_term.strip(),
'defined_at': paragraph.strip()
}
# Step 2: Check for acronym usage, excluding definitions
for paragraph in doc:
# Remove definitions from paragraph for usage checks
paragraph_excluding_definitions = re.sub(defined_pattern, '', paragraph)
usage_matches = acronym_pattern.findall(paragraph_excluding_definitions)
for acronym in usage_matches:
if acronym in defined_acronyms:
used_acronyms.add(acronym)
# Step 3: Identify unused acronyms
unused_acronyms = [
{
'acronym': acronym,
'full_term': data['full_term'],
'defined_at': data['defined_at']
}
for acronym, data in defined_acronyms.items()
if acronym not in used_acronyms
]
# Success is true if no unused acronyms are found
success = len(unused_acronyms) == 0
return DocumentCheckResult(success=success, issues=unused_acronyms)
@profile_performance
def check_terminology(self, doc: List[str]) -> DocumentCheckResult:
"""Check document terminology and output only unique term replacements needed."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
terminology_patterns = self.config_manager.pattern_registry.get('terminology', [])
prohibited_patterns = self.config_manager.pattern_registry.get('reference_terms', [])
unique_issues = set() # Using a set to avoid duplicate replacements
# Process each sentence
for paragraph in doc:
sentences = re.split(r'(?<=[.!?])\s+', paragraph)
for sentence in sentences:
sentence = sentence.strip()
if not sentence:
continue
# Check terminology patterns
for pattern_config in terminology_patterns:
compiled_pattern = pattern_config.compile()
matches = list(compiled_pattern.finditer(sentence))
for match in matches:
if pattern_config.replacement: # Only if there's a replacement term
unique_issues.add((match.group(), pattern_config.replacement))
# Check prohibited patterns
for pattern_config in prohibited_patterns:
compiled_pattern = pattern_config.compile()
match = compiled_pattern.search(sentence)
if match and pattern_config.replacement: # Only if there's a replacement term
unique_issues.add((match.group(), pattern_config.replacement))
# Format issues as simple replacement instructions
formatted_issues = [
{'incorrect_term': incorrect, 'correct_term': correct}
for incorrect, correct in sorted(unique_issues) # Sort for consistent output
]
return DocumentCheckResult(success=not formatted_issues, issues=formatted_issues)
@profile_performance
def check_section_symbol_usage(self, doc: List[str]) -> DocumentCheckResult:
"""Check for section symbol (Β§) usage issues."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
issues = []
for paragraph in doc:
sentences = re.split(r'(?<=[.!?])\s+', paragraph.strip())
for sentence in sentences:
sentence = sentence.strip()
# Check 14 CFR citations only
cfr_matches = re.finditer(r'\b14 CFR Β§\s*(\d+\.\d+)\b', sentence)
for match in cfr_matches:
# Skip if this is part of a U.S.C. citation
if not re.search(r'U\.S\.C\.\s*Β§', sentence):
full_match = match.group(0)
section_num = match.group(1)
issues.append({
'incorrect': full_match,
'correct': f'14 CFR {section_num}',
'description': f"Replace '{full_match}' with '14 CFR {section_num}'"
})
# Skip any checks for sections that are part of U.S.C. citations
if re.search(r'U\.S\.C\.\s*(?:Β§|Β§Β§)', sentence):
continue
# Skip any checks for sections that are part of 14 CFR citations
if re.search(r'14 CFR\s*Β§', sentence):
continue
# Check section symbol at start of sentence
if sentence.startswith('Β§'):
match = re.match(r'^Β§\s*(\d+(?:\.\d+)?)', sentence)
if match:
section_num = match.group(1)
issues.append({
'incorrect': f'Β§ {section_num}',
'correct': f'Section {section_num}',
'description': f"Replace 'Β§ {section_num}' with 'Section {section_num}'"
})
return DocumentCheckResult(success=len(issues) == 0, issues=issues)
@profile_performance
def caption_check(self, doc: List[str], doc_type: str, caption_type: str) -> DocumentCheckResult:
"""Check for correctly formatted table or figure captions."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
incorrect_captions = []
for paragraph in doc:
paragraph_strip = paragraph.strip()
# Check if paragraph starts with the caption type and includes a number
if paragraph_strip.lower().startswith(caption_type.lower()):
# Look for any number pattern after the caption type
number_match = re.search(rf'{caption_type}\s+(\d+(?:-\d+)?)', paragraph_strip, re.IGNORECASE)
if number_match:
number_format = number_match.group(1)
if doc_type in ["Advisory Circular", "Order"]:
if '-' not in number_format:
incorrect_captions.append({
'incorrect_caption': f"{caption_type} {number_format}",
'doc_type': doc_type,
'caption_type': caption_type
})
else:
if '-' in number_format:
incorrect_captions.append({
'incorrect_caption': f"{caption_type} {number_format}",
'doc_type': doc_type,
'caption_type': caption_type
})
return DocumentCheckResult(
success=len(incorrect_captions) == 0,
issues=incorrect_captions,
details={
'document_type': doc_type,
'caption_type': caption_type
}
)
@profile_performance
def table_figure_reference_check(self, doc: List[str], doc_type: str) -> DocumentCheckResult:
"""Check for correctly formatted table and figure references."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
incorrect_references = []
# Pattern to identify table/figure captions
caption_pattern = re.compile(r'^(Table|Figure)\s+\d+[-\d]*\.?', re.IGNORECASE)
# Patterns for references within sentences and at start
table_ref_pattern = re.compile(r'\b([Tt]able)\s+\d+(?:-\d+)?')
figure_ref_pattern = re.compile(r'\b([Ff]igure)\s+\d+(?:-\d+)?')
for paragraph in doc:
# Skip if this is a caption line
if caption_pattern.match(paragraph.strip()):
continue
# Split into sentences while preserving punctuation
sentences = re.split(r'(?<=[.!?])\s+', paragraph)
for sentence in sentences:
sentence = sentence.strip()
if not sentence:
continue
# Check table references
for pattern, ref_type in [(table_ref_pattern, "Table"), (figure_ref_pattern, "Figure")]:
matches = list(pattern.finditer(sentence))
for match in matches:
ref = match.group()
word = match.group(1) # The actual "Table" or "Figure" word
# Get text before the reference
text_before = sentence[:match.start()].strip()
# Determine if reference is at start of sentence
is_sentence_start = not text_before or text_before.endswith((':',';'))
if is_sentence_start and word[0].islower():
incorrect_references.append({
'reference': ref,
'issue': f"{ref_type} reference at sentence start should be capitalized",
'sentence': sentence,
'correct_form': ref.capitalize()
})
elif not is_sentence_start and word[0].isupper():
incorrect_references.append({
'reference': ref,
'issue': f"{ref_type} reference within sentence should be lowercase",
'sentence': sentence,
'correct_form': ref.lower()
})
return DocumentCheckResult(success=len(incorrect_references) == 0, issues=incorrect_references)
@profile_performance
def document_title_check(self, doc_path: str, doc_type: str) -> DocumentCheckResult:
"""
Check for correct formatting of document titles.
For Advisory Circulars: Use italics without quotes
For all other document types: Use quotes without italics
Args:
doc_path: Path to the document
doc_type: Type of document being checked
Returns:
DocumentCheckResult: Results of document title check
"""
try:
doc = Document(doc_path)
except Exception as e:
self.logger.error(f"Error reading the document in title check: {e}")
return DocumentCheckResult(success=False, issues=[{'error': str(e)}])
incorrect_titles = []
# Define formatting rules based on document type
use_italics = doc_type == "Advisory Circular"
use_quotes = doc_type != "Advisory Circular"
# Pattern to match document references (e.g., "AC 25.1309-1B, System Design and Analysis")
doc_ref_pattern = re.compile(
r'(?:AC|Order|Policy|Notice)\s+[\d.-]+[A-Z]?,\s+([^,.]+)(?:[,.]|$)'
)
for paragraph in doc.paragraphs:
matches = doc_ref_pattern.finditer(paragraph.text)
for match in matches:
title_text = match.group(1).strip()
title_start = match.start(1)
title_end = match.end(1)
# Check formatting within the matched range
title_is_italicized = False
title_in_quotes = False
current_pos = 0
for run in paragraph.runs:
run_length = len(run.text)
run_start = current_pos
run_end = current_pos + run_length
# Check if this run overlaps with the title
if (run_start <= title_start < run_end or
run_start < title_end <= run_end or
title_start <= run_start < title_end):
title_is_italicized = title_is_italicized or run.italic
# Check for any type of quotation marks
title_in_quotes = title_in_quotes or any(
q in run.text for q in ['"', "'", '"', '"', '"', '"']
)
current_pos += run_length
# Determine if formatting is incorrect
formatting_incorrect = False
issue_message = []
if use_italics:
if not title_is_italicized:
formatting_incorrect = True
issue_message.append("should be italicized")
if title_in_quotes:
formatting_incorrect = True
issue_message.append("should not be in quotes")
else: # use quotes
if title_is_italicized:
formatting_incorrect = True
issue_message.append("should not be italicized")
if not title_in_quotes:
formatting_incorrect = True
issue_message.append("should be in quotes")
if formatting_incorrect:
incorrect_titles.append({
'text': title_text,
'issue': ' and '.join(issue_message),
'sentence': paragraph.text.strip(),
'correct_format': 'italics' if use_italics else 'quotes'
})
success = len(incorrect_titles) == 0
return DocumentCheckResult(
success=success,
issues=incorrect_titles,
details={
'document_type': doc_type,
'formatting_rule': 'italics' if use_italics else 'quotes'
}
)
@profile_performance
def double_period_check(self, doc: List[str]) -> DocumentCheckResult:
"""Check for sentences that end with two periods."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
incorrect_sentences = []
# Common abbreviations that end with a period but don't end sentences
abbreviations = {
'U.S.C.', 'U.S.', 'CFR', 'e.g.', 'i.e.', 'etc.', 'vs.', 'Dr.', 'Mr.',
'Mrs.', 'Ms.', 'Prof.', 'Ph.D.', 'M.D.', 'B.A.', 'M.A.', 'Ph.D.'
}
# Create a regex pattern that matches these abbreviations
abbr_pattern = '|'.join(re.escape(abbr) for abbr in abbreviations)
for paragraph in doc:
# First, protect abbreviations from being checked
protected_paragraph = re.sub(
f'({abbr_pattern})',
lambda m: m.group(1).replace('.', 'ABBR_DOT'),
paragraph
)
# Split the paragraph into sentences based on common sentence-ending punctuation
sentences = re.split(r'(?<=[.!?]) +', protected_paragraph)
for sentence in sentences:
# Restore the periods in abbreviations
sentence = sentence.replace('ABBR_DOT', '.')
if sentence.endswith('..'):
incorrect_sentences.append({'sentence': sentence.strip()})
success = len(incorrect_sentences) == 0
return DocumentCheckResult(success=success, issues=incorrect_sentences)
@profile_performance
def spacing_check(self, doc: List[str]) -> DocumentCheckResult:
"""Check for correct spacing in the document."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
issues = []
try:
for paragraph in doc:
# Skip empty paragraphs
if not paragraph.strip():
continue
# Skip paragraphs with tabs
if '\t' in paragraph:
continue
# Check for multiple spaces between words, but ignore spaces around parentheses
# First, temporarily replace valid parenthetical patterns to protect them
working_text = paragraph
# Protect common regulatory reference patterns
patterns_to_ignore = [
r'\d+\(\d+\)\([a-z]\)', # matches patterns like 25(1)(a)
r'\d+\([a-z]\)', # matches patterns like 25(a)
r'\([a-z]\)\(\d+\)', # matches patterns like (a)(1)
r'\(\d+\)\([a-z]\)', # matches patterns like (1)(a)
r'\([a-z]\)', # matches single letter references like (a)
r'\(\d+\)', # matches number references like (1)
]
for pattern in patterns_to_ignore:
working_text = re.sub(pattern, lambda m: 'PROTECTED' + str(hash(m.group())), working_text)
# Now check for multiple spaces
matches = re.finditer(r'[ ]{2,}', working_text)
for match in matches:
issues.append({
'incorrect': match.group(),
'context': paragraph.strip(),
'description': "Remove extra spaces"
})
except Exception as e:
self.logger.error(f"Error in spacing check: {str(e)}")
return DocumentCheckResult(success=False, issues=[{'error': f'Spacing check failed: {str(e)}'}])
return DocumentCheckResult(success=len(issues) == 0, issues=issues)
def _format_spacing_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format spacing issues with clear instructions for fixing."""
formatted_issues = []
if result.issues:
for issue in result.issues:
if 'error' in issue:
formatted_issues.append(f" β’ {issue['error']}")
else:
formatted_issues.append(
f" β’ {issue['description']} in: \"{issue['context']}\""
)
return formatted_issues
@profile_performance
def check_abbreviation_usage(self, doc: List[str]) -> DocumentCheckResult:
"""Check for abbreviation consistency after first definition."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
# Track abbreviations and their usage
abbreviations = {} # Store defined abbreviations
inconsistent_uses = [] # Track full term usage after definition
def process_sentence(sentence: str) -> None:
"""Process a single sentence for abbreviation usage."""
for acronym, data in abbreviations.items():
full_term = data["full_term"]
if full_term not in sentence:
continue
# Skip if this is the definition sentence
if sentence.strip() == data["first_occurrence"]:
continue
# Track inconsistent usage
if not data["defined"]:
inconsistent_uses.append({
'issue_type': 'full_term_after_acronym',
'full_term': full_term,
'acronym': acronym,
'sentence': sentence.strip(),
'definition_context': data["first_occurrence"]
})
data["defined"] = False # Mark as used
# Process each paragraph
for paragraph in doc:
sentences = re.split(r'(?<=[.!?])\s+', paragraph)
for sentence in sentences:
process_sentence(sentence.strip())
success = len(inconsistent_uses) == 0
return DocumentCheckResult(success=success, issues=inconsistent_uses)
@profile_performance
def check_date_formats(self, doc: List[str]) -> DocumentCheckResult:
"""Check for inconsistent date formats while ignoring aviation reference numbers."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
# Get patterns from registry
date_patterns = self.config_manager.pattern_registry.get('dates', [])
# Patterns to ignore (aviation references)
ignore_patterns = [
r'\bAC\s*\d+(?:[-.]\d+)*[A-Z]*\b', # AC reference pattern
r'\bAD \d{4}-\d{2}-\d{2}\b', # Airworthiness Directive references
r'\bSWPM \d{2}-\d{2}-\d{2}\b', # Standard Wiring Practices Manual references
r'\bAMM \d{2}-\d{2}-\d{2}\b', # Aircraft Maintenance Manual references
r'\bSOPM \d{2}-\d{2}-\d{2}\b', # Standard Operating Procedure references
r'\b[A-Z]{2,4} \d{2}-\d{2}-\d{2}\b' # Generic manual reference pattern
]
# Combine ignore patterns into one
ignore_regex = '|'.join(f'(?:{pattern})' for pattern in ignore_patterns)
ignore_pattern = re.compile(ignore_regex)
# Track unique issues
unique_issues = []
# Use _process_sentences helper
for sentence, paragraph in self._process_sentences(doc, skip_empty=True, skip_headings=True):
# First, identify and temporarily remove text that should be ignored
working_sentence = sentence
# Find all matches to ignore
ignored_matches = list(ignore_pattern.finditer(sentence))
# Replace ignored patterns with placeholders
for match in reversed(ignored_matches):
start, end = match.span()
working_sentence = working_sentence[:start] + 'X' * (end - start) + working_sentence[end:]
# Now check for date patterns in the modified sentence
for pattern_config in date_patterns:
matches = list(re.finditer(pattern_config.pattern, working_sentence))
for match in matches:
# Get the original text from the match position
original_date = sentence[match.start():match.end()]
# Create formatted issue with incorrect/correct format
formatted_issue = {
'incorrect': original_date,
'correct': 'Month Day, Year'
}
unique_issues.append(formatted_issue)
return DocumentCheckResult(success=len(unique_issues) == 0, issues=unique_issues)
@profile_performance
def check_placeholders(self, doc: List[str]) -> DocumentCheckResult:
"""Check for placeholders that should be removed."""
def process_placeholders(doc: List[str], patterns: List[PatternConfig]) -> DocumentCheckResult:
tbd_placeholders = []
to_be_determined_placeholders = []
to_be_added_placeholders = []
pattern_categories = {
r'\bTBD\b': ('tbd', tbd_placeholders),
r'\bTo be determined\b': ('to_be_determined', to_be_determined_placeholders),
r'\bTo be added\b': ('to_be_added', to_be_added_placeholders)
}
# Use _process_sentences helper
for sentence, paragraph in self._process_sentences(doc, skip_empty=True, skip_headings=True):
for pattern_config in patterns:
compiled_pattern = re.compile(pattern_config.pattern, re.IGNORECASE)
for pattern_key, (category_name, category_list) in pattern_categories.items():
if pattern_config.pattern == pattern_key:
matches = compiled_pattern.finditer(sentence)
for match in matches:
category_list.append({
'placeholder': match.group().strip(),
'sentence': sentence.strip(),
'description': pattern_config.description
})
# Compile issues
issues = []
if tbd_placeholders:
issues.append({
'issue_type': 'tbd_placeholder',
'description': 'Remove TBD placeholder',
'occurrences': tbd_placeholders
})
if to_be_determined_placeholders:
issues.append({
'issue_type': 'to_be_determined_placeholder',
'description': "Remove 'To be determined' placeholder",
'occurrences': to_be_determined_placeholders
})
if to_be_added_placeholders:
issues.append({
'issue_type': 'to_be_added_placeholder',
'description': "Remove 'To be added' placeholder",
'occurrences': to_be_added_placeholders
})
details = {
'total_placeholders': len(tbd_placeholders) +
len(to_be_determined_placeholders) +
len(to_be_added_placeholders),
'placeholder_types': {
'TBD': len(tbd_placeholders),
'To be determined': len(to_be_determined_placeholders),
'To be added': len(to_be_added_placeholders)
}
}
return DocumentCheckResult(success=len(issues) == 0, issues=issues, details=details)
return self._process_patterns(doc, 'placeholders', process_placeholders)
@profile_performance
def _process_patterns(
self,
doc: List[str],
pattern_category: str,
process_func: Optional[Callable] = None
) -> DocumentCheckResult:
"""
Process document text against patterns from a specific category.
Args:
doc: List of document paragraphs
pattern_category: Category of patterns to check against
process_func: Optional custom processing function
Returns:
DocumentCheckResult with processed issues
"""
if not self.validate_input(doc):
self.logger.error("Invalid document input for pattern check")
return DocumentCheckResult(
success=False,
issues=[{'error': 'Invalid document input'}]
)
# Get patterns from registry
patterns = self.config_manager.pattern_registry.get(pattern_category, [])
if not patterns:
self.logger.warning(f"No patterns found for category: {pattern_category}")
return DocumentCheckResult(
success=True,
issues=[],
details={'message': f'No patterns defined for {pattern_category}'}
)
# Use custom processing function if provided
if process_func:
return process_func(doc, patterns)
# Default processing with deduplication
unique_issues = set() # Using a set to track unique issues
for paragraph in doc:
sentences = re.split(r'(?<=[.!?])\s+', paragraph)
for sentence in sentences:
sentence = sentence.strip()
if not sentence:
continue
for pattern_config in patterns:
matches = list(re.finditer(pattern_config.pattern, sentence))
if matches:
# Add each match as a tuple to ensure uniqueness
for match in matches:
unique_issues.add((
match.group(), # The matched text
pattern_config.description, # The issue description
pattern_config.replacement if hasattr(pattern_config, 'replacement') else None
))
# Convert unique issues back to the expected format
formatted_issues = [
{
'incorrect': issue[0],
'description': issue[1],
'replacement': issue[2]
}
for issue in sorted(unique_issues) # Sort for consistent output
]
return DocumentCheckResult(success=len(formatted_issues) == 0, issues=formatted_issues)
def run_all_checks(self, doc_path: str, doc_type: str, template_type: Optional[str] = None) -> Dict[str, DocumentCheckResult]:
"""
Run all checks on the document.
Args:
doc_path (str): Path to the document.
doc_type (str): Type of the document.
template_type (str, optional): Template type, if applicable.
Returns:
Dict[str, DocumentCheckResult]: Dictionary of check names to results.
"""
# Read the document
doc = self.extract_paragraphs(doc_path)
# Retrieve any specific flags
checks_config = self.config_manager.config['document_types'].get(doc_type, {})
skip_title_check = checks_config.get('skip_title_check', False)
# Initialize results dictionary
results = {}
# Define order of checks for better organization
check_sequence = [
('readability_check', lambda: self.check_readability(doc)),
('heading_title_check', lambda: self.heading_title_check(doc, doc_type)),
('heading_title_period_check', lambda: self.heading_title_period_check(doc, doc_type)),
('terminology_check', lambda: self.check_terminology(doc)),
('acronym_check', lambda: self.acronym_check(doc)),
('acronym_usage_check', lambda: self.acronym_usage_check(doc)),
('section_symbol_usage_check', lambda: self.check_section_symbol_usage(doc)),
('508_compliance_check', lambda: self.check_508_compliance(doc_path)),
# ('cross_references_check', lambda: self.check_cross_references(doc_path)),
('hyperlink_check', lambda: self.check_hyperlinks(doc)),
('date_formats_check', lambda: self.check_date_formats(doc)),
('placeholders_check', lambda: self.check_placeholders(doc)),
('document_title_check', lambda: self.document_title_check(doc_path, doc_type) if not skip_title_check else DocumentCheckResult(success=True, issues=[])),
('caption_check_table', lambda: self.caption_check(doc, doc_type, 'Table')),
('caption_check_figure', lambda: self.caption_check(doc, doc_type, 'Figure')),
('table_figure_reference_check', lambda: self.table_figure_reference_check(doc, doc_type)),
('parentheses_check', lambda: self.check_parentheses(doc)),
('double_period_check', lambda: self.double_period_check(doc)),
('spacing_check', lambda: self.spacing_check(doc)),
('paragraph_length_check', lambda: self.check_paragraph_length(doc)),
('sentence_length_check', lambda: self.check_sentence_length(doc)),
]
# Run each check and store results
for check_name, check_func in check_sequence:
try:
results[check_name] = check_func()
except Exception as e:
self.logger.error(f"Error running {check_name}: {str(e)}")
results[check_name] = DocumentCheckResult(
success=False,
issues=[{'error': f'Check failed with error: {str(e)}'}]
)
return results
def _compile_issues(
self,
issue_groups: Dict[str, List[Dict[str, Any]]],
category_descriptions: Dict[str, str]
) -> List[Dict[str, Any]]:
"""
Compile issues from different groups into a standardized format.
Args:
issue_groups: Dictionary of issue type to list of issues
category_descriptions: Dictionary of issue type to description
Returns:
List of compiled issues in standardized format
"""
compiled_issues = []
for issue_type, issues in issue_groups.items():
if issues: # Only add groups that have issues
compiled_issues.append({
'issue_type': issue_type,
'description': category_descriptions.get(
issue_type,
f'Issues found in {issue_type}'
),
'occurrences': issues
})
return compiled_issues
def _process_sentences(
self,
doc: List[str],
skip_empty: bool = True,
skip_headings: bool = False
) -> List[Tuple[str, str]]:
"""
Process document paragraphs into sentences with their parent paragraphs.
Args:
doc: List of document paragraphs
skip_empty: Whether to skip empty sentences
skip_headings: Whether to skip lines that appear to be headings
Returns:
List of tuples containing (sentence, parent_paragraph)
"""
sentences = []
# Common abbreviations that end with a period but don't end sentences
abbreviations = {
'U.S.C.', 'U.S.', 'CFR', 'e.g.', 'i.e.', 'etc.', 'vs.', 'Dr.', 'Mr.',
'Mrs.', 'Ms.', 'Prof.', 'Ph.D.', 'M.D.', 'B.A.', 'M.A.', 'Ph.D.'
}
# Legal citation patterns that shouldn't be split
legal_citations = [
r'\d+ U\.S\.C\. Β§ \d+\([a-zA-Z0-9]*\)(?:\([a-zA-Z0-9]*\))?', # e.g., 5 U.S.C. Β§ 533(a)(1)
r'\d+ CFR Β§ \d+\.\d+', # e.g., 14 CFR Β§ 1.1
r'\d+ CFR part \d+' # e.g., 14 CFR part 1
]
# Create a regex pattern that matches these abbreviations
abbr_pattern = '|'.join(re.escape(abbr) for abbr in abbreviations)
legal_pattern = '|'.join(legal_citations)
for paragraph in doc:
paragraph = paragraph.strip()
# Skip heading-like paragraphs if requested
if skip_headings:
words = paragraph.split()
if all(word.isupper() for word in words) and any(
word in self.HEADING_WORDS for word in words
):
continue
# First, protect legal citations from being split
protected_paragraph = re.sub(
f'({legal_pattern})',
lambda m: m.group(1).replace('.', 'LEGAL_DOT'),
paragraph
)
# Then protect abbreviations from being split
protected_paragraph = re.sub(
f'({abbr_pattern})',
lambda m: m.group(1).replace('.', 'ABBR_DOT'),
protected_paragraph
)
# Split paragraph into sentences
para_sentences = re.split(r'(?<=[.!?])\s+', protected_paragraph)
# Process each sentence
for sentence in para_sentences:
# Restore the periods in legal citations and abbreviations
sentence = sentence.replace('LEGAL_DOT', '.')
sentence = sentence.replace('ABBR_DOT', '.')
sentence = sentence.strip()
if skip_empty and not sentence:
continue
sentences.append((sentence, paragraph))
return sentences
@profile_performance
def check_parentheses(self, doc: List[str]) -> DocumentCheckResult:
"""
Check for matching parentheses in the document.
Args:
doc (List[str]): List of document paragraphs
Returns:
DocumentCheckResult: Result containing any mismatched parentheses issues
"""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
issues = []
for i, paragraph in enumerate(doc, 1):
if not paragraph.strip(): # Skip empty paragraphs
continue
stack = [] # Track unmatched opening parentheses
sentences = re.split(r'(?<=[.!?])\s+', paragraph) # Split paragraph into sentences
for sentence in sentences:
for j, char in enumerate(sentence):
if char == '(':
stack.append((sentence, j)) # Store sentence and character position
elif char == ')':
if stack:
stack.pop() # Remove matching opening parenthesis
else:
# No matching opening parenthesis
issues.append({
'type': 'missing_opening',
'paragraph': i, # Still tracked but not included in the message
'position': j,
'text': sentence,
'message': f"Add an opening parenthesis to the sentence: \"{sentence.strip()}\""
})
# Check for any unmatched opening parentheses left in the stack
for unmatched in stack:
sentence, pos = unmatched
issues.append({
'type': 'missing_closing',
'paragraph': i, # Still tracked but not included in the message
'position': pos,
'text': sentence,
'message': f"Add a closing parenthesis to the sentence: \"{sentence.strip()}\""
})
return DocumentCheckResult(success=len(issues) == 0, issues=issues)
@profile_performance
def spacing_check(self, doc: List[str]) -> DocumentCheckResult:
"""Check for correct spacing in the document."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
spacing_patterns = self.config_manager.pattern_registry.get('spacing', [])
issues = []
try:
for paragraph in doc:
if not paragraph.strip() or '\t' in paragraph:
continue
for pattern_config in spacing_patterns:
matches = re.finditer(pattern_config.pattern, paragraph)
for match in matches:
groups = match.groups()
description = pattern_config.description.replace('{0}', groups[0]).replace('{1}', groups[1])
context_start = max(0, match.start() - 20)
context_end = min(len(paragraph), match.end() + 20)
context = paragraph[context_start:context_end].strip()
issues.append({
'type': 'spacing',
'incorrect': match.group(),
'context': context,
'description': description
})
except Exception as e:
self.logger.error(f"Error in spacing check: {str(e)}")
return DocumentCheckResult(success=False, issues=[{'error': f'Spacing check failed: {str(e)}'}])
return DocumentCheckResult(success=len(issues) == 0, issues=issues)
def _format_spacing_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format spacing issues with clear instructions for fixing."""
formatted_issues = []
if result.issues:
for issue in result.issues:
if 'error' in issue:
formatted_issues.append(f" β’ {issue['error']}")
else:
formatted_issues.append(
f" β’ {issue['description']} in: \"{issue['context']}\""
)
return formatted_issues
@profile_performance
def check_abbreviation_usage(self, doc: List[str]) -> DocumentCheckResult:
"""Check for abbreviation consistency after first definition."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
# Track abbreviations and their usage
abbreviations = {} # Store defined abbreviations
inconsistent_uses = [] # Track full term usage after definition
def process_sentence(sentence: str) -> None:
"""Process a single sentence for abbreviation usage."""
for acronym, data in abbreviations.items():
full_term = data["full_term"]
if full_term not in sentence:
continue
# Skip if this is the definition sentence
if sentence.strip() == data["first_occurrence"]:
continue
# Track inconsistent usage
if not data["defined"]:
inconsistent_uses.append({
'issue_type': 'full_term_after_acronym',
'full_term': full_term,
'acronym': acronym,
'sentence': sentence.strip(),
'definition_context': data["first_occurrence"]
})
data["defined"] = False # Mark as used
# Process each paragraph
for paragraph in doc:
sentences = re.split(r'(?<=[.!?])\s+', paragraph)
for sentence in sentences:
process_sentence(sentence.strip())
success = len(inconsistent_uses) == 0
return DocumentCheckResult(success=success, issues=inconsistent_uses)
@profile_performance
def check_date_formats(self, doc: List[str]) -> DocumentCheckResult:
"""Check for inconsistent date formats while ignoring aviation reference numbers."""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
# Get patterns from registry
date_patterns = self.config_manager.pattern_registry.get('dates', [])
# Patterns to ignore (aviation references)
ignore_patterns = [
r'\bAD \d{4}-\d{2}-\d{2}\b', # Airworthiness Directive references
r'\bSWPM \d{2}-\d{2}-\d{2}\b', # Standard Wiring Practices Manual references
r'\bAMM \d{2}-\d{2}-\d{2}\b', # Aircraft Maintenance Manual references
r'\bSOPM \d{2}-\d{2}-\d{2}\b', # Standard Operating Procedure references
r'\b[A-Z]{2,4} \d{2}-\d{2}-\d{2}\b' # Generic manual reference pattern
]
# Combine ignore patterns into one
ignore_regex = '|'.join(f'(?:{pattern})' for pattern in ignore_patterns)
ignore_pattern = re.compile(ignore_regex)
# Track unique issues
unique_issues = []
# Use _process_sentences helper
for sentence, paragraph in self._process_sentences(doc, skip_empty=True, skip_headings=True):
# First, identify and temporarily remove text that should be ignored
working_sentence = sentence
# Find all matches to ignore
ignored_matches = list(ignore_pattern.finditer(sentence))
# Replace ignored patterns with placeholders
for match in reversed(ignored_matches):
start, end = match.span()
working_sentence = working_sentence[:start] + 'X' * (end - start) + working_sentence[end:]
# Now check for date patterns in the modified sentence
for pattern_config in date_patterns:
matches = list(re.finditer(pattern_config.pattern, working_sentence))
for match in matches:
# Get the original text from the match position
original_date = sentence[match.start():match.end()]
# Create formatted issue with incorrect/correct format
formatted_issue = {
'incorrect': original_date,
'correct': 'Month Day, Year'
}
unique_issues.append(formatted_issue)
return DocumentCheckResult(success=len(unique_issues) == 0, issues=unique_issues)
@profile_performance
def check_placeholders(self, doc: List[str]) -> DocumentCheckResult:
"""Check for placeholders that should be removed."""
def process_placeholders(doc: List[str], patterns: List[PatternConfig]) -> DocumentCheckResult:
tbd_placeholders = []
to_be_determined_placeholders = []
to_be_added_placeholders = []
pattern_categories = {
r'\bTBD\b': ('tbd', tbd_placeholders),
r'\bTo be determined\b': ('to_be_determined', to_be_determined_placeholders),
r'\bTo be added\b': ('to_be_added', to_be_added_placeholders)
}
# Use _process_sentences helper
for sentence, paragraph in self._process_sentences(doc, skip_empty=True, skip_headings=True):
for pattern_config in patterns:
compiled_pattern = re.compile(pattern_config.pattern, re.IGNORECASE)
for pattern_key, (category_name, category_list) in pattern_categories.items():
if pattern_config.pattern == pattern_key:
matches = compiled_pattern.finditer(sentence)
for match in matches:
category_list.append({
'placeholder': match.group().strip(),
'sentence': sentence.strip(),
'description': pattern_config.description
})
# Compile issues
issues = []
if tbd_placeholders:
issues.append({
'issue_type': 'tbd_placeholder',
'description': 'Remove TBD placeholder',
'occurrences': tbd_placeholders
})
if to_be_determined_placeholders:
issues.append({
'issue_type': 'to_be_determined_placeholder',
'description': "Remove 'To be determined' placeholder",
'occurrences': to_be_determined_placeholders
})
if to_be_added_placeholders:
issues.append({
'issue_type': 'to_be_added_placeholder',
'description': "Remove 'To be added' placeholder",
'occurrences': to_be_added_placeholders
})
details = {
'total_placeholders': len(tbd_placeholders) +
len(to_be_determined_placeholders) +
len(to_be_added_placeholders),
'placeholder_types': {
'TBD': len(tbd_placeholders),
'To be determined': len(to_be_determined_placeholders),
'To be added': len(to_be_added_placeholders)
}
}
return DocumentCheckResult(success=len(issues) == 0, issues=issues, details=details)
return self._process_patterns(doc, 'placeholders', process_placeholders)
@profile_performance
def _process_patterns(
self,
doc: List[str],
pattern_category: str,
process_func: Optional[Callable] = None
) -> DocumentCheckResult:
"""
Process document text against patterns from a specific category.
Args:
doc: List of document paragraphs
pattern_category: Category of patterns to check against
process_func: Optional custom processing function
Returns:
DocumentCheckResult with processed issues
"""
if not self.validate_input(doc):
self.logger.error("Invalid document input for pattern check")
return DocumentCheckResult(
success=False,
issues=[{'error': 'Invalid document input'}]
)
# Get patterns from registry
patterns = self.config_manager.pattern_registry.get(pattern_category, [])
if not patterns:
self.logger.warning(f"No patterns found for category: {pattern_category}")
return DocumentCheckResult(
success=True,
issues=[],
details={'message': f'No patterns defined for {pattern_category}'}
)
# Use custom processing function if provided
if process_func:
return process_func(doc, patterns)
# Default processing with deduplication
unique_issues = set() # Using a set to track unique issues
for paragraph in doc:
sentences = re.split(r'(?<=[.!?])\s+', paragraph)
for sentence in sentences:
sentence = sentence.strip()
if not sentence:
continue
for pattern_config in patterns:
matches = list(re.finditer(pattern_config.pattern, sentence))
if matches:
# Add each match as a tuple to ensure uniqueness
for match in matches:
unique_issues.add((
match.group(), # The matched text
pattern_config.description, # The issue description
pattern_config.replacement if hasattr(pattern_config, 'replacement') else None
))
# Convert unique issues back to the expected format
formatted_issues = [
{
'incorrect': issue[0],
'description': issue[1],
'replacement': issue[2]
}
for issue in sorted(unique_issues) # Sort for consistent output
]
return DocumentCheckResult(success=len(formatted_issues) == 0, issues=formatted_issues)
@profile_performance
def check_paragraph_length(self, doc: List[str]) -> DocumentCheckResult:
"""
Check for overly long paragraphs that may need to be split up.
Args:
doc (List[str]): List of document paragraphs
Returns:
DocumentCheckResult: Results of paragraph length check
"""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
issues = []
for paragraph in doc:
if not paragraph.strip(): # Skip empty paragraphs
continue
# Count sentences (split on period, exclamation, question mark followed by space)
sentences = re.split(r'(?<=[.!?])\s+', paragraph.strip())
# Count lines (split on newlines or when length exceeds ~80 characters)
lines = []
current_line = ""
for word in paragraph.split():
if len(current_line) + len(word) + 1 <= 80:
current_line += " " + word if current_line else word
else:
lines.append(current_line)
current_line = word
if current_line:
lines.append(current_line)
# Check if paragraph exceeds either threshold
if len(sentences) > 6 or len(lines) > 8:
# Get first sentence for context
first_sentence = sentences[0].strip()
issues.append(f"Review the paragraph that starts with: \"{first_sentence}\"")
return DocumentCheckResult(success=len(issues) == 0, issues=issues)
@profile_performance
def check_sentence_length(self, doc: List[str]) -> DocumentCheckResult:
"""
Check for overly long sentences that may need to be split for clarity.
Args:
doc (List[str]): List of document paragraphs
Returns:
DocumentCheckResult: Results of sentence length check including:
- success: Boolean indicating if all sentences are acceptable length
- issues: List of dicts with long sentence details
- details: Additional statistics about sentence lengths
"""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
issues = []
sentence_stats = {
'total_sentences': 0,
'long_sentences': 0,
'max_length': 0,
'avg_length': 0
}
# Skip patterns for technical content that might naturally be longer
skip_patterns = [
r'^(?:Note:|Warning:|Caution:)', # Notes and warnings
r'^\d+\.', # Numbered lists
r'^\([a-z]\)', # Letter lists
r'^\([0-9]\)', # Number lists in parentheses
r'^Table \d', # Table captions
r'^Figure \d', # Figure captions
r'(?:e\.g\.|i\.e\.|viz\.,)', # Latin abbreviations often used in complex sentences
r'\b(?:AC|AD|TSO|SFAR)\s+\d', # Technical references
r'\d+\s*(?:CFR|U\.S\.C\.)', # Regulatory references
]
skip_regex = re.compile('|'.join(skip_patterns), re.IGNORECASE)
total_words = 0
for paragraph in doc:
if not paragraph.strip():
continue
# Skip if paragraph matches any skip patterns
if skip_regex.search(paragraph):
continue
# Split into sentences while preserving punctuation
sentences = re.split(r'(?<=[.!?])\s+', paragraph.strip())
for sentence in sentences:
sentence = sentence.strip()
if not sentence:
continue
# Count words (splitting on whitespace)
words = sentence.split()
word_count = len(words)
sentence_stats['total_sentences'] += 1
total_words += word_count
if word_count > sentence_stats['max_length']:
sentence_stats['max_length'] = word_count
# Flag sentences over 35 words
if word_count > 35:
sentence_stats['long_sentences'] += 1
issues.append({
'sentence': sentence,
'word_count': word_count
})
# Calculate average sentence length
if sentence_stats['total_sentences'] > 0:
sentence_stats['avg_length'] = round(total_words / sentence_stats['total_sentences'], 1)
return DocumentCheckResult(
success=len(issues) == 0,
issues=issues,
details=sentence_stats
)
@profile_performance
def check_508_compliance(self, doc_path: str) -> DocumentCheckResult:
"""
Perform Section 508 compliance checks focusing on image alt text and heading structure.
"""
try:
doc = Document(doc_path)
issues = []
images_with_alt = 0
heading_structure = {}
heading_issues = [] # Separate list for heading-specific issues
hyperlink_issues = [] # New list for hyperlink issues
# Image alt text check
for shape in doc.inline_shapes:
alt_text = None
if hasattr(shape, '_inline') and hasattr(shape._inline, 'docPr'):
docPr = shape._inline.docPr
alt_text = docPr.get('descr') or docPr.get('title')
if alt_text:
images_with_alt += 1
else:
issues.append({
'category': 'image_alt_text',
'message': 'Image is missing descriptive alt text.',
'context': 'Ensure all images have descriptive alt text.'
})
# Enhanced heading structure check
headings = []
for paragraph in doc.paragraphs:
if paragraph.style.name.startswith('Heading'):
try:
level = int(paragraph.style.name.split()[-1])
text = paragraph.text.strip()
if not text:
continue
headings.append((text, level))
heading_structure[level] = heading_structure.get(level, 0) + 1
except ValueError:
continue
# Check heading hierarchy
if headings:
min_level = min(level for _, level in headings)
if min_level > 1:
heading_issues.append({
'severity': 'error',
'type': 'missing_h1',
'message': 'Document should start with a Heading 1',
'context': f"First heading found is level {headings[0][1]}: '{headings[0][0]}'",
'recommendation': 'Add a Heading 1 at the start of the document'
})
# Check for skipped levels
previous_heading = None
for text, level in headings:
if previous_heading:
prev_text, prev_level = previous_heading
# Only check for skipped levels when going deeper
if level > prev_level + 1:
missing_levels = list(range(prev_level + 1, level))
heading_issues.append({
'severity': 'error',
'type': 'skipped_levels',
'message': f"Skipped heading level(s) {', '.join(map(str, missing_levels))} - Found H{level} '{text}' after H{prev_level} '{prev_text}'. Add H{prev_level + 1} before this section.",
})
previous_heading = (text, level)
# Enhanced Hyperlink Accessibility Check
for paragraph in doc.paragraphs:
# Check both hyperlink fields and runs with hyperlink formatting
hyperlinks = []
# Method 1: Check for hyperlink fields
if hasattr(paragraph, '_element') and hasattr(paragraph._element, 'xpath'):
hyperlinks.extend(paragraph._element.xpath('.//w:hyperlink'))
# Method 2: Check for hyperlink style runs
for run in paragraph.runs:
if hasattr(run, '_element') and hasattr(run._element, 'rPr'):
if run._element.rPr is not None:
if run._element.rPr.xpath('.//w:rStyle[@w:val="Hyperlink"]'):
hyperlinks.append(run)
# Method 3: Check for direct hyperlink elements
if hasattr(run, '_r'):
if run._r.xpath('.//w:hyperlink'):
hyperlinks.append(run)
# Process found hyperlinks
for hyperlink in hyperlinks:
# Extract link text based on element type
if hasattr(hyperlink, 'text'): # For run objects
link_text = hyperlink.text.strip()
else: # For hyperlink elements
link_text = ''.join([t.text for t in hyperlink.xpath('.//w:t')])
if not link_text: # Skip empty links
continue
# Check for accessibility issues
non_descriptive = [
'click here', 'here', 'link', 'this link', 'more',
'read more', 'learn more', 'click', 'see this',
'see here', 'go', 'url', 'this', 'page'
]
if any(phrase == link_text.lower() for phrase in non_descriptive):
hyperlink_issues.append({
'category': 'hyperlink_accessibility',
'severity': 'warning',
'message': 'Non-descriptive hyperlink text detected',
'context': f'Link text: "{link_text}"',
'recommendation': 'Replace with descriptive text that indicates the link destination',
'user_message': f'Replace non-descriptive link text "{link_text}" with text that clearly indicates where the link will take the user'
})
elif len(link_text.strip()) < 4: # Check for very short link text
hyperlink_issues.append({
'category': 'hyperlink_accessibility',
'severity': 'warning',
'message': 'Hyperlink text may be too short to be meaningful',
'context': f'Link text: "{link_text}"',
'recommendation': 'Use longer, more descriptive text that indicates the link destination',
'user_message': f'Link text "{link_text}" is too short - use descriptive text that clearly indicates the link destination'
})
elif link_text.lower().startswith(('http', 'www', 'ftp')):
hyperlink_issues.append({
'category': 'hyperlink_accessibility',
'severity': 'warning',
'message': 'Raw URL used as link text',
'context': f'Link text: "{link_text}"',
'recommendation': 'Replace the URL with descriptive text that indicates the link destination',
'user_message': f'Replace the URL "{link_text}" with meaningful text that describes the link destination'
})
# Add hyperlink issues to main issues list
if hyperlink_issues:
issues.extend(hyperlink_issues)
# Combine all issues
if heading_issues:
issues.extend([{
'category': '508_compliance_heading_structure',
**issue
} for issue in heading_issues])
# Enhanced details with heading structure information
details = {
'total_images': len(doc.inline_shapes),
'images_with_alt': images_with_alt,
'heading_structure': {
'total_headings': len(headings),
'levels_found': dict(sorted(heading_structure.items())),
'hierarchy_depth': max(heading_structure.keys()) if heading_structure else 0,
'heading_sequence': [(text[:50] + '...' if len(text) > 50 else text, level)
for text, level in headings],
'issues_found': len(heading_issues)
},
'hyperlink_accessibility': { # New details section
'total_issues': len(hyperlink_issues),
'non_descriptive_links': sum(1 for issue in hyperlink_issues
if 'Non-descriptive' in issue['message']),
'raw_urls': sum(1 for issue in hyperlink_issues
if 'Raw URL' in issue['message'])
}
}
return DocumentCheckResult(
success=len(issues) == 0,
issues=issues,
details=details
)
except Exception as e:
self.logger.error(f"Error during 508 compliance check: {str(e)}")
return DocumentCheckResult(
success=False,
issues=[{
'category': 'error',
'message': f'Error performing 508 compliance check: {str(e)}'
}]
)
def _format_compliance_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format compliance issues with clear, user-friendly descriptions."""
formatted_issues = []
for issue in result.issues:
if issue.get('category') == '508_compliance_heading_structure':
# Existing heading structure formatting...
message = issue.get('message', 'No description provided')
context = issue.get('context', 'No context provided').strip()
recommendation = issue.get('recommendation', 'No recommendation provided').strip()
formatted_issues.append(
f" β’ {message}. Context: {context}. Recommendation: {recommendation}"
)
elif issue.get('category') == 'image_alt_text':
# Existing alt text formatting...
formatted_issues.append(
f" β’ {issue.get('message', 'No description provided')}. {issue.get('context', '')}"
)
elif issue.get('category') == 'hyperlink_accessibility':
# Use the new user-friendly message
formatted_issues.append(
f" β’ {issue.get('user_message', issue.get('message', 'No description provided'))}"
)
elif 'context' in issue and issue['context'].startswith('Link text:'):
# This catches the hyperlink issues that might not have the category set
link_text = issue['context'].replace('Link text:', '').strip().strip('"')
if any(phrase == link_text.lower() for phrase in ['here', 'click here', 'more', 'link']):
formatted_issues.append(
f" β’ Replace non-descriptive link text \"{link_text}\" with text that clearly indicates where the link will take the user"
)
elif link_text.lower().startswith(('http', 'www', 'ftp')):
formatted_issues.append(
f" β’ Replace the URL \"{link_text}\" with meaningful text that describes the link destination"
)
elif len(link_text) < 4:
formatted_issues.append(
f" β’ Link text \"{link_text}\" is too short - use descriptive text that clearly indicates the link destination"
)
else:
formatted_issues.append(f" β’ {issue.get('message', 'No description provided')} {issue['context']}")
else:
# Generic formatting for other issues
message = issue.get('message', 'No description provided')
context = issue.get('context', '').strip()
formatted_issues.append(
f" β’ {message} {context}"
)
return formatted_issues
@profile_performance
def check_hyperlinks(self, doc: List[str]) -> DocumentCheckResult:
"""
Enhanced hyperlink checker that identifies potentially broken URLs.
Args:
doc: List of document paragraphs.
Returns:
DocumentCheckResult with any potentially broken links.
"""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
issues = []
checked_urls = set()
# URL pattern - matches http/https URLs
url_pattern = re.compile(
r'https?://(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_\+.~#?&//=]*'
)
# Helper function to check a single URL
def check_url(url):
try:
response = requests.head(url, timeout=5, allow_redirects=True, headers={'User-Agent': 'CheckerTool/1.0'})
if response.status_code >= 400:
return {
'url': url,
'message': f"Broken link: {url} (HTTP {response.status_code})"
}
except requests.RequestException:
return {
'url': url,
'message': f"Check the link or internet connection: {url} (connection error)"
}
return None
# Extract and deduplicate URLs
for paragraph in doc:
urls = {match.group() for match in url_pattern.finditer(paragraph)}
checked_urls.update(urls)
# Concurrently check URLs
with ThreadPoolExecutor(max_workers=10) as executor:
future_to_url = {executor.submit(check_url, url): url for url in checked_urls}
for future in as_completed(future_to_url):
issue = future.result()
if issue:
issues.append(issue)
return DocumentCheckResult(
success=len(issues) == 0,
issues=issues,
details={
'total_urls_checked': len(checked_urls),
'broken_urls': len(issues)
}
)
def _load_valid_words(self) -> Set[str]:
"""
Load valid English words from valid_words.txt file.
Returns:
Set[str]: Set of valid English words in lowercase
"""
try:
# Get the directory containing the current file
current_dir = os.path.dirname(os.path.abspath(__file__))
words_file = os.path.join(current_dir, 'valid_words.txt')
# Load words from file
with open(words_file, 'r') as f:
words = {line.strip().lower() for line in f if line.strip()}
return words
except Exception as e:
self.logger.warning(f"Error loading word list: {e}")
return set() # Return empty set as fallback
@profile_performance
def check_cross_references(self, doc_path: str) -> DocumentCheckResult:
"""
Check for missing cross-referenced elements in the document.
"""
try:
doc = Document(doc_path)
except Exception as e:
self.logger.error(f"Error reading the document: {e}")
return DocumentCheckResult(success=False, issues=[{'error': str(e)}], details={})
heading_structure = self._extract_paragraph_numbering(doc)
valid_sections = {number for number, _ in heading_structure}
tables = set()
figures = set()
issues = []
# Skip patterns for external references
skip_patterns = [
r'(?:U\.S\.C\.|USC)\s+(?:Β§+\s*)?(?:Section|section)?\s*\d+',
r'Section\s+\d+(?:\([a-z]\))*\s+of\s+(?:the\s+)?(?:United States Code|U\.S\.C\.)',
r'Section\s+\d+(?:\([a-z]\))*\s+of\s+Title\s+\d+',
r'(?:Section|Β§)\s*\d+(?:\([a-z]\))*\s+of\s+the\s+Act',
r'Section\s+\d+\([a-z]\)',
r'Β§\s*\d+\([a-z]\)',
r'\d+\s*(?:CFR|C\.F\.R\.)',
r'Part\s+\d+(?:\.[0-9]+)*\s+of\s+Title\s+\d+',
r'Public\s+Law\s+\d+[-β]\d+',
r'Title\s+\d+,\s+Section\s+\d+(?:\([a-z]\))*',
r'\d+\s+U\.S\.C\.\s+\d+(?:\([a-z]\))*',
]
skip_regex = re.compile('|'.join(skip_patterns), re.IGNORECASE)
try:
# Extract tables and figures
for para in doc.paragraphs:
text = para.text.strip() if hasattr(para, 'text') else ''
# Table extraction
if text.lower().startswith('table'):
matches = [
re.match(r'^table\s+(\d{1,2}(?:-\d+)?)\b', text, re.IGNORECASE),
re.match(r'^table\s+(\d{1,2}(?:\.\d+)?)\b', text, re.IGNORECASE)
]
for match in matches:
if match:
tables.add(match.group(1))
# Figure extraction
if text.lower().startswith('figure'):
matches = [
re.match(r'^figure\s+(\d{1,2}(?:-\d+)?)\b', text, re.IGNORECASE),
re.match(r'^figure\s+(\d{1,2}(?:\.\d+)?)\b', text, re.IGNORECASE)
]
for match in matches:
if match:
figures.add(match.group(1))
# Check references
for para in doc.paragraphs:
para_text = para.text.strip() if hasattr(para, 'text') else ''
if not para_text or skip_regex.search(para_text):
continue
# Table reference check
table_refs = re.finditer(
r'(?:see|in|refer to)?\s*(?:table|Table)\s+(\d{1,2}(?:[-\.]\d+)?)\b',
para_text
)
for match in table_refs:
ref = match.group(1)
if ref not in tables:
issues.append({
'type': 'Table',
'reference': ref,
'context': para_text,
'message': f"Referenced Table {ref} not found in document"
})
# Figure reference check
figure_refs = re.finditer(
r'(?:see|in|refer to)?\s*(?:figure|Figure)\s+(\d{1,2}(?:[-\.]\d+)?)\b',
para_text
)
for match in figure_refs:
ref = match.group(1)
if ref not in figures:
issues.append({
'type': 'Figure',
'reference': ref,
'context': para_text,
'message': f"Referenced Figure {ref} not found in document"
})
# Section/paragraph reference check
section_refs = re.finditer(
r'(?:paragraph|section|appendix)\s+([A-Z]?\.?\d+(?:\.\d+)*)',
para_text,
re.IGNORECASE
)
for match in section_refs:
ref = match.group(1).strip('.')
if not skip_regex.search(para_text):
if ref not in valid_sections:
found = False
for valid_section in valid_sections:
if valid_section.strip('.') == ref.strip('.'):
found = True
break
if not found:
issues.append({
'type': 'Paragraph',
'reference': ref,
'context': para_text,
'message': f"Confirm paragraph {ref} referenced in '{para_text}' exists in the document"
})
except Exception as e:
self.logger.error(f"Error processing cross references: {str(e)}")
return DocumentCheckResult(
success=False,
issues=[{'type': 'error', 'message': f"Error processing cross references: {str(e)}"}],
details={}
)
return DocumentCheckResult(
success=len(issues) == 0,
issues=issues,
details={
'total_tables': len(tables),
'total_figures': len(figures),
'found_tables': sorted(list(tables)),
'found_figures': sorted(list(figures)),
'heading_structure': heading_structure,
'valid_sections': sorted(list(valid_sections))
}
)
def _extract_paragraph_numbering(self, doc: Document, in_appendix: bool = False) -> List[Tuple[str, str]]:
"""
Extract paragraph numbers from document headings.
"""
numbered_paragraphs = []
try:
# Track heading hierarchy (limit to 6 levels as per standard heading styles)
current_numbers = {
1: 0, # Heading 1: 1, 2, 3, ...
2: 0, # Heading 2: 1.1, 1.2, 1.3, ...
3: 0, # Heading 3: 1.1.1, 1.1.2, ...
4: 0,
5: 0,
6: 0
}
current_parent = {
2: 0, # Parent number for level 2
3: 0, # Parent number for level 3
4: 0,
5: 0,
6: 0
}
last_level = {
1: 0, # Last number used at each level
2: 0,
3: 0,
4: 0,
5: 0,
6: 0
}
for para in doc.paragraphs:
style_name = para.style.name if hasattr(para, 'style') and hasattr(para.style, 'name') else ''
text = para.text.strip() if hasattr(para, 'text') else ''
# Only process if it's a heading style
if style_name.startswith('Heading'):
try:
heading_level = int(style_name.replace('Heading ', ''))
# Skip if heading level is beyond our supported range
if heading_level > 6:
continue
if heading_level == 1:
# For Heading 1, simply increment
current_numbers[1] += 1
last_level[1] = current_numbers[1]
# Reset all lower levels
for level in range(2, 7): # Changed from 8 to 7
current_numbers[level] = 0
current_parent[level] = current_numbers[1]
last_level[level] = 0
else:
# Check if we're still in the same parent section
parent_changed = current_parent[heading_level] != current_numbers[heading_level - 1]
if parent_changed:
# Parent section changed
current_numbers[heading_level] = 1
current_parent[heading_level] = current_numbers[heading_level - 1]
else:
# Same parent, increment this level
current_numbers[heading_level] += 1
last_level[heading_level] = current_numbers[heading_level]
# Reset all lower levels
for level in range(heading_level + 1, 7): # Changed from 8 to 7
current_numbers[level] = 0
current_parent[level] = 0
last_level[level] = 0
# Build section number
section_parts = []
for level in range(1, heading_level + 1):
if level == 1:
section_parts.append(str(current_numbers[1]))
else:
if current_numbers[level] > 0:
section_parts.append(str(current_numbers[level]))
section_number = '.'.join(section_parts)
if text:
numbered_paragraphs.append((section_number, text))
except ValueError:
continue
except Exception as e:
self.logger.error(f"Error processing document structure: {str(e)}, Type: {type(e)}, Details: {repr(e)}")
return []
return numbered_paragraphs
def _check_heading_sequence(self, current_level: int, previous_level: int) -> Optional[str]:
"""
Check if heading sequence is valid.
Returns error message if invalid, None if valid.
Rules:
- Can go from any level to H1 or H2 (restart numbering)
- When going deeper, can only go one level at a time (e.g., H1 to H2, H2 to H3)
- Can freely go to any higher level (e.g., H3 to H1, H4 to H2)
"""
# When going to a deeper level, only allow one level at a time
if current_level > previous_level:
if current_level != previous_level + 1:
return f"Skipped heading level(s) {previous_level + 1} - Found H{current_level} after H{previous_level}. Add H{previous_level + 1} before this section."
# All other cases are valid:
# - Going to H1 (restart numbering)
# - Going to any higher level (e.g., H3 to H1)
return None
def _check_heading_structure(self, doc: Document) -> List[Dict[str, str]]:
"""Check document heading structure."""
issues = []
previous_level = 0
previous_heading = ""
for para in doc.paragraphs:
if para.style.name.startswith('Heading'):
try:
current_level = int(para.style.name.replace('Heading ', ''))
# Check sequence
error = self._check_heading_sequence(current_level, previous_level)
if error:
issues.append({
'category': '508_compliance_heading_structure',
'message': error,
'context': f"'{para.text}'",
'recommendation': f"Ensure heading levels follow a logical sequence."
})
previous_level = current_level
previous_heading = para.text
except ValueError:
continue
return issues
@profile_performance
def check_readability(self, doc: List[str]) -> DocumentCheckResult:
"""
Check document readability using multiple metrics and plain language standards.
Args:
doc (List[str]): List of document paragraphs
Returns:
DocumentCheckResult: Results including readability scores and identified issues
"""
if not self.validate_input(doc):
return DocumentCheckResult(success=False, issues=[{'error': 'Invalid document input'}])
issues = []
text_stats = {
'total_words': 0,
'total_syllables': 0,
'total_sentences': 0,
'complex_words': 0,
'passive_voice_count': 0
}
# Patterns for identifying passive voice
passive_patterns = [
r'\b(?:am|is|are|was|were|be|been|being)\s+\w+ed\b',
r'\b(?:am|is|are|was|were|be|been|being)\s+\w+en\b',
r'\b(?:has|have|had)\s+been\s+\w+ed\b',
r'\b(?:has|have|had)\s+been\s+\w+en\b'
]
passive_regex = re.compile('|'.join(passive_patterns), re.IGNORECASE)
def count_syllables(word: str) -> int:
"""Count syllables in a word using basic rules."""
word = word.lower()
count = 0
vowels = 'aeiouy'
on_vowel = False
for char in word:
is_vowel = char in vowels
if is_vowel and not on_vowel:
count += 1
on_vowel = is_vowel
if word.endswith('e'):
count -= 1
if word.endswith('le') and len(word) > 2 and word[-3] not in vowels:
count += 1
if count == 0:
count = 1
return count
# Process each paragraph
for paragraph in doc:
if not paragraph.strip():
continue
# Split into sentences
sentences = re.split(r'(?<=[.!?])\s+', paragraph.strip())
text_stats['total_sentences'] += len(sentences)
# Check each sentence
for sentence in sentences:
# Count passive voice instances
if passive_regex.search(sentence):
text_stats['passive_voice_count'] += 1
# Process words
words = sentence.split()
text_stats['total_words'] += len(words)
for word in words:
word = re.sub(r'[^\w\s]', '', word.lower())
if not word:
continue
syllables = count_syllables(word)
text_stats['total_syllables'] += syllables
if syllables >= 3:
text_stats['complex_words'] += 1
# Calculate readability metrics
try:
# Flesch Reading Ease
flesch_ease = 206.835 - 1.015 * (text_stats['total_words'] / text_stats['total_sentences']) - 84.6 * (text_stats['total_syllables'] / text_stats['total_words'])
# Flesch-Kincaid Grade Level
flesch_grade = 0.39 * (text_stats['total_words'] / text_stats['total_sentences']) + 11.8 * (text_stats['total_syllables'] / text_stats['total_words']) - 15.59
# Gunning Fog Index
fog_index = 0.4 * ((text_stats['total_words'] / text_stats['total_sentences']) + 100 * (text_stats['complex_words'] / text_stats['total_words']))
# Calculate passive voice percentage
passive_percentage = (text_stats['passive_voice_count'] / text_stats['total_sentences']) * 100 if text_stats['total_sentences'] > 0 else 0
# Add readability summary with high-level guidance and specific issues
issues = []
if flesch_ease < 50:
issues.append({
'type': 'readability_score',
'metric': 'Flesch Reading Ease',
'score': round(flesch_ease, 1),
'message': 'Document may be too difficult for general audience. Consider simplifying language.'
})
if flesch_grade > 12:
issues.append({
'type': 'readability_score',
'metric': 'Flesch-Kincaid Grade Level',
'score': round(flesch_grade, 1),
'message': 'Reading level is above 12th grade. Consider simplifying for broader accessibility.'
})
if fog_index > 12:
issues.append({
'type': 'readability_score',
'metric': 'Gunning Fog Index',
'score': round(fog_index, 1),
'message': 'Text complexity may be too high. Consider using simpler words and shorter sentences.'
})
if passive_percentage > 10:
issues.append({
'type': 'passive_voice',
'percentage': round(passive_percentage, 1),
'message': f'Document uses {round(passive_percentage, 1)}% passive voice (target: less than 10%). Consider using more active voice.'
})
details = {
'metrics': {
'flesch_reading_ease': round(flesch_ease, 1),
'flesch_kincaid_grade': round(flesch_grade, 1),
'gunning_fog_index': round(fog_index, 1),
'passive_voice_percentage': round(passive_percentage, 1)
}
}
return DocumentCheckResult(
success=len(issues) == 0,
issues=issues,
details=details
)
except Exception as e:
self.logger.error(f"Error calculating readability metrics: {str(e)}")
return DocumentCheckResult(
success=False,
issues=[{'error': f'Error calculating readability metrics: {str(e)}'}]
)
class DocumentCheckResultsFormatter:
def __init__(self):
init() # Initialize colorama
# Enhanced issue categories with examples and specific fixes
self.issue_categories = {
'heading_title_check': {
'title': 'Required Headings Check',
'description': 'Verifies that your document includes all mandatory section headings, with requirements varying by document type. For example, long-template Advisory Circulars require headings like "Purpose." and "Applicability." with initial caps and periods, while Federal Register Notices use ALL CAPS headings like "SUMMARY" and "BACKGROUND" without periods. This check ensures both the presence of required headings and their correct capitalization format based on document type.',
'solution': 'Add all required headings in the correct order using the correct capitalization format.',
'example_fix': {
'before': 'Missing required heading "PURPOSE."',
'after': 'Added heading "PURPOSE." at the beginning of the document'
}
},
'heading_title_period_check': {
'title': 'Heading Period Format',
'description': 'Examines heading punctuation to ensure compliance with FAA document formatting standards. Some FAA documents (like Advisory Circulars and Orders) require periods at the end of headings, while others (like Federal Register Notices) don\'t.',
'solution': 'Format heading periods according to document type requirements.',
'example_fix': {
'before': 'Purpose',
'after': 'Purpose.' # For ACs and Orders
}
},
'table_figure_reference_check': {
'title': 'Table and Figure References',
'description': 'Analyzes how tables and figures are referenced within your document text. Capitalize references at the beginning of sentences (e.g., "Table 2-1 shows...") and use lowercase references within sentences (e.g., "...as shown in table 2-1").',
'solution': 'Capitalize references at start of sentences, use lowercase within sentences.',
'example_fix': {
'before': 'The DTR values are specified in Table 3-1 and Figure 3-2.',
'after': 'The DTR values are specified in table 3-1 and figure 3-2.'
}
},
'acronym_check': {
'title': 'Acronym Definition Issues',
'description': 'Ensures every acronym is properly introduced with its full term at first use. The check identifies undefined acronyms while recognizing common exceptions (like U.S.) that don\'t require definition.',
'solution': 'Define each acronym at its first use, e.g., "Federal Aviation Administration (FAA)".',
'example_fix': {
'before': 'This order establishes general FAA organizational policies.',
'after': 'This order establishes general Federal Aviation Administration (FAA) organizational policies.'
}
},
'acronym_usage_check': {
'title': 'Unused Acronym Definitions',
'description': 'Ensures that all acronyms defined in the document are actually used later. If an acronym is defined but never referenced, the definition should be removed to avoid confusion or unnecessary clutter.',
'solution': 'Identify acronyms that are defined but not used later in the document and remove their definitions.',
'example_fix': {
'before': 'Operators must comply with airworthiness directives (AD) to ensure aircraft safety and regulatory compliance.',
'after': 'Operators must comply with airworthiness directives to ensure aircraft safety and regulatory compliance.'
}
},
'terminology_check': {
'title': 'Incorrect Terminology',
'description': 'Evaluates document text against the various style manuals and orders to identify non-compliant terminology, ambiguous references, and outdated phrases. This includes checking for prohibited relative references (like "above" or "below"), proper legal terminology (like "must" instead of "shall"), and consistent formatting of regulatory citations.',
'solution': 'Use explicit references to paragraphs, sections, tables, and figures.',
'example_fix': {
'before': 'Operators shall comply with ADs to ensure aircraft safety and regulatory compliance',
'after': 'Operators must comply with ADs to ensure aircraft safety and regulatory compliance.'
}
},
'section_symbol_usage_check': {
'title': 'Section Symbol (Β§) Format Issues',
'description': 'Examines the usage of section symbols (Β§) throughout your document. This includes verifying proper symbol placement in regulatory references, ensuring sections aren\'t started with the symbol, checking consistency in multiple-section citations, and validating proper CFR citations. For ACs, see FAA Order 1320.46.',
'solution': 'Format section symbols correctly and never start sentences with them.',
'example_fix': {
'before': 'Β§ 23.3 establishes design criteria.',
'after': 'Section 23.3 establishes design criteria.'
}
},
'double_period_check': {
'title': 'Multiple Period Issues',
'description': 'Examines sentences for accidental double periods that often occur during document editing and revision. While double periods are sometimes found in ellipses (...) or web addresses, they should never appear at the end of standard sentences in FAA documentation.',
'solution': 'Remove multiple periods that end sentences.',
'example_fix': {
'before': 'The following ACs are related to the guidance in this document..',
'after': 'The following ACs are related to the guidance in this document.'
}
},
'spacing_check': {
'title': 'Spacing Issues',
'description': 'Analyzes document spacing patterns to ensure compliance with FAA formatting standards. This includes checking for proper spacing around regulatory references (like "AC 25-1" not "AC25-1"), section symbols (Β§ 25.1), paragraph references, and multiple spaces between words.',
'solution': 'Fix spacing issues: remove any missing spaces, double spaces, or inadvertent tabs.',
'example_fix': {
'before': 'AC25.25 states that SFAR88 and Β§25.981 require...',
'after': 'AC 25.25 states that SFAR 88 and Β§ 25.981 require...'
}
},
'date_formats_check': {
'title': 'Date Format Issues',
'description': 'Examines all date references in your document. The check automatically excludes technical reference numbers that may look like dates to ensure accurate validation of true date references. Note, though, there might be instances in the heading of the document where the date is formatted as "MM/DD/YYYY", which is acceptable. This applies mostly to date formats within the document body.',
'solution': 'Use the format "Month Day, Year" where appropriate.',
'example_fix': {
'before': 'This policy statement cancels Policy Statement PS-AIR100-2006-MMPDS, dated 7/25/2006.',
'after': 'This policy statement cancels Policy Statement PS-AIR100-2006-MMPDS, dated July 25, 2006.'
}
},
'placeholders_check': {
'title': 'Placeholder Content',
'description': 'Identifies incomplete content and temporary placeholders that must be finalized before document publication. This includes common placeholder text (like "TBD" or "To be determined"), draft markers, and incomplete sections.',
'solution': 'Replace all placeholder content with actual content.',
'example_fix': {
'before': 'Pilots must submit the [Insert text] form to the FAA for approval.',
'after': 'Pilots must submit the Report of Eye Evaluation form 8500-7 to the FAA for approval.'
}
},
'parentheses_check': {
'title': 'Parentheses Balance Check',
'description': 'Ensures that all parentheses in the document are properly paired with matching opening and closing characters.',
'solution': 'Add missing opening or closing parentheses where indicated.',
'example_fix': {
'before': 'The system (as defined in AC 25-11B performs...',
'after': 'The system (as defined in AC 25-11B) performs...'
}
},
'paragraph_length_check': {
'title': 'Paragraph Length Issues',
'description': 'Flags paragraphs exceeding 6 sentences or 8 lines to enhance readability and clarity. While concise paragraphs are encouraged, with each focusing on a single idea or related points, exceeding these limits doesn\'t necessarily indicate a problem. Some content may appropriately extend beyond 8 lines, especially if it includes necessary details. Boilerplate language or template text exceeding these limits is not subject to modification or division.',
'solution': 'Where possible, split long paragraphs into smaller sections, ensuring each focuses on one primary idea. If restructuring is not feasible or the content is boilerplate text, no changes are needed.',
'example_fix': {
'before': 'A very long paragraph covering multiple topics and spanning many lines...',
'after': 'Multiple shorter paragraphs or restructured paragraphs, each focused on a single topic or related points.'
}
},
'sentence_length_check': {
'title': 'Sentence Length Issues',
'description': 'Analyzes sentence length to ensure readability. While the ideal length varies with content complexity, sentences over 35 words often become difficult to follow. Technical content, regulatory references, notes, warnings, and list items are excluded from this check.',
'solution': 'Break long sentences into smaller ones where possible, focusing on one main point per sentence. Consider using lists for complex items.',
'example_fix': {
'before': 'The operator must ensure that all required maintenance procedures are performed in accordance with the manufacturer\'s specifications and that proper documentation is maintained throughout the entire process to demonstrate compliance with regulatory requirements.',
'after': 'The operator must ensure all required maintenance procedures are performed according to manufacturer specifications. Additionally, proper documentation must be maintained to demonstrate regulatory compliance.'
}
},
'document_title_check': {
'title': 'Referenced Document Title Format Issues',
'description': 'Checks document title formatting based on document type. Advisory Circulars require italics without quotes, while all other document types require quotes without italics.',
'solution': 'Format document titles according to document type: use italics for Advisory Circulars, quotes for all other document types.',
'example_fix': {
'before': 'See AC 25.1309-1B, System Design and Analysis, for information on X.',
'after': 'See AC 25.1309-1B, <i>System Design and Analysis</i>, for information on X.'
}
},
'508_compliance_check': {
'title': 'Section 508 Compliance Issues',
'description': 'Checks document accessibility features required by Section 508 standards: Image alt text for screen readers, heading structure issues (missing heading 1, skipped heading levels, and out of sequence headings), and hyperlink accessibility (ensuring links have meaningful descriptive text).',
'solution': 'Address each accessibility issue: add image alt text for screen readers, fix heading structure, and ensure hyperlinks have descriptive text that indicates their destination.',
'example_fix': {
'before': [
'Image without alt text',
'Heading sequence: H1 β H2 β H4 (skipped H3)',
'Link text: "click here" or "www.example.com"'
],
'after': [
'Image with descriptive alt text',
'Proper heading sequence: H1 β H2 β H3 β H4',
'Descriptive link text: "FAA Compliance Guidelines" or "Download the Safety Report"'
]
}
},
'hyperlink_check': {
'title': 'Hyperlink Issues',
'description': 'Checks for potentially broken or inaccessible URLs in the document. This includes checking response codes and connection issues.',
'solution': 'Verify each flagged URL is correct and accessible.',
'example_fix': {
'before': 'See https://broken-link.example.com for more details.',
'after': 'See https://www.faa.gov for more details.'
}
},
'cross_references_check': {
'title': 'Cross-Reference Issues',
'description': 'Checks for missing or invalid cross-references to paragraphs, tables, figures, and appendices within the document.',
'solution': 'Ensure that all referenced elements are present in the document and update or remove any incorrect references.',
'example_fix': {
'before': 'See table 5-2 for more information. (there is no table 5-2)',
'after': 'Either update the table reference or add table 5-2 if missing'
}
},
'readability_check': {
'title': 'Readability Issues',
'description': 'Analyzes document readability using multiple metrics including Flesch Reading Ease, Flesch-Kincaid Grade Level, and Gunning Fog Index. Also checks for passive voice usage and technical jargon.',
'solution': 'Simplify language, reduce passive voice, and replace technical jargon with plain language alternatives.',
'example_fix': {
'before': 'The implementation of the procedure was facilitated by technical personnel.',
'after': 'Technical staff helped start the procedure.'
}
}
}
# Add these two helper methods here, after __init__ and before other methods
def _format_colored_text(self, text: str, color: str) -> str:
"""Helper method to format colored text with reset.
Args:
text: The text to be colored
color: The color to apply (from colorama.Fore)
Returns:
str: The colored text with reset styling
"""
return f"{color}{text}{Style.RESET_ALL}"
def _format_example(self, example_fix: Dict[str, str]) -> List[str]:
"""Format example fixes consistently.
Args:
example_fix: Dictionary containing 'before' and 'after' examples
Returns:
List[str]: Formatted example lines
"""
return [
f" β Incorrect: {example_fix['before']}",
f" β Correct: {example_fix['after']}"
]
def _format_heading_issues(self, result: DocumentCheckResult, doc_type: str) -> List[str]:
"""Format heading check issues consistently."""
output = []
for issue in result.issues:
if issue.get('type') == 'missing_headings':
missing = sorted(issue['missing'])
output.append(f"\n Missing Required Headings for {doc_type}:")
for heading in missing:
output.append(f" β’ {heading}")
elif issue.get('type') == 'unexpected_headings':
unexpected = sorted(issue['unexpected'])
output.append(f"\n Unexpected Headings Found:")
for heading in unexpected:
output.append(f" β’ {heading}")
return output
def _format_period_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format period check issues consistently."""
output = []
if result.issues:
output.append(f"\n Heading Period Format Issues:")
for issue in result.issues:
if 'message' in issue:
output.append(f" β’ {issue['message']}")
return output
def _format_caption_issues(self, issues: List[Dict], doc_type: str) -> List[str]:
"""Format caption check issues with clear replacement instructions."""
formatted_issues = []
for issue in issues:
if 'incorrect_caption' in issue:
caption_parts = issue['incorrect_caption'].split()
if len(caption_parts) >= 2:
caption_type = caption_parts[0] # "Table" or "Figure"
number = caption_parts[1]
# Determine correct format based on document type
if doc_type in ["Advisory Circular", "Order"]:
if '-' not in number:
correct_format = f"{caption_type} {number}-1"
else:
if '-' in number:
correct_format = f"{caption_type} {number.split('-')[0]}"
else:
correct_format = issue['incorrect_caption']
formatted_issues.append(
f" β’ Replace '{issue['incorrect_caption']}' with '{correct_format}'"
)
return formatted_issues
def _format_reference_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format reference issues with clear, concise descriptions."""
formatted_issues = []
for issue in result.issues:
ref_type = issue.get('type', '')
ref_num = issue.get('reference', '')
context = issue.get('context', '').strip()
if context: # Only include context if it exists
formatted_issues.append(
f" β’ Confirm {ref_type} {ref_num} referenced in '{context}' exists in the document"
)
else:
formatted_issues.append(
f" β’ Confirm {ref_type} {ref_num} exists in the document"
)
return formatted_issues
def _format_standard_issue(self, issue: Dict[str, Any]) -> str:
"""Format standard issues consistently."""
if isinstance(issue, str):
return f" β’ {issue}"
if 'incorrect' in issue and 'correct' in issue:
return f" β’ Replace '{issue['incorrect']}' with '{issue['correct']}'"
if 'incorrect_term' in issue and 'correct_term' in issue:
return f" β’ Replace '{issue['incorrect_term']}' with '{issue['correct_term']}'"
if 'sentence' in issue and 'word_count' in issue: # For sentence length check
return f" β’ Review this sentence: \"{issue['sentence']}\""
if 'sentence' in issue:
return f" β’ {issue['sentence']}"
if 'description' in issue:
return f" β’ {issue['description']}"
if 'type' in issue and issue['type'] == 'long_paragraph':
return f" β’ {issue['message']}"
# Fallback for other issue formats
return f" β’ {str(issue)}"
def _format_unused_acronym_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format unused acronym issues with a simple, clear message.
Args:
result: DocumentCheckResult containing acronym issues
Returns:
List[str]: Formatted list of unused acronym issues
"""
formatted_issues = []
if result.issues:
for issue in result.issues:
if isinstance(issue, dict) and 'acronym' in issue:
formatted_issues.append(f" β’ Acronym '{issue['acronym']}' was defined but never used.")
elif isinstance(issue, str):
# Handle case where issue might be just the acronym
formatted_issues.append(f" β’ Acronym '{issue}' was defined but never used.")
return formatted_issues
def _format_parentheses_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format parentheses issues with clear instructions for fixing."""
formatted_issues = []
if result.issues:
for issue in result.issues:
formatted_issues.append(f" β’ {issue['message']}")
return formatted_issues
def _format_section_symbol_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format section symbol issues with clear replacement instructions."""
formatted_issues = []
if result.issues:
for issue in result.issues:
if 'incorrect' in issue and 'correct' in issue:
if issue.get('is_sentence_start'):
formatted_issues.append(
f" β’ Do not begin sentences with the section symbol. "
f"Replace '{issue['incorrect']}' with '{issue['correct']}' at the start of the sentence"
)
else:
formatted_issues.append(
f" β’ Replace '{issue['incorrect']}' with '{issue['correct']}'"
)
return formatted_issues
def _format_paragraph_length_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format paragraph length issues with clear instructions for fixing.
Args:
result: DocumentCheckResult containing paragraph length issues
Returns:
List[str]: Formatted list of paragraph length issues
"""
formatted_issues = []
if result.issues:
for issue in result.issues:
if isinstance(issue, str):
formatted_issues.append(f" β’ {issue}")
elif isinstance(issue, dict) and 'message' in issue:
formatted_issues.append(f" β’ {issue['message']}")
else:
# Fallback for unexpected issue format
formatted_issues.append(f" β’ Review paragraph for length issues: {str(issue)}")
return formatted_issues
def format_results(self, results: Dict[str, Any], doc_type: str) -> str:
"""
Format check results into a detailed, user-friendly report.
Args:
results: Dictionary of check results
doc_type: Type of document being checked
Returns:
str: Formatted report with consistent styling
"""
# Determine caption format based on document type
if doc_type in ["Advisory Circular", "Order"]:
table_format = {
'title': 'Table Caption Format Issues',
'description': 'Analyzes table captions to ensure they follow the FAA\'s dual-numbering system, where tables must be numbered according to their chapter or appendix location (X-Y format). The first number (X) indicates the chapter number, while the second number (Y) provides sequential numbering within that chapter. For more information, see FAA Order 1320.46.',
'solution': 'Use the format "Table X-Y" where X is the chapter or appendix number and Y is the sequence number',
'example_fix': {
'before': 'Table 5. | Table A.',
'after': 'Table 5-1. | Table A-1.'
}
}
figure_format = {
'title': 'Figure Caption Format Issues',
'description': 'Analyzes figure captions to ensure they follow the FAA\'s dual-numbering system, where figures must be numbered according to their chapter or appendix location (X-Y format). The first number (X) indicates the chapter number, while the second number (Y) provides sequential numbering within that chapter. For more information, see FAA Order 1320.46.',
'solution': 'Use the format "Figure X-Y" where X is the chapter or appendix number and Y is the sequence number',
'example_fix': {
'before': 'Figure 5. | Figure A.',
'after': 'Figure 5-1. | Figure A-1.'
}
}
else:
table_format = {
'title': 'Table Caption Format Issues',
'description': f'Analyzes table captions to ensure they follow the FAA\'s sequential numbering system for {doc_type}s. Tables must be numbered consecutively throughout the document using a single number format.',
'solution': 'Use the format "Table X" where X is a sequential number',
'example_fix': {
'before': 'Table 5-1. | Table A-1',
'after': 'Table 5. | Table 1.'
}
}
figure_format = {
'title': 'Figure Caption Format Issues',
'description': f'Analyzes figure captions to ensure they follow the FAA\'s sequential numbering system for {doc_type}s. Figures must be numbered consecutively throughout the document using a single number format.',
'solution': 'Use the format "Figure X" where X is a sequential number',
'example_fix': {
'before': 'Figure 5-1. | Figure A-1.',
'after': 'Figure 5. | Figure 3.'
}
}
# Update the issue categories with the correct format
self.issue_categories['caption_check_table'] = table_format
self.issue_categories['caption_check_figure'] = figure_format
# Define formatting rules for different document types
formatting_rules = {
"italics_only": {
"types": ["Advisory Circular"],
"italics": True,
"quotes": False,
"description": "For Advisory Circulars, referenced document titles should be italicized but not quoted. For more information, see FAA Order 1320.46.",
"example": "See AC 25.1309-1B, <i>System Design and Analysis</i>, for information on X."
},
"quotes_only": {
"types": [
"Airworthiness Criteria", "Deviation Memo", "Exemption",
"Federal Register Notice", "Order", "Policy Statement", "Rule",
"Special Condition", "Technical Standard Order", "Other"
],
"italics": False,
"quotes": True,
"description": "For this document type, referenced document titles should be in quotes without italics.",
"example": 'See AC 25.1309-1B, "System Design and Analysis," for information on X.'
}
}
# Find the formatting group for the current document type
format_group = None
for group, rules in formatting_rules.items():
if doc_type in rules["types"]:
format_group = rules
break
# Use quotes_only as default if document type not found
if not format_group:
format_group = formatting_rules["quotes_only"]
# Update document title check category based on document type
if doc_type == "Advisory Circular":
self.issue_categories['document_title_check'] = {
'title': 'Referenced Document Title Format Issues',
'description': 'For Advisory Circulars, all referenced document titles must be italicized.',
'solution': 'Format document titles using italics for Advisory Circulars.',
'example_fix': {
'before': 'See AC 25.1309-1B, System Design and Analysis, for information on X.',
'after': 'See AC 25.1309-1B, <i>System Design and Analysis</i>, for information on X.'
}
}
else:
self.issue_categories['document_title_check'] = {
'title': 'Referenced Document Title Format Issues',
'description': f'For {doc_type}s, all referenced document titles must be enclosed in quotation marks.',
'solution': 'Format document titles using quotation marks.',
'example_fix': {
'before': 'See AC 25.1309-1B, System Design and Analysis, for information on X.',
'after': 'See AC 25.1309-1B, "System Design and Analysis," for information on X.'
}
}
output = []
self.issue_categories['acronym_usage_check'] = {
'title': 'Unused Acronym Definitions',
'description': 'Ensures that all acronyms defined in the document are actually used later. If an acronym is defined but never referenced, the definition should be removed to avoid confusion or unnecessary clutter.',
'solution': 'Identify acronyms that are defined but not used later in the document and remove their definitions.',
'example_fix': {
'before': 'Operators must comply with airworthiness directives (AD) to ensure aircraft safety and regulatory compliance.',
'after': 'Operators must comply with airworthiness directives to ensure aircraft safety and regulatory compliance.'
}
}
output = []
# Header
output.append(f"\n{Fore.CYAN}{'='*80}")
output.append(f"Document Check Results Summary")
output.append(f"{'='*80}{Style.RESET_ALL}\n")
# Count total issues
total_issues = sum(1 for r in results.values() if not r.success)
if total_issues == 0:
output.append(f"{self._format_colored_text('β All checks passed successfully!', Fore.GREEN)}\n")
return '\n'.join(output)
output.append(f"{Fore.YELLOW}Found {total_issues} categories of issues that need attention:{Style.RESET_ALL}\n")
# Process all check results consistently
for check_name, result in results.items():
if not result.success and check_name in self.issue_categories:
category = self.issue_categories[check_name]
output.append("\n")
output.append(self._format_colored_text(f"β {category['title']}", Fore.YELLOW))
output.append(f" {category['description']}")
output.append(f" {self._format_colored_text('How to fix: ' + category['solution'], Fore.GREEN)}")
output.append(f"\n {self._format_colored_text('Example Fix:', Fore.CYAN)}")
output.extend(self._format_example(category['example_fix']))
output.append("")
output.append(f" {self._format_colored_text('Issues found in your document:', Fore.CYAN)}")
# Special handling for date format issues
if check_name == 'date_formats_check':
for issue in result.issues:
output.append(f" β’ Replace '{issue['incorrect']}' with '{issue['correct']}'")
# Handle other check types
elif check_name == 'heading_title_check':
output.extend(self._format_heading_issues(result, doc_type))
elif check_name == 'heading_title_period_check':
output.extend(self._format_period_issues(result))
elif check_name == 'table_figure_reference_check':
output.extend(self._format_reference_issues(result))
elif check_name in ['caption_check_table', 'caption_check_figure']:
output.extend(self._format_caption_issues(result.issues, doc_type))
elif check_name == 'acronym_usage_check':
output.extend(self._format_unused_acronym_issues(result))
elif check_name == 'section_symbol_usage_check':
output.extend(self._format_section_symbol_issues(result))
elif check_name == 'parentheses_check':
output.extend(self._format_parentheses_issues(result))
elif check_name == '508_compliance_check':
if not result.success:
# Combine all 508 compliance issues into a single list
for issue in result.issues:
if issue.get('category') == '508_compliance_heading_structure':
output.append(f" β’ {issue['message']}")
if 'context' in issue:
output.append(f" Context: {issue['context']}")
if 'recommendation' in issue:
output.append(f" Recommendation: {issue['recommendation']}")
elif issue.get('category') == 'image_alt_text':
if 'context' in issue:
output.append(f" β’ {issue['context']}")
elif issue.get('category') == 'hyperlink_accessibility':
output.append(f" β’ {issue.get('user_message', issue.get('message', 'No description provided'))}")
elif check_name == 'hyperlink_check':
for issue in result.issues:
output.append(f" β’ {issue['message']}")
if 'status_code' in issue:
output.append(f" (HTTP Status: {issue['status_code']})")
elif 'error' in issue:
output.append(f" (Error: {issue['error']})")
elif check_name == 'cross_references_check':
for issue in result.issues:
output.append(f" β’ Confirm {issue['type']} {issue['reference']} referenced in '{issue['context']}' exists in the document")
elif check_name == 'readability_check':
output.extend(self._format_readability_issues(result))
else:
formatted_issues = [self._format_standard_issue(issue) for issue in result.issues[:15]]
output.extend(formatted_issues)
if len(result.issues) > 30:
output.append(f"\n ... and {len(result.issues) - 30} more similar issues.")
return '\n'.join(output)
def save_report(self, results: Dict[str, Any], filepath: str, doc_type: str) -> None:
"""Save the formatted results to a file with proper formatting."""
try:
with open(filepath, 'w', encoding='utf-8') as f:
# Create a report without color codes
report = self.format_results(results, doc_type)
# Strip color codes
for color in [Fore.CYAN, Fore.GREEN, Fore.YELLOW, Fore.RED, Style.RESET_ALL]:
report = report.replace(str(color), '')
# Convert markdown-style italics to alternative formatting for plain text
report = report.replace('*', '_')
f.write(report)
except Exception as e:
print(f"Error saving report: {e}")
def _format_readability_issues(self, result: DocumentCheckResult) -> List[str]:
"""Format readability issues with clear, actionable feedback."""
formatted_issues = []
if result.details and 'metrics' in result.details:
metrics = result.details['metrics']
formatted_issues.append("\n Readability Scores:")
formatted_issues.append(f" β’ Flesch Reading Ease: {metrics['flesch_reading_ease']} (Aim for 50+; higher is easier to read)")
formatted_issues.append(f" β’ Grade Level: {metrics['flesch_kincaid_grade']} (Aim for 10 or lower; 12 acceptable for technical/legal)")
formatted_issues.append(f" β’ Gunning Fog Index: {metrics['gunning_fog_index']} (Aim for 12 or lower)")
formatted_issues.append(f" β’ Passive Voice: {metrics['passive_voice_percentage']}% (Aim for less than 10%; use active voice for clarity)")
if result.issues:
formatted_issues.append("\n Identified Issues:")
for issue in result.issues:
if issue['type'] == 'jargon':
formatted_issues.append(
f" β’ Replace '{issue['word']}' with '{issue['suggestion']}' in: \"{issue['sentence']}\""
)
elif issue['type'] in ['readability_score', 'passive_voice']:
formatted_issues.append(f" β’ {issue['message']}")
return formatted_issues
def format_markdown_results(results: Dict[str, DocumentCheckResult], doc_type: str) -> str:
"""Format check results into a Markdown string for Gradio display."""
output = []
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
output.extend([
f"# Document Check Results - {current_time}",
f"## Document Type: {doc_type}",
"---\n"
])
total_issues = sum(1 for r in results.values() if not r.success)
if total_issues == 0:
output.append("β
**All checks passed successfully!**\n")
return "\n".join(output)
output.append(f"β Found issues in {total_issues} check categories\n")
check_categories = {
'heading_title_check': {'title': 'π Required Headings', 'priority': 1},
'heading_title_period_check': {'title': 'π Heading Period Usage', 'priority': 1},
'terminology_check': {'title': 'π Terminology Usage', 'priority': 1},
'acronym_check': {'title': 'π Acronym Definitions', 'priority': 1},
'acronym_usage_check': {'title': 'π Acronym Usage', 'priority': 1},
'section_symbol_usage_check': {'title': 'Β§ Section Symbol Usage', 'priority': 2},
'508_compliance_check': {'title': 'π΅οΈββοΈ 508 Compliance', 'priority': 2},
'cross_references_check': {'title': 'π Cross References', 'priority': 2},
'hyperlink_check': {'title': 'π Hyperlinks', 'priority': 2},
'date_formats_check': {'title': 'π
Date Formats', 'priority': 2},
'placeholders_check': {'title': 'π© Placeholder Content', 'priority': 2},
'document_title_check': {'title': 'π Document Title Format', 'priority': 2},
'caption_check_table': {'title': 'π Table Captions', 'priority': 3},
'caption_check_figure': {'title': 'πΌοΈ Figure Captions', 'priority': 3},
'table_figure_reference_check': {'title': 'π Table/Figure References', 'priority': 3},
'parentheses_check': {'title': 'π Parentheses Usage', 'priority': 4},
'double_period_check': {'title': 'β‘ Double Periods', 'priority': 4},
'spacing_check': {'title': 'β¨οΈ Spacing Issues', 'priority': 4},
'paragraph_length_check': {'title': 'π Paragraph Length', 'priority': 5},
'sentence_length_check': {'title': 'π Sentence Length', 'priority': 5},
}
sorted_checks = sorted(
[(name, result) for name, result in results.items()],
key=lambda x: check_categories.get(x[0], {'priority': 999})['priority']
)
for check_name, result in sorted_checks:
if not result.success:
category = check_categories.get(check_name, {'title': check_name.replace('_', ' ').title()})
output.append(f"### {category['title']}")
if isinstance(result.issues, list):
for issue in result.issues[:30]:
if isinstance(issue, dict):
for key, value in issue.items():
if isinstance(value, list):
output.extend([f"- {item}" for item in value])
else:
output.append(f"- {key}: {value}")
else:
output.append(f"- {issue}")
if len(result.issues) > 30:
output.append(f"\n*...and {len(result.issues) - 30} more similar issues*")
output.append("")
return "\n".join(output)
def create_interface():
"""Create and configure the Gradio interface."""
document_types = [
"Advisory Circular",
"Airworthiness Criteria",
"Deviation Memo",
"Exemption",
"Federal Register Notice",
"Order",
"Policy Statement",
"Rule",
"Special Condition",
"Technical Standard Order",
"Other"
]
template_types = ["Short AC template AC", "Long AC template AC"]
def format_results_as_html(text_results):
"""Convert the text results into styled HTML."""
if not text_results:
return """
<div class="p-4 text-gray-600">
Results will appear here after processing...
</div>
"""
# Remove ANSI color codes
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
text_results = ansi_escape.sub('', text_results)
# Split into sections while preserving the header
sections = text_results.split('β ')
header = sections[0].strip()
issues = sections[1:]
# Extract the number of issues from the header text
issues_count_match = re.search(r'Found (\d+) categories', header)
issues_count = issues_count_match.group(1) if issues_count_match else len(issues)
# Format header with title
header_html = f"""
<div class="max-w-4xl mx-auto p-4 bg-white rounded-lg shadow-sm mb-6">
<h1 class="text-2xl font-bold mb-4">Document Check Results Summary</h1>
<div class="text-lg text-amber-600">
Found {issues_count} categories of issues that need attention.
</div>
</div>
"""
# Format each issue section
issues_html = ""
for section in issues:
if not section.strip():
continue
parts = section.strip().split('\n', 1)
if len(parts) < 2:
continue
title = parts[0].strip()
content = parts[1].strip()
# Special handling for readability metrics
if "Readability Issues" in title:
metrics_match = re.search(r'Readability Scores:(.*?)(?=Identified Issues:|$)', content, re.DOTALL)
issues_match = re.search(r'Identified Issues:(.*?)(?=\Z)', content, re.DOTALL)
metrics_html = ""
if metrics_match:
metrics = metrics_match.group(1).strip().split('\n')
metrics_html = """
<div class="bg-blue-50 rounded-lg p-4 mb-4">
<h3 class="font-medium text-blue-800 mb-2">π Readability Metrics</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
"""
for metric in metrics:
if metric.strip():
label, value = metric.strip('β’ ').split(':', 1)
metrics_html += f"""
<div class="flex flex-col">
<span class="text-sm text-blue-600 font-medium">{label}</span>
<span class="text-lg text-blue-900">{value}</span>
</div>
"""
metrics_html += "</div></div>"
issues_html_section = ""
if issues_match:
issues_list = issues_match.group(1).strip().split('\n')
if issues_list:
issues_html_section = """
<div class="mt-4">
<h3 class="font-medium text-gray-800 mb-2">π Identified Issues:</h3>
<ul class="list-none space-y-2">
"""
for issue in issues_list:
if issue.strip():
issues_html_section += f"""
<li class="text-gray-600 ml-4">β’ {issue.strip('β’ ')}</li>
"""
issues_html_section += "</ul></div>"
# Combine the readability section
issues_html += f"""
<div class="bg-white rounded-lg shadow-sm mb-6 overflow-hidden">
<div class="bg-gray-50 px-6 py-4 border-b">
<h2 class="text-lg font-semibold text-gray-800">{title}</h2>
</div>
<div class="px-6 py-4">
{metrics_html}
{issues_html_section}
</div>
</div>
"""
continue
# Extract description and solution
description_parts = content.split('How to fix:', 1)
description = description_parts[0].strip()
solution = description_parts[1].split('Example Fix:', 1)[0].strip() if len(description_parts) > 1 else ""
# Extract examples and issues
examples_match = re.search(r'Example Fix:\s*β[^β]+β[^β’]+', content, re.MULTILINE | re.DOTALL)
examples_html = ""
if examples_match:
examples_text = examples_match.group(0)
incorrect = re.search(r'β\s*Incorrect:\s*([^β]+)', examples_text)
correct = re.search(r'β\s*Correct:\s*([^β’\n]+)', examples_text)
if incorrect and correct:
examples_html = f"""
<div class="mb-4">
<h3 class="font-medium text-gray-800 mb-2">Example Fix:</h3>
<div class="space-y-2 ml-4">
<div class="text-red-600">
β Incorrect:
</div>
<div class="text-red-600 ml-8">
{incorrect.group(1).strip()}
</div>
<div class="text-green-600 mt-2">
β Correct:
</div>
<div class="text-green-600 ml-8">
{correct.group(1).strip()}
</div>
</div>
</div>
"""
# Extract issues
issues_match = re.findall(r'β’\s*(.*?)(?=β’|\Z)', content, re.DOTALL)
issues_html_section = ""
if issues_match:
issues_html_section = """
<div class="mt-4">
<h3 class="font-medium text-gray-800 mb-2">Issues found in your document:</h3>
<ul class="list-none space-y-2">
"""
for issue in issues_match[:30]:
clean_issue = issue.strip().lstrip('β’').strip()
issues_html_section += f"""
<li class="text-gray-600 ml-4">β’ {clean_issue}</li>
"""
if len(issues_match) > 30:
issues_html_section += f"""
<li class="text-gray-500 italic ml-4">... and {len(issues_match) - 30} more similar issues.</li>
"""
issues_html_section += "</ul></div>"
# Combine the section
issues_html += f"""
<div class="bg-white rounded-lg shadow-sm mb-6 overflow-hidden">
<div class="bg-gray-50 px-6 py-4 border-b">
<h2 class="text-lg font-semibold text-gray-800">{title}</h2>
</div>
<div class="px-6 py-4">
<div class="text-gray-600 mb-4">
{description}
</div>
<div class="bg-green-50 rounded p-4 mb-4">
<div class="text-green-800">
<span class="font-medium">How to fix: </span>
{solution}
</div>
</div>
{examples_html}
{issues_html_section}
</div>
</div>
"""
# Add new CSS classes for readability metrics
additional_styles = """
.bg-blue-50 { background-color: #eff6ff; }
.text-blue-600 { color: #2563eb; }
.text-blue-800 { color: #1e40af; }
.text-blue-900 { color: #1e3a8a; }
.grid { display: grid; }
.grid-cols-1 { grid-template-columns: repeat(1, minmax(0, 1fr)); }
.gap-4 { gap: 1rem; }
@media (min-width: 768px) {
.md\\:grid-cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
"""
# Add summary section before the final return
summary_html = f"""
<div class="bg-white rounded-lg shadow-sm mb-6 overflow-hidden">
<div class="bg-gray-50 px-6 py-4 border-b">
<h2 class="text-lg font-semibold text-gray-800">π Next Steps</h2>
</div>
<div class="px-6 py-4">
<div class="space-y-4">
<div>
<h3 class="font-medium text-gray-800 mb-2">1. Review and Address Issues:</h3>
<ul class="list-none space-y-2 ml-4">
<li class="text-gray-600">β’ Review each issue category systematically</li>
<li class="text-gray-600">β’ Make corrections using the provided examples as guides</li>
<li class="text-gray-600">β’ Re-run document check to verify all issues are resolved</li>
</ul>
</div>
<div>
<h3 class="font-medium text-gray-800 mb-2">2. Best Practices:</h3>
<ul class="list-none space-y-2 ml-4">
<li class="text-gray-600">β’ Use search/replace for consistent fixes</li>
<li class="text-gray-600">β’ Update your document template to prevent future issues</li>
<li class="text-gray-600">β’ Keep the style manuals and Orders handy while making corrections</li>
</ul>
</div>
</div>
</div>
</div>
"""
# Update the final HTML to include the summary section
full_html = f"""
<div class="mx-auto p-4" style="font-family: system-ui, -apple-system, sans-serif;">
<style>
.text-2xl {{ font-size: 1.5rem; line-height: 2rem; }}
.text-lg {{ font-size: 1.125rem; }}
.text-sm {{ font-size: 0.875rem; }}
.font-bold {{ font-weight: 700; }}
.font-semibold {{ font-weight: 600; }}
.font-medium {{ font-weight: 500; }}
.text-gray-800 {{ color: #1f2937; }}
.text-gray-600 {{ color: #4b5563; }}
.text-gray-500 {{ color: #6b7280; }}
.text-green-600 {{ color: #059669; }}
.text-green-800 {{ color: #065f46; }}
.text-red-600 {{ color: #dc2626; }}
.text-amber-600 {{ color: #d97706; }}
.bg-white {{ background-color: #ffffff; }}
.bg-gray-50 {{ background-color: #f9fafb; }}
.bg-green-50 {{ background-color: #ecfdf5; }}
.rounded-lg {{ border-radius: 0.5rem; }}
.shadow-sm {{ box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05); }}
.mb-6 {{ margin-bottom: 1.5rem; }}
.mb-4 {{ margin-bottom: 1rem; }}
.mb-2 {{ margin-bottom: 0.5rem; }}
.ml-4 {{ margin-left: 1rem; }}
.ml-8 {{ margin-left: 2rem; }}
.mt-2 {{ margin-top: 0.5rem; }}
.mt-4 {{ margin-top: 1rem; }}
.p-4 {{ padding: 1rem; }}
.px-6 {{ padding-left: 1.5rem; padding-right: 1.5rem; }}
.py-4 {{ padding-top: 1rem; padding-bottom: 1rem; }}
.space-y-2 > * + * {{ margin-top: 0.5rem; }}
.italic {{ font-style: italic; }}
.border-b {{ border-bottom: 1px solid #e5e7eb; }}
.overflow-hidden {{ overflow: hidden; }}
.list-none {{ list-style-type: none; }}
.space-y-4 > * + * {{ margin-top: 1rem; }}
{additional_styles}
</style>
{header_html}
{issues_html}
{summary_html}
</div>
"""
return full_html
# Define the function to read the README content
def get_readme_content():
readme_path = "README.md"
try:
with open(readme_path, "r", encoding="utf-8") as file:
readme_content = file.read()
return readme_content
except Exception as e:
logging.error(f"Error reading README.md: {str(e)}")
return "Error loading help content."
with gr.Blocks() as demo:
with gr.Tabs():
with gr.Tab("Document Checker"):
gr.Markdown(
"""
# π FAA Document Checker Tool
This tool performs multiple **validation checks** on Word documents to ensure compliance with U.S. federal documentation standards. See the About tab for more information.
## How to Use
1. Upload your Word document (`.docx` format).
2. Select the document type.
3. Click **Check Document**.
> **Note:** Please ensure your document is clean (no track changes or comments).
"""
)
with gr.Row():
with gr.Column(scale=1):
file_input = gr.File(
label="π Upload Word Document (.docx)",
file_types=[".docx"],
type="binary"
)
doc_type = gr.Dropdown(
choices=document_types,
label="π Document Type",
value="Advisory Circular",
info="Select the type of document you're checking"
)
template_type = gr.Radio(
choices=template_types,
label="π Template Type",
visible=False,
info="Only applicable for Advisory Circulars"
)
submit_btn = gr.Button(
"π Check Document",
variant="primary"
)
with gr.Column(scale=2):
results = gr.HTML()
# Download button
# with gr.Row():
# download_btn = gr.Button(
# "β¬οΈ Download Results as PDF",
# variant="secondary",
# visible=False
# )
#
# pdf_file = gr.File(
# label="Download PDF",
# visible=False,
# interactive=False,
# file_types=[".pdf"]
# )
def process_and_format(file_obj, doc_type_value, template_type_value):
"""Process document and format results as HTML."""
try:
# Get text results from your original process_document function
checker = FAADocumentChecker()
if isinstance(file_obj, bytes):
file_obj = io.BytesIO(file_obj)
results_data = checker.run_all_checks(file_obj, doc_type_value, template_type_value)
# Format results using DocumentCheckResultsFormatter
formatter = DocumentCheckResultsFormatter()
text_results = formatter.format_results(results_data, doc_type_value)
# Convert to HTML
html_results = format_results_as_html(text_results)
# Return only the HTML results
return html_results
except Exception as e:
logging.error(f"Error processing document: {str(e)}")
traceback.print_exc()
error_html = f"""
<div style="color: red; padding: 1rem;">
β Error processing document: {str(e)}
<br><br>
Please ensure the file is a valid .docx document and try again.
</div>
"""
return error_html
# Update template type visibility based on document type
def update_template_visibility(doc_type_value):
if doc_type_value == "Advisory Circular":
return gr.update(visible=True)
else:
return gr.update(visible=False)
doc_type.change(
fn=update_template_visibility,
inputs=[doc_type],
outputs=[template_type]
)
# Handle document processing
submit_btn.click(
fn=process_and_format,
inputs=[file_input, doc_type, template_type],
outputs=[results] # Only output the results
)
# Function to generate PDF and provide it for download
# def generate_pdf(html_content):
# try:
# # Use a temporary file to store the PDF
# with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf:
# # Convert HTML to PDF using WeasyPrint
# HTML(string=html_content, base_url='.').write_pdf(tmp_pdf.name)
#
# # Return the path to the PDF file
# return gr.update(value=tmp_pdf.name, visible=True)
# except Exception as e:
# logging.error(f"Error generating PDF with WeasyPrint: {str(e)}")
# traceback.print_exc()
# return gr.update(value=None, visible=False)
# When the download button is clicked, generate the PDF
# download_btn.click(
# fn=generate_pdf,
# inputs=[results],
# outputs=[pdf_file]
# )
gr.Markdown(
"""
### π Important Notes
- This tool helps ensure compliance with federal documentation standards
- Results are based on current style guides and FAA requirements
- The tool provides suggestions but final editorial decisions rest with the document author
- For questions or feedback on the FAA documentation standards, contact the AIR-646 Senior Technical Writers
- For questions or feedback on the tool, contact Eric Putnam
- Results are not stored or saved
"""
)
with gr.Tab("About"):
readme_content = get_readme_content()
gr.Markdown(readme_content)
return demo
# Initialize and launch the interface
if __name__ == "__main__":
# Create and launch the interface
demo = create_interface()
demo.launch(
share=False, # Set to True if you want to generate a public link
server_name="0.0.0.0", # Allows external access
server_port=7860, # Default Gradio port
debug=True
) |