问:
Read rpt file content column by column in PHP
I have .rpt files and I need to read their data and then store the data in MYSQL Database. I tried the following solution:

```<code>$lines = file("aa.rpt");
echo '<table>';
foreach ($lines as $line_num) {
        $str = trim(htmlspecialchars($line_num));
        echo '<tr>';
        echo '<td>' . getColumnText("/\s([a-zA-Z\s]+)/", $str) .'</td>';
        $secondCol = getColumnText("/\s([0-9]+)/", $str);
        echo '<td>' . $secondCol .'</td>';
        $thirdCol = end(explode(" ", $str));
        if (trim($secondCol) === $thirdCol) {
            echo '<td style="text-align:right">' . str_repeat(" ", 10) .'</td>';
        } else {
            echo '<td style="text-align:right">' . str_repeat(" ", 10) . $thirdCol .'</td>';
        }
        echo '</tr>';

}
echo '</table>';

function getColumnText($pattern, $str) {
    preg_match($pattern, $str, $matches);
    return trim(current($matches));
}
</code>```

I got this code idea from another post in stackoverflow however I still have two problems with it:

I dont know the number of columns available in my file. It may vary with files.

Every File consists of numerous records where in after some records header is repeated again. Also, header is not consistent among different files so I cannot estimate the number of lines to remove from top and between the content whenever header occurs again.

I edited the file in Notepad++ which I am pasting here as a sample, however I am not sure whether all hidden characters will be replicated as it is or not.

```<code>                                            PSOMENAMEK                                                                                PASREP 1/29
                                            ========================

                                            Address wise list of customers for loans and advances for Sol Id : 119600                 Date of Report           06-12-2018
                                            ===============================================================================
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SNO     Name of the Borrower                Communication Address                                                           City           State               Pin                 Customer Id    Account No          Mobile Number_1  Mobile Number_2  Mobile Number_3  Mobile Number_4  Pan Number  SchemeCode  Date of Loan          Limit      Purpose of Advance        Bal. O/S      BC Entered on
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1      CHANDER SO RAMKALA AND MANSA RAM VP   VPO DALAMWALA,DIST JIND                                                        JIND CITY      HARYANA             126102              ABC111111      1111111111111111     1111111111                                                                      CCAKC       02-03-2009        1500000.00      RAISING CROPS            1505269.22   15-02-2017
2      ISHWAR  SO JITA                       VPO DALAMWALA,JIND                                                             JIND CITY      HARYANA             126102              ABC111111      1111111111111111     2222222222                                                                      CCAKC       02-03-2009         800000.00      RAISING CROPS             607925.78   06-02-2017
3      ROHTASH SO RAMPHAL                   #78/2 BLOCK NO 87 NEAR GOVT GIRLS SCHOOL,VPO SHAHPUR DISTT JIND HARYANA         JIND CITY      HARYANA             126125              ABC111111      1111111111111111     2222222222                                                                      CCAKC       30-10-2017         640000.00      RAISING CROPS             631757.63
4      RAM PHAL S/O SH GIANI RAM    KANDEL  112 BLOCK 96 KENDELA KANDELA,JIND                                               JIND CITY      HARYANA             126125              ABC111111      1111111111111111     1111111111                        2222222222       3333333333                   CCAKC       28-10-2007         995000.00      RAISING CROPS             998043.05   01-09-2017
5      SAT NARAIN S/O SH JHANDU RAM   SRI   1156 13 BHUNA ROAD OLD BAJRANG ROAD,TOHANA RURAL DISTT FATEHABAD HARYANA        TOHANA DISTT-F HARYANA             125120              ABC111111      1111111111111111     1111111111                        3333333333       2222222222                   CCAKC       28-10-2007         600000.00      RAISING CROPS             354523.05   21-02-2017
6      RAJESH SO DARIYA SINGH               146 V RAJ PANA PO SHAHPUR TEH JIND,JIND HARYANA                                 JIND CITY      HARYANA             126125              ABC111111      1111111111111111     1111111111                                                                      CCAKC       29-06-2018         550000.00      RAISING CROPS             245288.37
7      JAI BHAGWAN SO ATAR SINGH            1585/7 JAWAHAR NAGAR,PATIALA CHOWK                                              JIND CITY      HARYANA             126102              ABC111111      1111111111111111     3333333333                                                                      CCOTH       06-03-2017          40000.00      WORKING CAPITAL F          39636.00

                                            PSOMENAMEK                                                                                PSTREP 1/29
                                            ========================

                                            Address wise list of customers for loans and advances for Sol Id : 119600                 Date of Report           06-12-2018
                                            ===============================================================================
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SNO     Name of the Borrower                Communication Address                                                           City           State               Pin                 Customer Id    Account No          Mobile Number_1  Mobile Number_2  Mobile Number_3  Mobile Number_4  Pan Number  SchemeCode  Date of Loan          Limit      Purpose of Advance        Bal. O/S      BC Entered on
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

172    RAJKUMAR SO JAGTU                    V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     1111111111                                                                      CCAKC       28-10-2007         300000.00      RAISING CROPS             294446.29   02-05-2016
173    RAM MEHAR S/O MOJI RAM               V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     2222222222                                                          AAAAAAAAAA  CCAKC       07-10-2010         400000.00      RAISING CROPS             401412.40   04-10-2015
174    RAMKARAN SO SHIVDHAN                 V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     1111111111                                                                      CCAKC       28-10-2007         550000.00      RAISING CROPS             417816.51   06-01-2017
175    RISHI LAL SO SHIVDHAN                V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111                                                                                CCAKC       28-10-2007         995000.00      RAISING CROPS             997902.96   14-02-2017
176    SATYAWAN URF SATNARAIN S/O JAGE RAM  V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     2222222222                                                                      CCAKC       17-11-2010         300000.00      RAISING CROPS UND         308424.76   09-12-2014
177    SHYAM S/O SATYA NARAYAN              V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126125              ABC111111      1111111111111111     1111111111                                                                      CCAKC       16-05-2017         301000.00      RAISING CROPS             301836.88
178    SHAMSHER SINGH SO BIRU RAM           V-SIRIRAG PO-SHAHPUR TEH-JIND,JIND HARYANA                                      JIND CITY      HARYANA             126125              ABC111111      1111111111111111     3333333333                                                                      CCKGS       17-12-2007         600000.00      RAISING CROPS             603381.00   26-06-2018
179    JAI BHAGWAN S/O SH BARU RAM    SRI   V-SRI RAG KHERA PO- SHAHPUR TEH-JIND,JIND HARYANA                               JIND CITY      HARYANA             126125              ABC111111      1111111111111111     4444444444                                                          AAAAAAAAAA  CCAKC       28-10-2007         600000.00      RAISING CROPS             663130.17   26-06-2018
</code>```

OTHER SAMPLE

```<code>BANK NAME                             Report Date:   06-12-2018               Page No.   :   1

                                                        Daily Outstanding Report

CIRCLE :  119600 - KANDELA                                                                     Amount in Account Currency
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
S No.  A/c No.          Name of the Account               Account  Dt of           Sanction Limit           Value of        Drawing Power             Balance              IRREGULARITY      Interest
                                                           Cur.     Sanction                                 Security                              Outstanding                               Rate        Clas
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Scheme Code : CCAKC
------------------------------------------
     1  1196008800001006 CHHAJJU RAM SO SH HIRA LAL        INR      28-01-2014         3,00,000.00        5,00,000.00          3,00,000.00         2,88,588.31 Dr                  0.00    7.00   STANDARD AS
     2  1196008800001158 RAMKARAN SO SHIVDHAN              INR      03-10-2016         5,50,000.00       11,00,000.00          5,50,000.00         4,17,816.51 Dr                  0.00   11.30   STANDARD AS
     3  1196008800001219 BALJIT SINGH SO SH RAM DHAN       INR      12-01-2015        18,00,000.00       30,00,000.00         18,00,000.00        17,99,175.06 Dr                  0.00   11.30   STANDARD AS
     4  1196008800001325 RAM NIWAS SO RAM KISHAN           INR      09-06-2014           30,000.00        1,50,000.00            30,000.00           30,402.62 Dr                402.62    7.00   STANDARD AS
     5  1196008800001352 SAJANPAL S/O SAMAT                INR      18-11-2015         1,50,000.00        3,00,000.00          1,50,000.00         1,47,677.50 Dr                  0.00   11.25   STANDARD AS
     6  1196008800001361 MUNSHI RAM SO SH JAI RAM    SR    INR      19-06-2015           80,000.00          80,000.00            80,000.00            1,585.63 Dr                  0.00   11.25   STANDARD AS
     7  1196008800001440 RICHH PAL SO RANPAT               INR      20-10-2018         2,50,000.00        4,00,000.00          2,50,000.00         2,49,819.36 Dr                  0.00    7.00   STANDARD AS
     8  1196008800001477 RAM DIYA SO HARI RAM              INR      19-05-2015           80,000.00        5,00,000.00            80,000.00           79,671.79 Dr                  0.00   14.00   STANDARD AS
     9  1196008800001486 SIYA RAM SO JOGI RAM              INR      01-05-2017         9,95,000.00       13,00,000.00          9,95,000.00         5,03,155.07 Dr                  0.00   11.30   STANDARD AS
    10  1196008800001547 GANGA BISAN S/O RAI SINGH         INR      27-03-2018         1,85,000.00        2,50,000.00          1,85,000.00         1,84,895.47 Dr                  0.00    7.00   STANDARD AS
    11  1196008800001556 SUNHERA SINGH SO MANGE RAM        INR      06-01-2014         4,00,000.00        8,00,000.00          4,00,000.00         1,79,359.39 Dr                  0.00    7.00   STANDARD AS
</code>```

