Extract data from txt file to use in autocomplete

8
  

Update: Just for the record, you do not need to use jQuery-ui, it might be another solution.

I'm creating a autocomplete for a form, which should include all the options of the CBO - Brazilian Code of Occupations

Unlike the zip code, that does not have a free official database , in the MTE Download Area there is a txt file, where the professions and the code of each are listed (inside the link page above it has the link: Estrutura CBO (TXT) - Arquivo ZIP (106kb) - is the fourth file, CBO2002-Ocupacao.txt ).

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

I've already created autocomplete , using the JQuery-ui plugin, but I got discouraged from writing all the professions and codes in the nail and here I am.

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

This is autocomplete (I put all the external links and it works in "execute 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><linkhref="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><formmethod="post" id="Cpost" name="Tpost">

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

</form>

So, in short, the doubts are as follows:

1 - Is it possible to create the code automatically from the .txt (or html) file? How to do this?

2 - How to make loading options not slow.

Updating: I still can not, I even made another file with the line breaks (it seems that what can be downloaded in the site does not have line breaks) to test, but I still can not do it script to work. Getting, put the result here, but if anyone has another idea I'm accepting it too. :)

Update: I converted the pdf to html file, and it got the following structure (the whole file has over 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>

Does this help?

/ upate

    
asked by anonymous 10.04.2015 / 18:16

3 answers

4

The following code generates the CBO vector with exactly the same structure as the example you presented in the question, and then adds that 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
    });
});
    
07.07.2015 / 18:19
6

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

Here is the TXT snippet 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 turn this file into 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  });
});
    
10.04.2015 / 21:02
4

You can manipulate the txt file by parsing each line, and saving them in an array, or by concatenating a single lineable string for each line returned.

something like this

$(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 to each row and store in variables.  your code:

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

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

 });

Considering that txt is with each record in a row.

    
10.04.2015 / 19:15