Skip to content

Commit

Permalink
Macron For Names
Browse files Browse the repository at this point in the history
Macron For Names
  • Loading branch information
maxtyson123 committed Oct 29, 2023
1 parent da51ea0 commit 35b8017
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 63 deletions.
109 changes: 53 additions & 56 deletions website/src/lib/plant_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,18 +804,19 @@ export function fixAttachmentsPaths (plant: PlantData) {
return plant;
}

function convertMacron(str: string) {
return str
.replaceAll("macron_a", "ā")
.replaceAll("macron_e", "ē")
.replaceAll("macron_i", "ī")
.replaceAll("macron_o", "ō")
.replaceAll("macron_u", "ū")
.replaceAll("macron_A", "Ā")
.replaceAll("macron_E", "Ē")
.replaceAll("macron_I", "Ī")
.replaceAll("macron_O", "Ō")
.replaceAll("macron_U", "Ū")
export const macronDictionary = ["ā", "ē", "ī", "ō", "ū", "Ā", "Ē", "Ī", "Ō", "Ū"]
export const numberDictionary = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
export const textDictionary = ["macron_a", "macron_e", "macron_i", "macron_o", "macron_u", "macron_A", "macron_E", "macron_I", "macron_O", "macron_U"]


export function macronCodeToChar(str: string, dict = textDictionary) {
let string = str

for(let i = 0; i < dict.length; i++) {
string = string.replaceAll(dict[i], macronDictionary[i])
}

return string
}

/**
Expand All @@ -826,40 +827,40 @@ function convertMacron(str: string) {
*/
export function macronsForDisplay(plant: PlantData) {

plant.english_name = convertMacron(plant.english_name)
plant.maori_name = convertMacron(plant.maori_name)
plant.latin_name = convertMacron(plant.latin_name)
plant.long_description = convertMacron(plant.long_description)
plant.small_description = convertMacron(plant.small_description)
plant.english_name = macronCodeToChar(plant.english_name, numberDictionary)
plant.maori_name = macronCodeToChar(plant.maori_name, numberDictionary)
plant.latin_name = macronCodeToChar(plant.latin_name, numberDictionary)
plant.long_description = macronCodeToChar(plant.long_description)
plant.small_description = macronCodeToChar(plant.small_description)

// Loop through the sections
for (let i = 0; i < plant.sections.length; i++) {

switch (plant.sections[i].type) {
case "edible":
plant.sections[i].part_of_plant = convertMacron(plant.sections[i].part_of_plant)
plant.sections[i].nutrition = convertMacron(plant.sections[i].nutrition)
plant.sections[i].preparation = convertMacron(plant.sections[i].preparation)
plant.sections[i].part_of_plant = macronCodeToChar(plant.sections[i].part_of_plant)
plant.sections[i].nutrition = macronCodeToChar(plant.sections[i].nutrition)
plant.sections[i].preparation = macronCodeToChar(plant.sections[i].preparation)
break;

case "medical":
plant.sections[i].use = convertMacron(plant.sections[i].use)
plant.sections[i].preparation = convertMacron(plant.sections[i].preparation)
plant.sections[i].use = macronCodeToChar(plant.sections[i].use)
plant.sections[i].preparation = macronCodeToChar(plant.sections[i].preparation)
break;

case "craft":
plant.sections[i].part_of_plant = convertMacron(plant.sections[i].part_of_plant)
plant.sections[i].use = convertMacron(plant.sections[i].use)
plant.sections[i].additonal_info = convertMacron(plant.sections[i].additonal_info)
plant.sections[i].part_of_plant = macronCodeToChar(plant.sections[i].part_of_plant)
plant.sections[i].use = macronCodeToChar(plant.sections[i].use)
plant.sections[i].additonal_info = macronCodeToChar(plant.sections[i].additonal_info)
break;

case "custom":
plant.sections[i].title = convertMacron(plant.sections[i].title)
plant.sections[i].text = convertMacron(plant.sections[i].text)
plant.sections[i].title = macronCodeToChar(plant.sections[i].title)
plant.sections[i].text = macronCodeToChar(plant.sections[i].text)
break;

case "source":
plant.sections[i].data = convertMacron(plant.sections[i].data)
plant.sections[i].data = macronCodeToChar(plant.sections[i].data)
break;

default:
Expand All @@ -870,18 +871,14 @@ export function macronsForDisplay(plant: PlantData) {
return plant;
}

export function escapeMacron(str: string) {
return str
.replaceAll("ā", "macron_a")
.replaceAll("ē", "macron_e")
.replaceAll("ī", "macron_i")
.replaceAll("ō", "macron_o")
.replaceAll("ū", "macron_u")
.replaceAll("Ā", "macron_A")
.replaceAll("Ē", "macron_E")
.replaceAll("Ī", "macron_I")
.replaceAll("Ō", "macron_O")
.replaceAll("Ū", "macron_U")
export function macronCharToCode(str: string, dict = textDictionary) {
let string = str

for(let i = 0; i < dict.length; i++) {
string = string.replaceAll(macronDictionary[i], dict[i])
}

return string
}

/**
Expand All @@ -892,40 +889,40 @@ export function escapeMacron(str: string) {
*/
export function macronsForDatabase(plant: PlantData){

plant.english_name = escapeMacron(plant.english_name)
plant.maori_name = escapeMacron(plant.maori_name)
plant.latin_name = escapeMacron(plant.latin_name)
plant.long_description = escapeMacron(plant.long_description)
plant.small_description = escapeMacron(plant.small_description)
plant.english_name = macronCharToCode(plant.english_name, numberDictionary)
plant.maori_name = macronCharToCode(plant.maori_name, numberDictionary)
plant.latin_name = macronCharToCode(plant.latin_name, numberDictionary)
plant.long_description = macronCharToCode(plant.long_description)
plant.small_description = macronCharToCode(plant.small_description)

// Loop through the sections
for (let i = 0; i < plant.sections.length; i++) {

switch (plant.sections[i].type) {
case "edible":
plant.sections[i].part_of_plant = escapeMacron(plant.sections[i].part_of_plant)
plant.sections[i].nutrition = escapeMacron(plant.sections[i].nutrition)
plant.sections[i].preparation = escapeMacron(plant.sections[i].preparation)
plant.sections[i].part_of_plant = macronCharToCode(plant.sections[i].part_of_plant)
plant.sections[i].nutrition = macronCharToCode(plant.sections[i].nutrition)
plant.sections[i].preparation = macronCharToCode(plant.sections[i].preparation)
break;

case "medical":
plant.sections[i].use = escapeMacron(plant.sections[i].use)
plant.sections[i].preparation = escapeMacron(plant.sections[i].preparation)
plant.sections[i].use = macronCharToCode(plant.sections[i].use)
plant.sections[i].preparation = macronCharToCode(plant.sections[i].preparation)
break;

case "craft":
plant.sections[i].part_of_plant = escapeMacron(plant.sections[i].part_of_plant)
plant.sections[i].use = escapeMacron(plant.sections[i].use)
plant.sections[i].additonal_info = escapeMacron(plant.sections[i].additonal_info)
plant.sections[i].part_of_plant = macronCharToCode(plant.sections[i].part_of_plant)
plant.sections[i].use = macronCharToCode(plant.sections[i].use)
plant.sections[i].additonal_info = macronCharToCode(plant.sections[i].additonal_info)
break;

case "custom":
plant.sections[i].title = escapeMacron(plant.sections[i].title)
plant.sections[i].text = escapeMacron(plant.sections[i].text)
plant.sections[i].title = macronCharToCode(plant.sections[i].title)
plant.sections[i].text = macronCharToCode(plant.sections[i].text)
break;

case "source":
plant.sections[i].data = escapeMacron(plant.sections[i].data)
plant.sections[i].data = macronCharToCode(plant.sections[i].data)
break;

default:
Expand Down
13 changes: 10 additions & 3 deletions website/src/pages/api/plants/search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {NextApiRequest, NextApiResponse} from 'next';
import {getClient, getTables, makeQuery} from "@/lib/databse";
import {escapeMacron} from "@/lib/plant_data";

export default async function handler(
request: NextApiRequest,
Expand Down Expand Up @@ -46,8 +45,16 @@ export default async function handler(

// Select what the user entered
if (name) {
name = escapeMacron(name as string)
query += ` ${selector} (english_name LIKE '%${name}%' OR maori_name LIKE '%${name}%' OR latin_name LIKE '%${name}%')`;

let replaceChars = ["ā", "ē", "ī", "ō", "ū", "Ā", "Ē", "Ī", "Ō", "Ū", "a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]

// Replace macrons with wildcard
for (let i = 0; i < replaceChars.length; i++) {
name = (name as string).replaceAll(replaceChars[i], `_`)
}


query += ` ${selector} (english_name LIKE '${name}%' OR maori_name LIKE '${name}%' OR latin_name LIKE '${name}%')`;
selector = "AND";
}

Expand Down
8 changes: 4 additions & 4 deletions website/src/pages/plant_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import styles from "@/styles/plant_index.module.css";
import axios from "axios";
import Stats from "@/components/stats";
import {DropdownInput} from "@/components/input_sections";
import {escapeMacron, getNamesInPreference} from "@/lib/plant_data";
import {getNamesInPreference, macronCodeToChar, numberDictionary} from "@/lib/plant_data";

interface plantEntry {
id: number,
Expand Down Expand Up @@ -102,17 +102,17 @@ export default function PlantIndex(){

// If there is an english name then add it to the array
if (data[i].english_name !== null) {
ids.push({id: data[i].id, name: escapeMacron(data[i].english_name), tag: "english"})
ids.push({id: data[i].id, name: macronCodeToChar(data[i].english_name, numberDictionary), tag: "english"})
}

// If there is a maori name then add it to the array
if (data[i].maori_name !== null) {
ids.push({id: data[i].id, name: escapeMacron(data[i].maori_name), tag: "maori"})
ids.push({id: data[i].id, name: macronCodeToChar(data[i].maori_name, numberDictionary), tag: "maori"})
}

// If there is a latin name then add it to the array
if (data[i].latin_name !== null) {
ids.push({id: data[i].id, name: escapeMacron(data[i].latin_name), tag: "latin"})
ids.push({id: data[i].id, name: macronCodeToChar(data[i].latin_name, numberDictionary), tag: "latin"})
}

}
Expand Down

1 comment on commit 35b8017

@vercel
Copy link

@vercel vercel bot commented on 35b8017 Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.