答:
What about something like this.

```<code>//           SNO     Name   Addr   City   State  Pin    CustId AccNo  Mobiles                     Pan    Scheme DateLoan               Limit  PrAdv  BalOs  BCEntered
$pattern = '/(\d+\s*)(.{37})(.{80})(.{15})(.{20})(.{20})(.{15})(.{21})(.{17})(.{17})(.{17})(.{17})(.{12})(.{12})(\d\d-\d\d-\d\d\d\d\s*)(.{15})(.{26})(.{12})((?:\d\d-\d\d-\d\d\d\d)?)/';

function readAccount($str) {
    if( !preg_match($pattern, $str, $matches) ) return null;
    return array(
            "sno"=>trim($matches[1]),
            "name"=>trim($matches[2]),
            "addr"=>trim($matches[3]),
            //...
    );
}

$lines = file("aa.rpt");
foreach ($lines as $line_num) {
    $str = trim(htmlspecialchars($line_num));
    $account = readAccount($str);
    if( $account)
            saveToDb($account);
}
</code>```

If currently processing line is an invalid data row, readCustomer will not match and return null. Otherwise, method will read info groups one by one, and return account object. In the loop, if returned value is a valid account, you can save it to db.

Body:
I have .rpt files and I need to read their data and then store the data in MYSQL Database. I tried the following solution:

$lines = file("aa.rpt");
echo '
';
foreach ($lines as $line_num) {
        $str = trim(htmlspecialchars($line_num));
        echo '
';
        echo '' . getColumnText("/\s([a-zA-Z\s]+)/", $str) .'	';
        $secondCol = getColumnText("/\s([0-9]+)/", $str);
        echo '' . $secondCol .'	';
        $thirdCol = end(explode(" ", $str));
        if (trim($secondCol) === $thirdCol) {
            echo '' . str_repeat(" ", 10) .'	';
        } else {
            echo '' . str_repeat(" ", 10) . $thirdCol .'	';
        }
        echo '
';

}
echo '
';

function getColumnText($pattern, $str) {
    preg_match($pattern, $str, $matches);
    return trim(current($matches));
}

I got this code idea from another post in stackoverflow however I still have two problems with it:

I dont know the number of columns available in my file. It may vary with files.

Every File consists of numerous records where in after some records header is repeated again. Also, header is not consistent among different files so I cannot estimate the number of lines to remove from top and between the content whenever header occurs again.

I edited the file in Notepad++ which I am pasting here as a sample, however I am not sure whether all hidden characters will be replicated as it is or not.

                                            PSOMENAMEK                                                                                PASREP 1/29
                                            ========================

                                            Address wise list of customers for loans and advances for Sol Id : 119600                 Date of Report           06-12-2018
                                            ===============================================================================
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SNO     Name of the Borrower                Communication Address                                                           City           State               Pin                 Customer Id    Account No          Mobile Number_1  Mobile Number_2  Mobile Number_3  Mobile Number_4  Pan Number  SchemeCode  Date of Loan          Limit      Purpose of Advance        Bal. O/S      BC Entered on
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1      CHANDER SO RAMKALA AND MANSA RAM VP   VPO DALAMWALA,DIST JIND                                                        JIND CITY      HARYANA             126102              ABC111111      1111111111111111     1111111111                                                                      CCAKC       02-03-2009        1500000.00      RAISING CROPS            1505269.22   15-02-2017
2      ISHWAR  SO JITA                       VPO DALAMWALA,JIND                                                             JIND CITY      HARYANA             126102              ABC111111      1111111111111111     2222222222                                                                      CCAKC       02-03-2009         800000.00      RAISING CROPS             607925.78   06-02-2017
3      ROHTASH SO RAMPHAL                   #78/2 BLOCK NO 87 NEAR GOVT GIRLS SCHOOL,VPO SHAHPUR DISTT JIND HARYANA         JIND CITY      HARYANA             126125              ABC111111      1111111111111111     2222222222                                                                      CCAKC       30-10-2017         640000.00      RAISING CROPS             631757.63
4      RAM PHAL S/O SH GIANI RAM    KANDEL  112 BLOCK 96 KENDELA KANDELA,JIND                                               JIND CITY      HARYANA             126125              ABC111111      1111111111111111     1111111111                        2222222222       3333333333                   CCAKC       28-10-2007         995000.00      RAISING CROPS             998043.05   01-09-2017
5      SAT NARAIN S/O SH JHANDU RAM   SRI   1156 13 BHUNA ROAD OLD BAJRANG ROAD,TOHANA RURAL DISTT FATEHABAD HARYANA        TOHANA DISTT-F HARYANA             125120              ABC111111      1111111111111111     1111111111                        3333333333       2222222222                   CCAKC       28-10-2007         600000.00      RAISING CROPS             354523.05   21-02-2017
6      RAJESH SO DARIYA SINGH               146 V RAJ PANA PO SHAHPUR TEH JIND,JIND HARYANA                                 JIND CITY      HARYANA             126125              ABC111111      1111111111111111     1111111111                                                                      CCAKC       29-06-2018         550000.00      RAISING CROPS             245288.37
7      JAI BHAGWAN SO ATAR SINGH            1585/7 JAWAHAR NAGAR,PATIALA CHOWK                                              JIND CITY      HARYANA             126102              ABC111111      1111111111111111     3333333333                                                                      CCOTH       06-03-2017          40000.00      WORKING CAPITAL F          39636.00

                                            PSOMENAMEK                                                                                PSTREP 1/29
                                            ========================

                                            Address wise list of customers for loans and advances for Sol Id : 119600                 Date of Report           06-12-2018
                                            ===============================================================================
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SNO     Name of the Borrower                Communication Address                                                           City           State               Pin                 Customer Id    Account No          Mobile Number_1  Mobile Number_2  Mobile Number_3  Mobile Number_4  Pan Number  SchemeCode  Date of Loan          Limit      Purpose of Advance        Bal. O/S      BC Entered on
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

172    RAJKUMAR SO JAGTU                    V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     1111111111                                                                      CCAKC       28-10-2007         300000.00      RAISING CROPS             294446.29   02-05-2016
173    RAM MEHAR S/O MOJI RAM               V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     2222222222                                                          AAAAAAAAAA  CCAKC       07-10-2010         400000.00      RAISING CROPS             401412.40   04-10-2015
174    RAMKARAN SO SHIVDHAN                 V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     1111111111                                                                      CCAKC       28-10-2007         550000.00      RAISING CROPS             417816.51   06-01-2017
175    RISHI LAL SO SHIVDHAN                V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111                                                                                CCAKC       28-10-2007         995000.00      RAISING CROPS             997902.96   14-02-2017
176    SATYAWAN URF SATNARAIN S/O JAGE RAM  V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     2222222222                                                                      CCAKC       17-11-2010         300000.00      RAISING CROPS UND         308424.76   09-12-2014
177    SHYAM S/O SATYA NARAYAN              V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126125              ABC111111      1111111111111111     1111111111                                                                      CCAKC       16-05-2017         301000.00      RAISING CROPS             301836.88
178    SHAMSHER SINGH SO BIRU RAM           V-SIRIRAG PO-SHAHPUR TEH-JIND,JIND HARYANA                                      JIND CITY      HARYANA             126125              ABC111111      1111111111111111     3333333333                                                                      CCKGS       17-12-2007         600000.00      RAISING CROPS             603381.00   26-06-2018
179    JAI BHAGWAN S/O SH BARU RAM    SRI   V-SRI RAG KHERA PO- SHAHPUR TEH-JIND,JIND HARYANA                               JIND CITY      HARYANA             126125              ABC111111      1111111111111111     4444444444                                                          AAAAAAAAAA  CCAKC       28-10-2007         600000.00      RAISING CROPS             663130.17   26-06-2018

OTHER SAMPLE

BANK NAME                             Report Date:   06-12-2018               Page No.   :   1

                                                        Daily Outstanding Report

