Extract data from txt file to use in autocomplete

Asked

Viewed 2,830 times

9

Update: Just for the record, there is no need to use jQuery-ui, it can be with another solution.

I’m creating a autocomplete for a form, containing all the options in the CBO - Brazilian Code of Occupations

Other than CEP, which does not have an official free base, in MTE download area there is a file txt, where the professions and the code of each one are listed (inside the page of the link above has the link: Estrutura CBO (TXT) - Arquivo ZIP (106kb) - is the fourth archive, CBO2002-Ocupacao.txt).

So what I want to do is take this file and generate the code automatically, including the professions and the respective CBO numbers in the file. js (as in the full example below). This is possible?

I’ve already created the autocomplete, using the plugin Jquery-ui, but I got discouraged from writing all the professions and codes in my fingernail and here I am.

In addition, I need the code not to be too heavy and slow to load. What is the best way to do this? With database, everything in the file . js?

This is the autocomplete (put all external links and is working on "run code snippet"):

$(function() {

    var ocupacao = [

        "Abacaxicultor (CBO 6125-10)",
        "Abade (CBO 2631-05)",
        "Abadessa (CBO 2631-05)",
        "Abanador na agricultura (CBO 6220-20)",
        "Abastecedor de caldeira (CBO 8621-20)",
        "Abastecedor de linha de produção (CBO 7842-05)",
        "Abastecedor de máquinas de linha de produção (CBO 7842-05)",
        "Abastecedor de silos de carvão (CBO 6326-05)",
        "Abatedor (CBO 8485-05)"
    ];
    $("#profiss"). autocomplete({
        source:ocupacao
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.11.4/themes/start/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>

<form method="post" id="Cpost" name="Tpost">

<input type="text" id="profiss" placeholder="Informe a profissão"/>

</form>

So, long story short, the questions are as follows::

1 - Can you create the code automatically, from the . txt (or html) file? How to do this?

2 - How to make the loading of options not slow.

Updating: I still could not, I even made another file with the line breaks (it seems that what you can download on the site has no line breaks) to test, but I still could not make the script work. Succeeding, put the result here, but if anyone has another idea I’m accepting too. :)

Update: I converted the pdf into html file, and it got the following structure (the whole file has more 65k lines):

<DIV id="id_1">
<DIV id="id_1_1">
<TABLE cellpadding=0 cellspacing=0 class="t2">
<TR>
	<TD class="tr5 td4"><P class="p9 ft2"><NOBR>7681-25</NOBR></P></TD>
	<TD class="tr5 td5"><P class="p10 ft3">Acabador de chapéus de palha</P></TD>
</TR>
<TR>
	<TD class="tr6 td4"><P class="p9 ft2"><NOBR>7663-05</NOBR></P></TD>
	<TD class="tr6 td5"><P class="p10 ft4">Acabador de embalagens (flexíveis e cartotécnicas)</P></TD>
</TR>
</TABLE>
</DIV>

That helps?

/upate

  • 2

    Gustavo, why not use a json file that is much more "malleable"?

  • 1

    I go there to find what is Json and come back. : D But I think it can be yes...

  • Quiet, it can be yes (who sees thinks I learned Json in two minutes, kkk, but I only saw that will give to understand after studying a little)...

  • A json is "basically" an object (or array, or object array, or object of arrays, etc...) in an external file, which you can load with ajax in javascript, or work with it in any other language, such as PHP, etc....

  • But then just save txt with the extension . json?

  • 1

    For the record, we continue this conversation on javascript chat, and it’s come a long way, but it hasn’t been solved yet...

Show 1 more comment

3 answers

7

In the link you sent you can download a TXT, with it is much simpler to work.

Follow the text of the TXT in question:

CBO2002 - Occupation

Codigo Titulo
------ ---------------------------------------------------------
010115 Oficial general da marinha
010110 Oficial general do exército
010105 Oficial general da aeronáutica
010210 Oficial do exército
010215 Oficial da marinha
010205 Oficial da aeronáutica
010315 Praça da marinha
010310 Praça do exército
010305 Praça da aeronáutica
020105 Coronel da polícia militar
020110 Tenente-coronel da polícia militar
020115 Major da polícia militar

you can transform this file into the source for your autocomplete as follows:

$.get('CBO2002 - Ocupacao.txt', function ()
    var linhas = arquivo.split('\n');
    var source = linhas.map(function(linha, indice) {
        if (indice >= 2) {
            var ocupacao = {
                Familia: linha.substring(0, 4),
                Codigo: linha.substring(4, 6),
                Titulo: linha.substring(7),
            };    
            return ocupacao.Titulo + " (CBO " + ocupacao.Familia + "-" + ocupacao.Codigo + ")";
        }
    });
    $("#profiss"). autocomplete({ source: source  });
});
  • So this file you downloaded is part of the CBO (group) structure. What I need is the complete listing, which is in the file .pdf. I was able to convert it to txt, but it wasn’t the same as the one you downloaded. Each profession and code are in separate lines, and there are other words (but this can be solved easily, just do a search and delete what doesn’t matter). I will start testing here the script you passed, and update here with progress. Thanks! + rep

  • There are 2 TXT files, the file 4 has all occupations, while the 3 the synonyms.

  • It’s true, you’re right! Which is embarrassing, because it’s something I should know... This should make it even easier, because the converted PDF file had 65k lines. I’m trying to run the script here, but it’s not working. I put between the tags head, or inside the form, or in an external file, and it seems that he is not finding the file (which is in the same folder as html)... I also changed the name of the file to avoid spaces, but I think that was not tbm... but I’m testing here, as soon as I get the score across... Thanks!

  • When inspecting by browser I get this error for the second line of the script: Uncaught Referenceerror: file is not defined. I already followed this tip: http://fellipeguimaraes.com.br/tag/uncaught-referenceerror-is-not-defined/ but it didn’t solve either. And it also has this error: GET http://localhost:63342/site/public_html/_javascript/jquery.min.map 404 (Not Found)

  • Strange, try to access the TXT file through an AJAX request.

  • Using ajax (only change get by ajax right?) the first error is gone, it is now just this: GET jquery.min.map:1http://localhost:63342/site/public_html/_javascript/jquery.min.map 404 (Not Found) This jquery.min.map file doesn’t exist at all, do I have to create it? The only reference to . map is on line 3 of the code. Is it for him to create this file? can it be permission error? Could it be some library missing? Thanks for the patience.

  • The $.get method only makes a call to the $.ajax method with a few pre-configured options, the same goes for $.post and others.

  • Try to access the txt file directly from the browser, type localhost:portNumber/filename.txt, can you access it? Otherwise, tell me the error.

  • I can open normal txt. Following a Soen topic (http://stackoverflow.com/questions/18365315/jquerys-jquery-1-10-2-min-map-triggering-a-404-not-found), I installed jquery.min.map (which appears as json on webstorm, is that right?), and Linkei it in html. Now it’s giving a different error: Navigated to http://localhost:63342/site/public_html/_javascript/autocomplete.html Uncaught Syntaxerror: Unexpected token: jquery-2.1.3.min. map:1 XHR finished loading: GET . jquery-2.1.3.js:8625 "http://localhost:63342/site/public_html/_javascript/occupation.txt"

  • I found this other topic, but my English is terrible: http://stackoverflow.com/questions/3143698/uncaught-syntaxerror-unexpected-token Now I think he’s found the txt file, but the autocomplete hasn’t worked yet...

  • Just one more update, because I don’t know when I will look for this solution: using 'ajaxTransport', I don’t get any error inspecting through the browser,.

  • So, trying to manipulate the file, I realized that despite the appearance, it does not have a record on each line. Could that be what’s making the script not work? Is there any way to fix it? Thanks.

  • You can put some example that has more than one record per line?

  • Then, I created another txt file with the separate lines but it did not help, the error continues: XHR finished loading: GET jquery-2.1.3.js:8625 "http://localhost:63342/site/public_html/_javascript/occupation.txt". PS: The jquery 8625 line is xhr.send( options.hasContent && options.data || null );

Show 9 more comments

5

Voce can manipulate the txt file by parsing each line and saving it in an array, or concatenating into a single scan each line returned.

something like that

$(function(){
       $.get('file.txt',function(data){
              var contents = $.trim(data).split(/\n/);
              console.log(contents.shift());
       });
});

Now just manipulate each Contents[x] q will be each line of the file and then, if necessary separate profession/value, use a "," or pipe"|" and give another split for each line and store in variables. your code:

$(function() {
$.get('file.txt',function(data){
              var ocupacao= $.trim(data).split(/\n/);
 });

     $("#profiss"). autocomplete({
        source:ocupacao
    });

 });

this considering that txt has each record in a row.

  • So, I’m still a beginner, it seemed very complicated for me... I’m studying the parse here, but I didn’t understand one thing: would I have to edit the entire txt file, in each line? I managed to check the pdf in html, if you can take a look, I edited the op including a snippet of html. See if this can help. Thanks.

  • in the above example, I split in n, so the variable Contents has become an array, i.e., it has stored each line in a position, Contents[0] q has in line 1, Contents[1] what it has in line 2 etc... if it replaces the console.log with "Alert(Contents[0]);" it will show in an alert the entire line 1 of the file, and so on, in case if Voce has a text file with the lines as in the example above, Voce can use the Contents as source:Contents so it is easier. ai it understands q each line of your txt is a line of your array, just loading the file and giving a split on n.

  • Thanks for editing the answer, it was clearer. + rep

  • So, trying to manipulate the file, I realized that despite the appearance, it does not have a record on each line. Could this be what’s making the script not work? Is there any way to fix it? Thanks. -

  • only seeing even... depending on the OS may be necessary n r

5


The following code generates the Cbos vector with exactly the same structure as the example you presented in the question, and then adds this vector as the autocomplete data source.

$.get('ArquivoCbo.txt', function (conteudoDoArquivo) {
    var linhas = conteudoDoArquivo.split('\n');

    var profissoes = new Array();

    for (i = 2; i < linhas.length; i++) {
        var linha = linhas[i];
        var codigoDaProfissao = linha.split(" ")[0];
        var descricaoDaProfissao = linha.replace(codigoDaProfissao, "").trim();

        profissoes.push(descricaoDaProfissao + " (CBO " + codigoDaProfissao + ")");
    }

    $("#profiss"). autocomplete({
        source:profissoes
    });
});
  • So, it worked! It was missing close a parentese in profissoes.push(descricaoDaProfissao + " (" + codigoDaProfissao + ")"); (the latter) and I followed my IDE’s suggestion to simplify new Array() to just []... worked great, really worth!

  • 1

    It’s true, now that I noticed it. The parentheses inside the quote at the end of the line made me forget to close with the push parenthesis. Thanks for letting me know. Taking advantage, @gustavox, being that my solution worked, could mark it as the answer to the question, to help other people when researching this same problem. Thank you. :)

  • Cool, I was just letting you know in chat before, because there were other users trying to fix it too... I’ve already marked how you accept and granted the reward, thank you very much! Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.