Skip to content

Instantly share code, notes, and snippets.

View jjesusfilho's full-sized avatar
🏠
Working from home

José de Jesus Filho jjesusfilho

🏠
Working from home
View GitHub Profile
@jjesusfilho
jjesusfilho / dir_size.R
Created May 1, 2024 07:42
Retorna o tamanho dos diretórios
dir_size <- function(x){
purrr::map_dfr(x, purrr::possibly(~{
system(glue::glue("du -s {.x}"), intern = T) |>
stringr::str_split_1("\\s+") |>
setNames(c("tamanho","diretorio"))
},NULL)) |>
dplyr::mutate(tamanho = as.numeric(tamanho))
ibge_ftp <- paste0("https://ftp.ibge.gov.br/Trabalho_e_Rendimento/",
"Pesquisa_Nacional_por_Amostra_de_Domicilios_anual/",
"microdados/")
r1 <- ibge_ftp |>
httr2::request() |>
httr2::req_perform() |>
httr2::resp_body_html()
@jjesusfilho
jjesusfilho / busca_fuzzy.R
Last active January 30, 2024 16:12
Busca_fuzzy.R procura a palavra ou frase do segundo vetor que melhor se aproxima do primeiro. Particularmente útil para corrigir nomes de municípios.
#' Procura a palavra ou frase do segundo vetor que melhor
#' se aproxima do primeiro. Particularmente útil para
#' corrigir nomes de municípios.
#'
#' @param x Vetor de strings de referência.
#' @param y Vetor de strings a serem buscados.
#'
#' @return vetor com as strings de y próximos
#' de x.
#' @export
@jjesusfilho
jjesusfilho / compatibilizar_chromever.R
Last active January 26, 2024 20:54
Compatibiliza o chromdriver com o google chrome
#' Baixa e atualiza versão do chromedriver com a versão do chrome.
#'
#' @return OK!
#' @export
#'
compatibilizar_chromever <- function(){
driver <- binman::list_versions("chromedriver") |>
unlist() |>
utils::tail(1) |>
@jjesusfilho
jjesusfilho / toTitleCasePT.R
Last active January 18, 2024 17:33
Converts Portuguese strings to Title Case
#' Convertion to title case with lower case for some classes of words.
#'
#' In written Portuguese, when converting to title case, it is not usual
#' to keep in title case some words, like prepositions, conjunctions,
#' articles and some kinds of pronouns. This functions locates those
#' cases and converts them to lower case.
#'
#' @param string vector of characters to be converted to title case
#' but with connector words (one-word prepositions and conjunctions)
#' and articles (both definite and indefinite) and some pronouns
@jjesusfilho
jjesusfilho / pg_pivot_wider.R
Last active January 18, 2024 11:28
Converts PostgreSQL table from long to wide format from R
#' Converts PostgreSQL table from long to wide format
#'
#' @param conn Connection
#' @param tbl Table
#' @param new_tbl Optional. It creates a new table in the database
#' instead of importing it to R
#' @param id_cols String vector with the name of columns to be used as identities
#' @param names_from Column whose values will become variables
#' @param values_from Column whose values will populate the new variables
#'
@jjesusfilho
jjesusfilho / openai_contar_tokens.R
Created January 10, 2024 17:16
Chama o tiktoken do python para contar os tokens.
#' Conta tokens de textos com base em modelos da OPENAI
#'
#' @param x Vetor de textos
#' @param modelo Modelo a ser utilizado.
#'
#' @details Para usar esta função você tem de ter instalado o pacote
#' tiktoken do Python, o qual será chamado via reticulate.
#'
#' @return Vetor com quantidade de tokens em cada texto.
#' @export
@jjesusfilho
jjesusfilho / msql_colacao.R
Last active December 18, 2023 20:28
Aplica colação ao SQL server para ignorar acentuação e caixa #'
msql_colacao <- function(conn, schema = NULL, table = NULL, ...){
dots <- rlang::ensyms(...) |>
purrr::map_chr(rlang::as_string)
### Verifica se as colunas existem
q <- glue::glue_sql("select top 0 * from {`schema`}.{`table`}", .con = conn)
@jjesusfilho
jjesusfilho / Oscar_bot.py
Created November 24, 2023 16:30 — forked from janakiramm/Oscar_bot.py
Implementing RAG with OpenAI
import openai
import tiktoken
from scipy import spatial
import pandas as pd
df=pd.read_csv('./data/oscars.csv')
print(df.head())
df=df.loc[df['year_ceremony'] == 2023]
df=df.dropna(subset=['film'])
@jjesusfilho
jjesusfilho / mni.py
Last active November 1, 2023 15:04
Consulta processo do tjsp via mni
from zeep import Client
import xmltodict
import json
from dotenv import load_dotenv
import os
import getpass
def consultar_processo(
usuario= None,