CIRCLE :  119600 - KANDELA                                                                     Amount in Account Currency
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
S No.  A/c No.          Name of the Account               Account  Dt of           Sanction Limit           Value of        Drawing Power             Balance              IRREGULARITY      Interest
                                                           Cur.     Sanction                                 Security                              Outstanding                               Rate        Clas
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Scheme Code : CCAKC
------------------------------------------
     1  1196008800001006 CHHAJJU RAM SO SH HIRA LAL        INR      28-01-2014         3,00,000.00        5,00,000.00          3,00,000.00         2,88,588.31 Dr                  0.00    7.00   STANDARD AS
     2  1196008800001158 RAMKARAN SO SHIVDHAN              INR      03-10-2016         5,50,000.00       11,00,000.00          5,50,000.00         4,17,816.51 Dr                  0.00   11.30   STANDARD AS
     3  1196008800001219 BALJIT SINGH SO SH RAM DHAN       INR      12-01-2015        18,00,000.00       30,00,000.00         18,00,000.00        17,99,175.06 Dr                  0.00   11.30   STANDARD AS
     4  1196008800001325 RAM NIWAS SO RAM KISHAN           INR      09-06-2014           30,000.00        1,50,000.00            30,000.00           30,402.62 Dr                402.62    7.00   STANDARD AS
     5  1196008800001352 SAJANPAL S/O SAMAT                INR      18-11-2015         1,50,000.00        3,00,000.00          1,50,000.00         1,47,677.50 Dr                  0.00   11.25   STANDARD AS
     6  1196008800001361 MUNSHI RAM SO SH JAI RAM    SR    INR      19-06-2015           80,000.00          80,000.00            80,000.00            1,585.63 Dr                  0.00   11.25   STANDARD AS
     7  1196008800001440 RICHH PAL SO RANPAT               INR      20-10-2018         2,50,000.00        4,00,000.00          2,50,000.00         2,49,819.36 Dr                  0.00    7.00   STANDARD AS
     8  1196008800001477 RAM DIYA SO HARI RAM              INR      19-05-2015           80,000.00        5,00,000.00            80,000.00           79,671.79 Dr                  0.00   14.00   STANDARD AS
     9  1196008800001486 SIYA RAM SO JOGI RAM              INR      01-05-2017         9,95,000.00       13,00,000.00          9,95,000.00         5,03,155.07 Dr                  0.00   11.30   STANDARD AS
    10  1196008800001547 GANGA BISAN S/O RAI SINGH         INR      27-03-2018         1,85,000.00        2,50,000.00          1,85,000.00         1,84,895.47 Dr                  0.00    7.00   STANDARD AS
    11  1196008800001556 SUNHERA SINGH SO MANGE RAM        INR      06-01-2014         4,00,000.00        8,00,000.00          4,00,000.00         1,79,359.39 Dr                  0.00    7.00   STANDARD AS

Body:
What about something like this.

//           SNO     Name   Addr   City   State  Pin    CustId AccNo  Mobiles                     Pan    Scheme DateLoan               Limit  PrAdv  BalOs  BCEntered
$pattern = '/(\d+\s*)(.{37})(.{80})(.{15})(.{20})(.{20})(.{15})(.{21})(.{17})(.{17})(.{17})(.{17})(.{12})(.{12})(\d\d-\d\d-\d\d\d\d\s*)(.{15})(.{26})(.{12})((?:\d\d-\d\d-\d\d\d\d)?)/';

function readAccount($str) {
    if( !preg_match($pattern, $str, $matches) ) return null;
    return array(
            "sno"=>trim($matches[1]),
            "name"=>trim($matches[2]),
            "addr"=>trim($matches[3]),
            //...
    );
}

$lines = file("aa.rpt");
foreach ($lines as $line_num) {
    $str = trim(htmlspecialchars($line_num));
    $account = readAccount($str);
    if( $account)
            saveToDb($account);
}

If currently processing line is an invalid data row, readCustomer will not match and return null. Otherwise, method will read info groups one by one, and return account object. In the loop, if returned value is a valid account, you can save it to db.


来源: stackexchange

问:
PHP web-crawler
I'm looking for a PHP web-crawler to gather all the links to for a large site and tell me if the links are broken.

So far I've tried modifying an example on here myself. My question about the codeI've also tried grabbing phpDig but the site is down. any suggestions would be great on how I should proceed would be great.

EDIT

The problem isn't the grabbing of the links the issue of the scale I'm not sure if the script I modified is sufficient enough to grab what possibly be thousands of URL's as I tried setting the depth for the search link to 4 and the crawler timed out through the browser. Someone else mentioned something about killing processes as to not overload the server, could someone please elaborate on the issue.

答:
Not a ready-to-use solution, but Simple HTML Dom parser is one of my favourite dom parsers.
It let's you use CSS selectors for finding nodes over the document, so you can easily find 's.
With these hyperlinks's you can build your own crawler and check if the pages are still available.

href="http://simplehtmldom.sourceforge.net/" rel="nofollow">You can find it here.

ContentLicense: CC BY-SA 3.0

Title: PHP web-crawler

Body:
Not a ready-to-use solution, but Simple HTML Dom parser is one of my favourite dom parsers.
It let's you use CSS selectors for finding nodes over the document, so you can easily find <a href="">'s.
With these hyperlinks's you can build your own crawler and check if the pages are still available.

You can find it here.


问:
Joint for physical gun slider?
I want to make it so when I fire my gun the slider shoots back and then comes forward like a real gun. However I don't know how I should start because I can't find any relevant information on Google and I don't know what to search. All I need is the logic behind how to do it, I can code it myself.</p>

答:
I would agree with zambari.
Just create an animation and play it when your guns gets fired.

Edit:

Since you are talking about 3D, you could either

move the pivot of the object to the point where the trigger would be attached. This way, all you need to do is change the objects rotation for the animation.

use joints

ContentLicense: CC BY-SA 4.0

Title: Joint for physical gun slider?


问:
Why won't my query execute in my code, while it executes just fine in normal sql?
I've run into a problem while making a project for school, we are supposed to import data from a .txt file to our C# database. I thought I had it figured out but my "insert" lines weren't inserting data to my tables. So, in the end, I tried to insert only 1 line with all values written in, and it still won't insert the data into the database.

I tried the "New query" option by right clicking on my table and copy-pasted the insert line from my code, and that worked just fine, so I don't know why the line in the code isn't working.

```<code>class Program
{
    static void Main(string[] args)
    {
        string connectionString = @"Data Source= (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|BazaPRO2.mdf;Integrated Security=True;Connect Timeout=30";
        SqlConnection dataConnection = new SqlConnection(connectionString);
        string q;
        dataConnection.Open();
        q = "INSERT INTO Sola(SolaID,Naziv,Naslov,Kraj,Posta,Telefon,Eposta) VALUES(1,'Test','Test','Test',1000,'Test','Test')";
        SqlCommand dataCommand = new SqlCommand(q, dataConnection);

        try
        {
            dataCommand.ExecuteNonQuery();
            Console.WriteLine("Success");
            dataConnection.Close();
        }
        catch { Console.WriteLine("Fail"); }
    }
}
</code>```

I tried pasting the executenonquery line in a try block, and it DOES write "Success" on my screen, but the insert line does NOT execute.

答:
Check the return value of dataCommand.ExecuteNonQuery(); (integer Value) if it return -1 something went wrong (for example a transaction rollback). If 0 no rows were affected.

```<code>int return_value = dataCommand.ExecuteNonQuery();
if(return_value > 0)
   //goood :)
else
   //something wrong :(
</code>```

EDIT:

Btw is better dispose the commands after using them like below:

```<code>string connectionString = @"Data Source= (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|BazaPRO2.mdf;Integrated Security=True;Connect Timeout=30";
SqlConnection dataConnection = new SqlConnection(connectionString);
dataConnection.Open();
string q = "INSERT INTO Sola(SolaID,Naziv,Naslov,Kraj,Posta,Telefon,Eposta) VALUES(1,'Test','Test','Test',1000,'Test','Test')";
using(SqlCommand dataCommand = new SqlCommand(q, dataConnection))
{
    try
    {
        dataCommand.ExecuteNonQuery();
        Console.WriteLine("Success");
    }
    catch { Console.WriteLine("Fail"); }
}
dataConnection.Close();
</code>```

EDIT2: Considering questions in the comments.

What are you saying writing |DataDirectory| is "search in the application path for that dasabase", if you're debugging your application it means that it search the dabase in the output debug folder... If you wont that you should target a database out of your appliation directory with a relative/absolute path (look AppDomain.SetData method) or copy your database in your application directory... is hard answer you without knowing your goal :)

To be more specific, before initialize your SqlConnection call the following code:

```<code>AppDomain.SetData("DataDirectory", "C:\\TEST\\");
</code>```

To set your |DataDirectory| pointing at your database path.

Body:
I've run into a problem while making a project for school, we are supposed to import data from a .txt file to our C# database. I thought I had it figured out but my "insert" lines weren't inserting data to my tables. So, in the end, I tried to insert only 1 line with all values written in, and it still won't insert the data into the database.

I tried the "New query" option by right clicking on my table and copy-pasted the insert line from my code, and that worked just fine, so I don't know why the line in the code isn't working.

class Program
{
    static void Main(string[] args)
    {
        string connectionString = @"Data Source= (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|BazaPRO2.mdf;Integrated Security=True;Connect Timeout=30";
        SqlConnection dataConnection = new SqlConnection(connectionString);
        string q;
        dataConnection.Open();
        q = "INSERT INTO Sola(SolaID,Naziv,Naslov,Kraj,Posta,Telefon,Eposta) VALUES(1,'Test','Test','Test',1000,'Test','Test')";
        SqlCommand dataCommand = new SqlCommand(q, dataConnection);

        try
        {
            dataCommand.ExecuteNonQuery();
            Console.WriteLine("Success");
            dataConnection.Close();
        }
        catch { Console.WriteLine("Fail"); }
    }
}

