API & Code examples
API
This page shows how to use the BourdieuVectors API with an example POST request for the cultural event "american football" and how to expand all vector fields into separate columns.
Attention: Note that the request limit is 500 vectors / h for unknown vectors or vectors with context. If you need to process a lot of vectors please reach out to me.
How to get a vector
The following POST request returns a vector. It is generated when the input is unknown to the system or returned if already known. When you provide a context the vector is always generated once again.
POST https://api.bourdieuvectors.com/api/v1/vectors/bourdieuvectors Content-Type: application/json{
"cultural_event": "american football",
"cultural_event_context": "watching it"
}Response
{
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"cultural_event": "american football",
"vector": {
"cultural_capital_cultural_art": 0.1,
"cultural_capital_education": 0.2,
...
},
"vector_array": [
0.1,
...
],
"vector_size": 76,
"model_name": "bourdieuvectors",
"model_version": "1.0.6"
}Code
Example R code (POST)
library(httr)
library(jsonlite)
library(dplyr)
data <- data.frame(
cultural_event = c("american football"),
stringsAsFactors = FALSE
)
get_vector <- function(event_name) {
url <- "https://api.bourdieuvectors.com/api/v1/vectors/bourdieuvectors"
response <- POST(url, body = list(cultural_event = event_name), encode = "json")
if (status_code(response) == 200) {
content(response, as = "parsed", type = "application/json")
} else {
warning(paste("Failed to fetch for event:", event_name))
return(NULL)
}
}
results <- data %>%
rowwise() %>%
mutate(api_response = list(get_vector(cultural_event))) %>%
mutate(across(api_response, ~ if(!is.null(.)) as_tibble(.x$vector) else NULL))
resultsExample Python code (POST)
Requirement:
pip install bourdieuvectors pandas
Code
import requests
import pandas as pd
import hashlib
from bourdieuvectors import get_bourdieu_vector, save_bourdieu_vector_cache, load_bourdieu_vector_cache
data = pd.DataFrame({
"cultural_event": ["american football"]
})
vector_df = data["cultural_event"].apply(lambda x: get_bourdieu_vector(x, model='bourdieuvectors')).apply(pd.Series)
data_expanded = pd.concat([data, vector_df], axis=1)
# Optionally, save the cache so that people can restore your vectors
# Save: save_bourdieu_vector_cache('cached_vectors.zip', 'bourdieuvectors')
# Restore: load_bourdieu_vector_cache('cached_vectors.zip', 'bourdieuvectors')
print(data_expanded)
data_expanded.to_csv("bourdieuvectors_data.csv")