I tried pasting the executenonquery line in a try block, and it DOES write "Success" on my screen, but the insert line does NOT execute.

Body:
Check the return value of dataCommand.ExecuteNonQuery(); (integer Value) if it return -1 something went wrong (for example a transaction rollback). If 0 no rows were affected.

int return_value = dataCommand.ExecuteNonQuery();
if(return_value > 0)
   //goood :)
else
   //something wrong :(

EDIT:

Btw is better dispose the commands after using them like below:

string connectionString = @"Data Source= (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|BazaPRO2.mdf;Integrated Security=True;Connect Timeout=30";
SqlConnection dataConnection = new SqlConnection(connectionString);
dataConnection.Open();
string q = "INSERT INTO Sola(SolaID,Naziv,Naslov,Kraj,Posta,Telefon,Eposta) VALUES(1,'Test','Test','Test',1000,'Test','Test')";
using(SqlCommand dataCommand = new SqlCommand(q, dataConnection))
{
    try
    {
        dataCommand.ExecuteNonQuery();
        Console.WriteLine("Success");
    }
    catch { Console.WriteLine("Fail"); }
}
dataConnection.Close();

EDIT2: Considering questions in the comments.

What are you saying writing |DataDirectory| is "search in the application path for that dasabase", if you're debugging your application it means that it search the dabase in the output debug folder... If you wont that you should target a database out of your appliation directory with a relative/absolute path (look AppDomain.SetData method) or copy your database in your application directory... is hard answer you without knowing your goal :)

To be more specific, before initialize your SqlConnection call the following code:

AppDomain.SetData("DataDirectory", "C:\\TEST\\");

To set your |DataDirectory| pointing at your database path.


问:
Avoid refreshing of content in SplitLayout
I have a splitlayout in my vaadin application in which in the first column I should show different pages added through the addToPrimary method, while in the second column I should have a page which contains an IFrame with a videoconference. Now the problem is that when I change route in the first column, even the second is updated and this refreshes the IFrame.  The implementation of the showRouterLayoyutContent is the following:

```<code>    @Override
public void showRouterLayoutContent(HasElement content) {

    if (this.accessControl.isAccessGranted(UI.getCurrent(), ((ContentView) content).getName()) && ((ContentView) content).getName().equals("contattaView") ) {
        setLayoutCall((com.vaadin.flow.component.Component) content);
    }
    else if (this.accessControl.isAccessGranted(UI.getCurrent(), ((ContentView) content).getName())) {
        setLayoutContent((com.vaadin.flow.component.Component) content);

    }

}
</code>```

And the two methods setLayoutCall and setLayoutContent are the following:

```<code>private void setLayoutContent(com.vaadin.flow.component.Component content) {

    split.addToPrimary(content);

}
private void setLayoutCall(com.vaadin.flow.component.Component content) {

    split.addToSecondary(content);
    split.setThemeName("visible-split");
}
</code>```

How can I avoid to refresh the entire content when I update the first column of the split layout through navigation?

UPDATE: I'm showing also a very simple code on which I'm testing. The following class is the main layout:

```<code>
private SplitLayout split = new SplitLayout();
    private HorizontalLayout hl = new HorizontalLayout();
    private Div firstDiv = new Div();
    private Div secondDiv = new Div();
    public MainView() {
        Button button = new Button("Click me",
                event -> Notification.show("Clicked!"));
        final VerticalLayout menuBar = new VerticalLayout();
        menuBar.add(new RouterLink("first view", FirstView.class));
        menuBar.add(new RouterLink("second view", SecondView.class));
        menuBar.setAlignItems(Alignment.CENTER);
        add(menuBar);
        //split.addToPrimary(firstDiv);
        //split.addToSecondary(secondDiv);
        //firstDiv.setId("first");
        //secondDiv.setId("second");
        //hl.add(firstDiv,secondDiv);
        add(split);
        //add(hl);
    }

    @Override
    public void showRouterLayoutContent(HasElement element) {

        if(element!=null && element.getClass().getName().contains("FirstView")) {
            split.addToPrimary((Component) element);
            //firstDiv.removeAll();
            //firstDiv.add((Component) element);
            //firstDiv.removeAll();
            //firstDiv.getElement().appendChild(new Element[]{element.getElement()});

        }
        else if(element!=null && element.getClass().getName().contains("SecondView") ) {
            secondDiv.removeAll();
            secondDiv.add((Component) element);
            split.addToSecondary((Component) element);
            //split.addToSecondary(element.getElement().getComponent().get());
        }

    }

</code>```

While these are the two views added to the split:

```<code>    @Route(value="v1",layout=MainView.class)
public class FirstView extends VerticalLayout implements RouterLayout  {

    public FirstView() {
        add(new Label("First View"));
    }
}
</code>```

```<code>@Route(value = "v2",layout=MainView.class)
public class SecondView extends VerticalLayout implements RouterLayout {
public SecondView() {
    IFrame frame = new IFrame();
    frame.setSrc("https://www.youtube.com/watch?v=LoigVtPCYPk&list=RDLoigVtPCYPk&start_radio=1");
    add(frame);
}
}
</code>```

答:
Your comment does indeed seem to be the issue.

I recommend creating a Div wrapper for the primary content, and instead changing the content of that.

```<code>private final Div wrapper;

public MyLayout() {
    wrapper = new Div();
    wrapper.setSizeFull();

    split.addToPrimary(wrapper);
}

private void setLayoutContent(com.vaadin.flow.component.Component content) {
    wrapper.removeAll();
    wrapper.add(content);
}
</code>```

You might also want to do the same for the secondary. In addition, to prevent any components from being automatically removed when navigating, you can override removeRouterLayoutContent as well (available in Vaadin 14)

```<code>@Override
public void removeRouterLayoutContent(HasElement oldContent) {
    // Do nothing, we remove manually in showRouterLayoutContent
}
</code>```

Edit

If you can't override removeRouterLayoutContent, you can try creating your own instance of the HasElement to add. This is a bit of a hack, but might be the simplest solution.

```<code>public void showRouterLayoutContent(HasElement content) {
    if (content.getClass().getSimpleName().contains("TestView")) {
        // Creating a new instance should stop it from being auto removed
        content = new TestView();
        firstDiv.add((Component) content);
    }
    ...
}
</code>```

Body:
I have a splitlayout in my vaadin application in which in the first column I should show different pages added through the addToPrimary method, while in the second column I should have a page which contains an IFrame with a videoconference. Now the problem is that when I change route in the first column, even the second is updated and this refreshes the IFrame.  The implementation of the showRouterLayoyutContent is the following:

    @Override
public void showRouterLayoutContent(HasElement content) {

    if (this.accessControl.isAccessGranted(UI.getCurrent(), ((ContentView) content).getName()) && ((ContentView) content).getName().equals("contattaView") ) {
        setLayoutCall((com.vaadin.flow.component.Component) content);
    }
    else if (this.accessControl.isAccessGranted(UI.getCurrent(), ((ContentView) content).getName())) {
        setLayoutContent((com.vaadin.flow.component.Component) content);

    }

}

And the two methods setLayoutCall and setLayoutContent are the following:

private void setLayoutContent(com.vaadin.flow.component.Component content) {

    split.addToPrimary(content);

}
private void setLayoutCall(com.vaadin.flow.component.Component content) {

    split.addToSecondary(content);
    split.setThemeName("visible-split");
}

How can I avoid to refresh the entire content when I update the first column of the split layout through navigation?

UPDATE: I'm showing also a very simple code on which I'm testing. The following class is the main layout:

private SplitLayout split = new SplitLayout();
    private HorizontalLayout hl = new HorizontalLayout();
    private Div firstDiv = new Div();
    private Div secondDiv = new Div();
    public MainView() {
        Button button = new Button("Click me",
                event -> Notification.show("Clicked!"));
        final VerticalLayout menuBar = new VerticalLayout();
        menuBar.add(new RouterLink("first view", FirstView.class));
        menuBar.add(new RouterLink("second view", SecondView.class));
        menuBar.setAlignItems(Alignment.CENTER);
        add(menuBar);
        //split.addToPrimary(firstDiv);
        //split.addToSecondary(secondDiv);
        //firstDiv.setId("first");
        //secondDiv.setId("second");
        //hl.add(firstDiv,secondDiv);
        add(split);
        //add(hl);
    }

    @Override
    public void showRouterLayoutContent(HasElement element) {

        if(element!=null && element.getClass().getName().contains("FirstView")) {
            split.addToPrimary((Component) element);
            //firstDiv.removeAll();
            //firstDiv.add((Component) element);
            //firstDiv.removeAll();
            //firstDiv.getElement().appendChild(new Element[]{element.getElement()});

        }
        else if(element!=null && element.getClass().getName().contains("SecondView") ) {
            secondDiv.removeAll();
            secondDiv.add((Component) element);
            split.addToSecondary((Component) element);
            //split.addToSecondary(element.getElement().getComponent().get());
        }

    }

While these are the two views added to the split:

    @Route(value="v1",layout=MainView.class)
public class FirstView extends VerticalLayout implements RouterLayout  {

    public FirstView() {
        add(new Label("First View"));
    }
}

@Route(value = "v2",layout=MainView.class)
public class SecondView extends VerticalLayout implements RouterLayout {
public SecondView() {
    IFrame frame = new IFrame();
    frame.setSrc("https://www.youtube.com/watch?v=LoigVtPCYPk&list=RDLoigVtPCYPk&start_radio=1");
    add(frame);
}
}

Body:
Your comment does indeed seem to be the issue.

I recommend creating a Div wrapper for the primary content, and instead changing the content of that.

private final Div wrapper;

public MyLayout() {
    wrapper = new Div();
    wrapper.setSizeFull();

    split.addToPrimary(wrapper);
}

private void setLayoutContent(com.vaadin.flow.component.Component content) {
    wrapper.removeAll();
    wrapper.add(content);
}

You might also want to do the same for the secondary. In addition, to prevent any components from being automatically removed when navigating, you can override removeRouterLayoutContent as well (available in Vaadin 14)

@Override
public void removeRouterLayoutContent(HasElement oldContent) {
    // Do nothing, we remove manually in showRouterLayoutContent
}

Edit

If you can't override removeRouterLayoutContent, you can try creating your own instance of the HasElement to add. This is a bit of a hack, but might be the simplest solution.

public void showRouterLayoutContent(HasElement content) {
    if (content.getClass().getSimpleName().contains("TestView")) {
        // Creating a new instance should stop it from being auto removed
        content = new TestView();
        firstDiv.add((Component) content);
    }
    ...
}


问:
Exclude a certain file or directory while copying from a Google Cloud Storage
I wish to copy all files except a certain directory or directories (or files) from my GCS bucket to my local directory. Is there anyway I can do the same?

For example:
My GCS bucket named so-bucket has three folders dir1, dir2, dir3, file1 and file2. I want to copy all the files and directories except dir3 from the bucket to my local directory.

Usually I would do gsutil -m cp -r gs://so-bucket/* . and then delete the dir3 folder.

答:
You can use gsutil rsync command, with -x option to exclude some objects. Something like :

```<code>gsutil -m rsync -r -x '^dir3/*' gs://so-bucket .
</code>```

should retrieve all objects located on the bucket, except objects beginning with dir3 (files not located in dir3 directory in your example).

Body:
You can use gsutil rsync command, with -x option to exclude some objects. Something like :

gsutil -m rsync -r -x '^dir3/*' gs://so-bucket .

should retrieve all objects located on the bucket, except objects beginning with dir3 (files not located in dir3 directory in your example).


问:
selenium checkbox click not working in python
https://agent.loadbook.in/#/login
When I go to the signup form, the form has a checkbox as I agree with the condition. It's an Input tag checkbox. when I select the tag by id and when I perform click or send_key of submitting its showing checkbox not clickable.

below all Method are not work

```<code>driver.find_element_by_xpath('//div[@class="switch__container"]//input').click()

driver.find_element_by_xpath('//div[@class="switch__container"]//input').send_keys(Keys.ENTER)

driver.find_element_by_xpath('//div[@class="switch__container"]//input').submit()

driver.find_element_by_xpath('//div[@class="switch__container"]//input').send_keys("after")

driver.find_element_by_xpath('//div[@class="switch__container"]//label').click()

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://agent.loadbook.in/#/login")

driver.find_element_by_partial_link_text("Create an account").click()
</code>```

try:

```<code>driver.find_element_by_xpath('//div[@class="switch__container"]//input').click()
# driver.find_element("name","username").send_keys("test")
# driver.find_element("name","email").send_keys("test@test.com")
# driver.find_element("name","phone").send_keys("99999999")
# driver.find_element("name","password").send_keys("12345")
</code>```

except NoSuchElementException as exception:
      print("not found")

selenium.common.exceptions.ElementNotVisibleException: Message:
  element not interactable

答:
You need to wait for the element to appear in DOM using Selenium's Explicit Wait feature.

Open the page using WebDriver.get() function

```<code>driver.get("https://agent.loadbook.in/#/login")
</code>```

Click "Create an account" link:

```<code>driver.find_element_by_link_text("Create an account").click()
</code>```

Wait until the checkbox loads and becomes clickable:

```<code>WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "switch_div")))
</code>```

Click the checkbox:

```<code> driver.find_element_by_class_name("switch_div").click()
</code>```

More information: How to use Selenium to test web applications using AJAX technology

Body:
https://agent.loadbook.in/#/login
When I go to the signup form, the form has a checkbox as I agree with the condition. It's an Input tag checkbox. when I select the tag by id and when I perform click or send_key of submitting its showing checkbox not clickable.

below all Method are not work

driver.find_element_by_xpath('//div[@class="switch__container"]//input').click()

driver.find_element_by_xpath('//div[@class="switch__container"]//input').send_keys(Keys.ENTER)

driver.find_element_by_xpath('//div[@class="switch__container"]//input').submit()

driver.find_element_by_xpath('//div[@class="switch__container"]//input').send_keys("after")

driver.find_element_by_xpath('//div[@class="switch__container"]//label').click()

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://agent.loadbook.in/#/login")

driver.find_element_by_partial_link_text("Create an account").click()

try:

driver.find_element_by_xpath('//div[@class="switch__container"]//input').click()
# driver.find_element("name","username").send_keys("test")
# driver.find_element("name","email").send_keys("test@test.com")
# driver.find_element("name","phone").send_keys("99999999")
# driver.find_element("name","password").send_keys("12345")

except NoSuchElementException as exception:
      print("not found")

selenium.common.exceptions.ElementNotVisibleException: Message:
  element not interactable

Body:
You need to wait for the element to appear in DOM using Selenium's Explicit Wait feature.

Open the page using WebDriver.get() function

driver.get("https://agent.loadbook.in/#/login")

Click "Create an account" link:

driver.find_element_by_link_text("Create an account").click()

Wait until the checkbox loads and becomes clickable:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "switch_div")))

Click the checkbox:

 driver.find_element_by_class_name("switch_div").click()

More information: How to use Selenium to test web applications using AJAX technology


问:
Is it possible to determine if an enumeration was strongly typed?
C++11 introduced two different additions to how we can handle enums: an option to make them scoped, and an option to make them typed. So now we have four different enum subtypes:

```<code>enum Old {};
enum Typed : int8_t {};
enum class Scoped {};
enum class TypedScoped : int8_t {};
</code>```

This question asks how to determine whether the enumeration is scoped. I want to know how to determine whether the enumeration is typed.

Additional information

I use the Qt framework, which provides the QDataStream class for serializing/deserializing data in a portable cross-platform way.
Obviously, in order for the resulting data stream to be portable, you must store all integers in fixed-length form. That includes enums, too.
Back in the day, I made a couple of helper macros to define serialization/deserialization of enums by casting them to an integer with fixed (user-specified) length:

```<code>#define SC_DECLARE_DATASTREAM_WRITE_OPERATOR(_TYPE) \
    QDataStream &operator<<(QDataStream &stream, _TYPE v);

#define SC_DECLARE_DATASTREAM_READ_OPERATOR(_TYPE) \
    QDataStream &operator>>(QDataStream &stream, _TYPE &v);

#define SC_DECLARE_DATASTREAM_OPERATORS(_TYPE) \
    SC_DECLARE_DATASTREAM_WRITE_OPERATOR(_TYPE) \
    SC_DECLARE_DATASTREAM_READ_OPERATOR(_TYPE)

#define SC_DEFINE_DATASTREAM_ENUM_WRITE_OPERATOR(_TYPE, _LEN) \
    QDataStream &operator<<(QDataStream &stream, _TYPE v) \
{ \
    qint ## _LEN t = v; \
    static_assert(sizeof(t) >= sizeof(v), "Increase length"); \
    stream << t; \
    return stream; \
    }

#define SC_DEFINE_DATASTREAM_ENUM_READ_OPERATOR(_TYPE, _LEN) \
    QDataStream &operator>>(QDataStream &stream, _TYPE &v) \
{ \
    qint ## _LEN t {0}; \
    static_assert(sizeof(t) >= sizeof(v), "Increase length"); \
    stream >> t; \
    if(stream.status() == QDataStream::Ok) \
    v = static_cast<_TYPE>(t); \
    return stream; \
    }

#define SC_DEFINE_DATASTREAM_ENUM_OPERATORS(_TYPE, _LEN) \
    SC_DEFINE_DATASTREAM_ENUM_WRITE_OPERATOR(_TYPE, _LEN) \
    SC_DEFINE_DATASTREAM_ENUM_READ_OPERATOR(_TYPE, _LEN)
</code>```

Now that C++11 allows specifying the underlying enum type, I can simplify the above mentioned macros:

```<code>#define SC_DEFINE_DATASTREAM_TYPED_ENUM_WRITE_OPERATOR(_TYPE) \
    QDataStream &operator<<(QDataStream &stream, _TYPE v) \
{ \
    const std::underlying_type<_TYPE>::type t {static_cast<std::underlying_type<_TYPE>::type>(v)}; \
    stream << t; \
    return stream; \
    }

#define SC_DEFINE_DATASTREAM_TYPED_ENUM_READ_OPERATOR(_TYPE) \
    QDataStream &operator>>(QDataStream &stream, _TYPE &v) \
{ \
    std::underlying_type<_TYPE>::type t {0}; \
    stream >> t; \
    if(stream.status() == QDataStream::Ok) \
    v = static_cast<_TYPE>(t); \
    return stream; \
    }
</code>```

However, if the user accidentally uses the new (*_TYPED_*) macros for enums that do not have their underlying type specified, that will break the guarantee of portability, because compiling the same code on different platform may yield different underlying type and hence different integer length in serialization/deserialization code.
What I need is to add a static_assert to the code, which will break the compilation process if the enum was not strongly typed at the point of its declaration.

答:
std::underlying_type can be used to limit the compilation to a set of fixed width integer types (e.g. with std::is_same):

#include <type_traits>
#include <cstdint>

template <typename T>
    constexpr bool is_fixed =
        std::is_same<T, std::int8_t>::value ||
        std::is_same<T, std::int16_t>::value
        // etc..
    ;

enum class E1 : std::int8_t {};
    static_assert( is_fixed<std::underlying_type_t<E1>>, "fixed");

enum class E2 {};
    static_assert(!is_fixed<std::underlying_type_t<E2>>, "not fixed");
```

<p>Variable templates are indeed since C++14, but in C++11 the same can be achieved with a <code>constexpr</code> function or <code>struct</code>/<code>class</code>:</p>

```template <typename T>
    constexpr bool is_fixed_f() {
        return  std::is_same<T, std::int8_t>::value ||
                std::is_same<T, std::int16_t>::value
                // etc..
        ;
    }

template <typename T>
    struct is_fixed_s {
        static constexpr bool value =
            std::is_same<T, std::int8_t>::value ||
            std::is_same<T, std::int16_t>::value
            // etc..
        ;
    };
```

Body:
C++11 introduced two different additions to how we can handle enums: an option to make them scoped, and an option to make them typed. So now we have four different enum subtypes:

enum Old {};
enum Typed : int8_t {};
enum class Scoped {};
enum class TypedScoped : int8_t {};

This question asks how to determine whether the enumeration is scoped. I want to know how to determine whether the enumeration is typed.

Additional information

I use the Qt framework, which provides the QDataStream class for serializing/deserializing data in a portable cross-platform way.
Obviously, in order for the resulting data stream to be portable, you must store all integers in fixed-length form. That includes enums, too.
Back in the day, I made a couple of helper macros to define serialization/deserialization of enums by casting them to an integer with fixed (user-specified) length:

#define SC_DECLARE_DATASTREAM_WRITE_OPERATOR(_TYPE) \
    QDataStream &operator<<(QDataStream &stream, _TYPE v);

#define SC_DECLARE_DATASTREAM_READ_OPERATOR(_TYPE) \
    QDataStream &operator>>(QDataStream &stream, _TYPE &v);

#define SC_DECLARE_DATASTREAM_OPERATORS(_TYPE) \
    SC_DECLARE_DATASTREAM_WRITE_OPERATOR(_TYPE) \
    SC_DECLARE_DATASTREAM_READ_OPERATOR(_TYPE)

#define SC_DEFINE_DATASTREAM_ENUM_WRITE_OPERATOR(_TYPE, _LEN) \
    QDataStream &operator<<(QDataStream &stream, _TYPE v) \
{ \
    qint ## _LEN t = v; \
    static_assert(sizeof(t) >= sizeof(v), "Increase length"); \
    stream << t; \
    return stream; \
    }

#define SC_DEFINE_DATASTREAM_ENUM_READ_OPERATOR(_TYPE, _LEN) \
    QDataStream &operator>>(QDataStream &stream, _TYPE &v) \
{ \
    qint ## _LEN t {0}; \
    static_assert(sizeof(t) >= sizeof(v), "Increase length"); \
    stream >> t; \
    if(stream.status() == QDataStream::Ok) \
    v = static_cast<_TYPE>(t); \
    return stream; \
    }

#define SC_DEFINE_DATASTREAM_ENUM_OPERATORS(_TYPE, _LEN) \
    SC_DEFINE_DATASTREAM_ENUM_WRITE_OPERATOR(_TYPE, _LEN) \
    SC_DEFINE_DATASTREAM_ENUM_READ_OPERATOR(_TYPE, _LEN)

Now that C++11 allows specifying the underlying enum type, I can simplify the above mentioned macros:

#define SC_DEFINE_DATASTREAM_TYPED_ENUM_WRITE_OPERATOR(_TYPE) \
    QDataStream &operator<<(QDataStream &stream, _TYPE v) \
{ \
    const std::underlying_type<_TYPE>::type t {static_cast<std::underlying_type<_TYPE>::type>(v)}; \
    stream << t; \
    return stream; \
    }

#define SC_DEFINE_DATASTREAM_TYPED_ENUM_READ_OPERATOR(_TYPE) \
    QDataStream &operator>>(QDataStream &stream, _TYPE &v) \
{ \
    std::underlying_type<_TYPE>::type t {0}; \
    stream >> t; \
    if(stream.status() == QDataStream::Ok) \
    v = static_cast<_TYPE>(t); \
    return stream; \
    }

However, if the user accidentally uses the new (*_TYPED_*) macros for enums that do not have their underlying type specified, that will break the guarantee of portability, because compiling the same code on different platform may yield different underlying type and hence different integer length in serialization/deserialization code.
What I need is to add a static_assert to the code, which will break the compilation process if the enum was not strongly typed at the point of its declaration.

Body:
std::underlying_type can be used to limit the compilation to a set of fixed width integer types (e.g. with std::is_same):

#include <type_traits>
#include <cstdint>

template <typename T>
    constexpr bool is_fixed =
        std::is_same<T, std::int8_t>::value ||
        std::is_same<T, std::int16_t>::value
        // etc..
    ;

enum class E1 : std::int8_t {};
    static_assert( is_fixed<std::underlying_type_t<E1>>, "fixed");

enum class E2 {};
    static_assert(!is_fixed<std::underlying_type_t<E2>>, "not fixed");

Variable templates are indeed since C++14, but in C++11 the same can be achieved with a constexpr function or struct/class:

template <typename T>
    constexpr bool is_fixed_f() {
        return  std::is_same<T, std::int8_t>::value ||
                std::is_same<T, std::int16_t>::value
                // etc..
        ;
    }

template <typename T>
    struct is_fixed_s {
        static constexpr bool value =
            std::is_same<T, std::int8_t>::value ||
            std::is_same<T, std::int16_t>::value
            // etc..
        ;
    };


问:
Swift search bar(controller) memory leaks
I have main screen, with a button on which I segue to searchVC screen.I have a navigation controller between them, in searchVC there are searchController and searchBar.

Problem:I need to activate search when screen appears, but searchBar activation(tap or becomeFirstResponder() )  causes memory leaks(image below)

I tried to remove delegates and the problem disappears, but I need to know when cancel button pressed to segue/dismiss to mainVC

Code:tableView for results, resultView with label for empty results

```<code>class SearchViewController: UIViewController,UISearchBarDelegate,UISearchControllerDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var resultView: ResultView!

let searchController = UISearchController(searchResultsController: nil)

var filteredSongs = [SongListModel]()
var songs = SongListModel.fetchSongs()

override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search songs"
    if #available(iOS 11.0, *) {
        navigationItem.titleView = searchController.searchBar
        navigationItem.hidesSearchBarWhenScrolling = false
        //            navigationController?.navigationBar.topItem?.searchController = searchController
        //            navigationItem.titleView?.isHidden = true
        searchController.dimsBackgroundDuringPresentation = false
        searchController.hidesNavigationBarDuringPresentation = false
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }
    searchController.searchBar.showsCancelButton = true
    searchController.definesPresentationContext = true

    searchController.searchBar.sizeToFit()
    searchController.delegate = self
    searchController.searchBar.delegate = self

    tableView.keyboardDismissMode = .interactive
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    delay(0.1) { [unowned self] in
        self.searchController.searchBar.becomeFirstResponder()
    }

}
func delay(_ delay: Double, closure: @escaping ()->()) {
    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    searchController.searchBar.resignFirstResponder()
}

func searchBarIsEmpty() -> Bool {
    // Returns true if the text is empty or nil
    return searchController.searchBar.text?.isEmpty ?? true
}

func filterContentForSearchText(_ searchText: String, scope: String = "All") {
    filteredSongs = songs.filter({( song : SongListModel) -> Bool in
        return song.musicFileName.lowercased().contains(searchText.lowercased())
    })

    tableView.reloadData()
}

func isFiltering() -> Bool {
    return searchController.isActive && !searchBarIsEmpty()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchController.searchBar.endEditing(true)
    searchController.searchBar.resignFirstResponder()
    //        searchController.searchBar.delegate = nil
    //        searchController.searchResultsUpdater = nil

    dismiss(animated: true, completion: nil)
}

// MARK: - Navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "PlayTilesSegue", let destinationVC = segue.destination as? TilesViewController, let selectedIndex = tableView.indexPathForSelectedRow?.row {
        let song: SongListModel
        if isFiltering() {
            song = filteredSongs[selectedIndex]
        } else {
            song = songs[selectedIndex]
        }
        destinationVC.songFileName = song.musicFileName
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
}
}
extension SearchViewController: UITableViewDelegate,UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isFiltering() {
        resultView.setIsFilteringToShow(filteredItemCount: filteredSongs.count, of: songs.count)
        return filteredSongs.count
    }

    resultView.setNotFiltering()
    return songs.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "filteredCell", for: indexPath) as! FilteredSongCell

    let song: SongListModel

    if isFiltering() {
        song = filteredSongs[indexPath.row]
    } else {
        song = songs[indexPath.row]
    }

    cell.listenSongButton.setBackgroundImage(UIImage(named: "playback"), for: .normal)
    cell.filteredAuthorNameLabel.text = song.authorName
    cell.filteredSongNameLabel.text = song.songName
    cell.playGameButton.setTitle(song.playButton.rawValue, for: .normal)

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "PlayTilesSegue", sender: indexPath)
    tableView.deselectRow(at: indexPath, animated: true)

}

}

extension SearchViewController: UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        filterContentForSearchText(searchController.searchBar.text!)
    }
}
</code>```

Image memory leaks

答:
You need to set the UISearchController searchBar's delegate. Once you have done this, the addition of the delegate method searchBarCancelButtonClicked: will properly be called.

Here it is.

Body:
I have main screen, with a button on which I segue to searchVC screen.I have a navigation controller between them, in searchVC there are searchController and searchBar.

Problem:I need to activate search when screen appears, but searchBar activation(tap or becomeFirstResponder() )  causes memory leaks(image below)

I tried to remove delegates and the problem disappears, but I need to know when cancel button pressed to segue/dismiss to mainVC

Code:tableView for results, resultView with label for empty results

class SearchViewController: UIViewController,UISearchBarDelegate,UISearchControllerDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var resultView: ResultView!

let searchController = UISearchController(searchResultsController: nil)

var filteredSongs = [SongListModel]()
var songs = SongListModel.fetchSongs()

override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search songs"
    if #available(iOS 11.0, *) {
        navigationItem.titleView = searchController.searchBar
        navigationItem.hidesSearchBarWhenScrolling = false
        //            navigationController?.navigationBar.topItem?.searchController = searchController
        //            navigationItem.titleView?.isHidden = true
        searchController.dimsBackgroundDuringPresentation = false
        searchController.hidesNavigationBarDuringPresentation = false
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }
    searchController.searchBar.showsCancelButton = true
    searchController.definesPresentationContext = true

    searchController.searchBar.sizeToFit()
    searchController.delegate = self
    searchController.searchBar.delegate = self

    tableView.keyboardDismissMode = .interactive
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    delay(0.1) { [unowned self] in
        self.searchController.searchBar.becomeFirstResponder()
    }

}
func delay(_ delay: Double, closure: @escaping ()->()) {
    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    searchController.searchBar.resignFirstResponder()
}

func searchBarIsEmpty() -> Bool {
    // Returns true if the text is empty or nil
    return searchController.searchBar.text?.isEmpty ?? true
}

func filterContentForSearchText(_ searchText: String, scope: String = "All") {
    filteredSongs = songs.filter({( song : SongListModel) -> Bool in
        return song.musicFileName.lowercased().contains(searchText.lowercased())
    })

    tableView.reloadData()
}

func isFiltering() -> Bool {
    return searchController.isActive && !searchBarIsEmpty()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchController.searchBar.endEditing(true)
    searchController.searchBar.resignFirstResponder()
    //        searchController.searchBar.delegate = nil
    //        searchController.searchResultsUpdater = nil

    dismiss(animated: true, completion: nil)
}

// MARK: - Navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "PlayTilesSegue", let destinationVC = segue.destination as? TilesViewController, let selectedIndex = tableView.indexPathForSelectedRow?.row {
        let song: SongListModel
        if isFiltering() {
            song = filteredSongs[selectedIndex]
        } else {
            song = songs[selectedIndex]
        }
        destinationVC.songFileName = song.musicFileName
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
}
}
extension SearchViewController: UITableViewDelegate,UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isFiltering() {
        resultView.setIsFilteringToShow(filteredItemCount: filteredSongs.count, of: songs.count)
        return filteredSongs.count
    }

    resultView.setNotFiltering()
    return songs.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "filteredCell", for: indexPath) as! FilteredSongCell

    let song: SongListModel

    if isFiltering() {
        song = filteredSongs[indexPath.row]
    } else {
        song = songs[indexPath.row]
    }

    cell.listenSongButton.setBackgroundImage(UIImage(named: "playback"), for: .normal)
    cell.filteredAuthorNameLabel.text = song.authorName
    cell.filteredSongNameLabel.text = song.songName
    cell.playGameButton.setTitle(song.playButton.rawValue, for: .normal)

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "PlayTilesSegue", sender: indexPath)
    tableView.deselectRow(at: indexPath, animated: true)

}

}

extension SearchViewController: UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        filterContentForSearchText(searchController.searchBar.text!)
    }
}

Image memory leaks


问:
Mallet Api in scala Akka throwing error -Request timeout encountered for request [GET /mallet Empty]
Please pardon me if the question sound naive as i am pretty new in akka.

I am trying to use the mallet api in scala akka in rest API
But getting error Request timeout encountered

below is the snapshot of my Server

```<code>Configuration.parser.parse(args,Configuration.ConfigurationOptions()) match {
        case Some(config) =>
          val serverBinding: Future[Http.ServerBinding] = Http().bindAndHandle(routes, config.interface, config.port)
          serverBinding.onComplete {
            case Success(bound) =>
              println(s"Server online at http://${bound.localAddress.getHostString}:${bound.localAddress.getPort}/")
            case Failure(e) =>
              log.error("Server could not start: ", e)
              system.terminate()
          }
        case None =>
          system.terminate()
      }

      Await.result(system.whenTerminated, Duration.Inf)
</code>```

snapshot of Router

```<code>lazy val apiRoutes: Route =
    ignoreTrailingSlash {
      pathSingleSlash {
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<html><body>Hello world!</body></html>"))
      } ~
        path("health") {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "ok"))
        } ~
        pathPrefix("mallet") {
          parameter('malletFile) { malletFile =>
          {val modelFile: Future[MalletModel] =
            (malletActor ? GetMalletOutput(malletFile)).mapTo[MalletModel]
            complete(modelFile)
          }
          }
        }
    }
</code>```

and finally the snapshot of MalletActor

```<code>class MalletActor(implicit val uaCache: Cache[String, MalletModel],
                  implicit val executionContext: ExecutionContext)
  extends Actor with ActorLogging with JsonSupport {

  import MalletActor._

  def receive: Receive = {
    case GetMalletOutput(malletFile) => sender() ! createMalletResult2(malletFile)
  }

  def createMalletResult2(malletFile: String): MalletModel = {

    logger.debug("run count...")
        val res = MalletResult(malletFile)
        val converted = res.Score.parseJson.convertTo[MalletRepo]
        val fileName = converted.ContentId

        val fileTemp = new File("src/main/resources/new_corpus/" + fileName)
        if (fileTemp.exists) {
          fileTemp.delete()
        }

        val output = new BufferedWriter(new FileWriter("src/main/resources/new_corpus/" + fileName))
        output.write(converted.ContentText)
        output.close()

    //runMalletInferring()

    val tmpDir = "src/main/resources/"
    logger.debug("Import all documents to mallet...")
    Text2Vectors.main(("--input " + tmpDir + "new_corpus/ --keep-sequence --remove-stopwords " + "--output " + tmpDir + "new_corpus.mallet --use-pipe-from " + tmpDir + "corpus.mallet").split(" "))
    logger.debug("Run training process...")
    InferTopics.main(("--input " + tmpDir + "new_corpus.mallet --inferencer " + tmpDir + "inferencer " + "--output-doc-topics " + tmpDir + "doc-topics-new.txt --num-iterations 1000").split(" "))
    logger.debug("Inferring process finished.")
</code>```

I am the getting error while calling text2Vector.main , the new vectorized file is getting created in new_corpus directory and new_corpus is getting generated as well. however after that I am getting the below error

```<code>Server online at http://127.0.0.1:9000/
12:45:38.622 [Sophi-Mallet-Api-akka.actor.default-dispatcher-3] DEBUG io.sophi.api.mallet.actors.MalletActor$ - run count...
12:45:38.634 [Sophi-Mallet-Api-akka.actor.default-dispatcher-3] DEBUG io.sophi.api.mallet.actors.MalletActor$ - Import all documents to mallet...
Couldn't open cc.mallet.util.MalletLogger resources/logging.properties file.
 Perhaps the 'resources' directories weren't copied into the 'class' directory.
 Continuing.
May 26, 2019 12:45:38 PM cc.mallet.classify.tui.Text2Vectors main
INFO: Labels =
May 26, 2019 12:45:38 PM cc.mallet.classify.tui.Text2Vectors main
INFO:    src/main/resources/new_corpus/
May 26, 2019 12:45:46 PM cc.mallet.classify.tui.Text2Vectors main
INFO:  rewriting previous instance list, with ID = 4e0a5a65540221c3:d508579:14b2ca15a26:-7ff7
[INFO] [05/26/2019 12:45:58.476] [Sophi-Mallet-Api-akka.actor.default-dispatcher-4] [akka.actor.ActorSystemImpl(Sophi-Mallet-Api)] Request timeout encountered for request [GET /mallet Empty]
</code>```

in web browser as well I am getting the error

```<code>The server was not able to produce a timely response to your request.
Please try again in a short while!
</code>```

答:
I figure out the solution myself. Please correct me if it still is vague.

There were couple of change needed.
First in application configuration file (application.conf)

below code needed to be updated.

```<code>server{
    idle-timeout = infinite
    request-timeout = infinite
}
settings {
  akka-workers-count = 100
  akka-workers-count = ${?AKKA_WORKERS_COUNT}
  actor-timeout = 100
  actor-timeout = ${?ACTOR_TIMEOUT}
}
</code>```

And the issue is needed to be handled in server code
by putting the api call inside timeout response

```<code>  val timeoutResponse = HttpResponse(
    StatusCodes.EnhanceYourCalm,
    entity = "Running Mallet modelling.")

  lazy val apiRoutes: Route =
    ignoreTrailingSlash {
      pathSingleSlash {
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<html><body>Hello world!</body></html>"))
      } ~
        path("health") {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "ok"))
        } ~
        pathPrefix("mallet") {
          withRequestTimeout(5.minute, request => timeoutResponse) {
          parameter('malletFile) { malletFile => {
            val modelFile: Future[MalletModel] =
              (malletActor ? GetMalletOutput(malletFile)).mapTo[MalletModel]
            complete(modelFile)
          }
          }

          }
        }
    }
</code>```

Body:
Please pardon me if the question sound naive as i am pretty new in akka.

I am trying to use the mallet api in scala akka in rest API
But getting error Request timeout encountered

below is the snapshot of my Server

Configuration.parser.parse(args,Configuration.ConfigurationOptions()) match {
        case Some(config) =>
          val serverBinding: Future[Http.ServerBinding] = Http().bindAndHandle(routes, config.interface, config.port)
          serverBinding.onComplete {
            case Success(bound) =>
              println(s"Server online at http://${bound.localAddress.getHostString}:${bound.localAddress.getPort}/")
            case Failure(e) =>
              log.error("Server could not start: ", e)
              system.terminate()
          }
        case None =>
          system.terminate()
      }

      Await.result(system.whenTerminated, Duration.Inf)

snapshot of Router

lazy val apiRoutes: Route =
    ignoreTrailingSlash {
      pathSingleSlash {
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Hello world!"))
      } ~
        path("health") {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "ok"))
        } ~
        pathPrefix("mallet") {
          parameter('malletFile) { malletFile =>
          {val modelFile: Future[MalletModel] =
            (malletActor ? GetMalletOutput(malletFile)).mapTo[MalletModel]
            complete(modelFile)
          }
          }
        }
    }

and finally the snapshot of MalletActor

class MalletActor(implicit val uaCache: Cache[String, MalletModel],
                  implicit val executionContext: ExecutionContext)
  extends Actor with ActorLogging with JsonSupport {

  import MalletActor._

  def receive: Receive = {
    case GetMalletOutput(malletFile) => sender() ! createMalletResult2(malletFile)
  }

  def createMalletResult2(malletFile: String): MalletModel = {

    logger.debug("run count...")
        val res = MalletResult(malletFile)
        val converted = res.Score.parseJson.convertTo[MalletRepo]
        val fileName = converted.ContentId

        val fileTemp = new File("src/main/resources/new_corpus/" + fileName)
        if (fileTemp.exists) {
          fileTemp.delete()
        }

        val output = new BufferedWriter(new FileWriter("src/main/resources/new_corpus/" + fileName))
        output.write(converted.ContentText)
        output.close()

    //runMalletInferring()

    val tmpDir = "src/main/resources/"
    logger.debug("Import all documents to mallet...")
    Text2Vectors.main(("--input " + tmpDir + "new_corpus/ --keep-sequence --remove-stopwords " + "--output " + tmpDir + "new_corpus.mallet --use-pipe-from " + tmpDir + "corpus.mallet").split(" "))
    logger.debug("Run training process...")
    InferTopics.main(("--input " + tmpDir + "new_corpus.mallet --inferencer " + tmpDir + "inferencer " + "--output-doc-topics " + tmpDir + "doc-topics-new.txt --num-iterations 1000").split(" "))
    logger.debug("Inferring process finished.")

I am the getting error while calling text2Vector.main , the new vectorized file is getting created in new_corpus directory and new_corpus is getting generated as well. however after that I am getting the below error

Server online at http://127.0.0.1:9000/
12:45:38.622 [Sophi-Mallet-Api-akka.actor.default-dispatcher-3] DEBUG io.sophi.api.mallet.actors.MalletActor$ - run count...
12:45:38.634 [Sophi-Mallet-Api-akka.actor.default-dispatcher-3] DEBUG io.sophi.api.mallet.actors.MalletActor$ - Import all documents to mallet...
Couldn't open cc.mallet.util.MalletLogger resources/logging.properties file.
 Perhaps the 'resources' directories weren't copied into the 'class' directory.
 Continuing.
May 26, 2019 12:45:38 PM cc.mallet.classify.tui.Text2Vectors main
INFO: Labels =
May 26, 2019 12:45:38 PM cc.mallet.classify.tui.Text2Vectors main
INFO:    src/main/resources/new_corpus/
May 26, 2019 12:45:46 PM cc.mallet.classify.tui.Text2Vectors main
INFO:  rewriting previous instance list, with ID = 4e0a5a65540221c3:d508579:14b2ca15a26:-7ff7
[INFO] [05/26/2019 12:45:58.476] [Sophi-Mallet-Api-akka.actor.default-dispatcher-4] [akka.actor.ActorSystemImpl(Sophi-Mallet-Api)] Request timeout encountered for request [GET /mallet Empty]

in web browser as well I am getting the error

The server was not able to produce a timely response to your request.
Please try again in a short while!

Body:
I figure out the solution myself. Please correct me if it still is vague.

There were couple of change needed.
First in application configuration file (application.conf)

below code needed to be updated.

server{
    idle-timeout = infinite
    request-timeout = infinite
}
settings {
  akka-workers-count = 100
  akka-workers-count = ${?AKKA_WORKERS_COUNT}
  actor-timeout = 100
  actor-timeout = ${?ACTOR_TIMEOUT}
}

And the issue is needed to be handled in server code
by putting the api call inside timeout response

  val timeoutResponse = HttpResponse(
    StatusCodes.EnhanceYourCalm,
    entity = "Running Mallet modelling.")

  lazy val apiRoutes: Route =
    ignoreTrailingSlash {
      pathSingleSlash {
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<html><body>Hello world!</body></html>"))
      } ~
        path("health") {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "ok"))
        } ~
        pathPrefix("mallet") {
          withRequestTimeout(5.minute, request => timeoutResponse) {
          parameter('malletFile) { malletFile => {
            val modelFile: Future[MalletModel] =
              (malletActor ? GetMalletOutput(malletFile)).mapTo[MalletModel]
            complete(modelFile)
          }
          }

          }
        }
    }


问:
Name Matching in python
We have a third party 'tool' which finds similar names and assigns a similarity score between two names.

I am supposed to mimic the tool's behavior as closely as possible.
After searching over internet, gave a shot at distance method.Used fuzzywuzzy for the same.

```<code>matches = process.extractBests(
    name,
    choices,
    score_cutoff=50,
    scorer=fuzz.token_sort_ratio,
    limit=1
);
</code>```

It gave results close to the tool result.However there are few outliers - as highlighted below.

After further searches over internet, I have come to the understand that further refinement will need implementation of machine learning of sort. I am a complete newbie in the machine learning world - so seeking some advice as to where I should attempt at next for further code refinement.

Thanks!

答: Take a look at the Jaccard and Levenshtein algorithms for fuzzy string matching. Both are relatively simple and can be implemented in about 40 or 50 lines of code.</p>

Title: Name Matching in python

Body:
We have a third party 'tool' which finds similar names and assigns a similarity score between two names.

I am supposed to mimic the tool's behavior as closely as possible.
After searching over internet, gave a shot at distance method.Used fuzzywuzzy for the same.

matches = process.extractBests(
    name,
    choices,
    score_cutoff=50,
    scorer=fuzz.token_sort_ratio,
    limit=1
);

It gave results close to the tool result.However there are few outliers - as highlighted below.

After further searches over internet, I have come to the understand that further refinement will need implementation of machine learning of sort. I am a complete newbie in the machine learning world - so seeking some advice as to where I should attempt at next for further code refinement.

Thanks!