mcp-turkce
Version:
Model Context Protocol Server for Turkish Language Foundation API
119 lines (118 loc) • 4.51 kB
JavaScript
// TurkishDictionaryAPI.ts
import { TurkishDialect, PersonNameType, PersonNameSearchType, ScienceArtDict, } from "./types.js";
export class TurkishDictionaryAPI {
baseUrl = "https://sozluk.gov.tr";
async makeRequest(endpoint, searchTerm, params = {}) {
let url = `${this.baseUrl}${endpoint}${encodeURIComponent(searchTerm)}`;
const urlParams = new URLSearchParams(params).toString();
if (urlParams) {
url += (url.includes("?") ? "&" : "?") + urlParams;
}
try {
const response = await fetch(url);
if (!response.ok)
throw new Error(`API error: ${response.status}`);
return (await response.json());
}
catch (error) {
console.error("Request error:", error);
throw error;
}
}
// Main dictionary
async searchWord(word) {
return this.makeRequest("/gts?ara=", word);
}
// Western-origin words
async searchWesternOrigin(word) {
return this.makeRequest("/bati?ara=", word);
}
// Scanning dictionary
async searchScanning(word) {
return this.makeRequest("/tarama?ara=", word);
}
// Compilation dictionary
async searchCompilation(word) {
return this.makeRequest("/derleme?ara=", word);
}
// Proverbs and idioms
async searchProverbsAndIdioms(word) {
return this.makeRequest("/atasozu?ara=", word);
}
// Foreign words guide
async searchForeignWordsGuide(word) {
return this.makeRequest("/kilavuz?prm=ysk&ara=", word);
}
// Eren Etymology Dictionary
async searchErenEtymology(word) {
return this.makeRequest("/etms?ara=", word);
}
// Origin Information Dictionary
async searchOriginInfo(word) {
return this.makeRequest("/etimoloji?ara=", word);
}
// Turkish Sign Language
async searchSignLanguage(word) {
return this.makeRequest("/tid?ara=", word);
}
// Person Names Dictionary
async searchPersonNames(term, type, searchType) {
const endpoint = searchType === PersonNameSearchType.BY_NAME
? `/adlar?gore=1&cins=${type}&ara=`
: `/adlar?gore=2&cins=${type}&ara=`;
return this.makeRequest(endpoint, term);
}
// Simplified versions of person name searches
async searchMaleNames(name) {
return this.searchPersonNames(name, PersonNameType.MALE, PersonNameSearchType.BY_NAME);
}
async searchFemaleNames(name) {
return this.searchPersonNames(name, PersonNameType.FEMALE, PersonNameSearchType.BY_NAME);
}
async searchUnisexNames(name) {
return this.searchPersonNames(name, PersonNameType.UNISEX, PersonNameSearchType.BY_NAME);
}
async searchByNameMeaning(meaning, type) {
return this.searchPersonNames(meaning, type, PersonNameSearchType.BY_MEANING);
}
// Turkish Dialects Dictionary
async searchDialects(word, dialect = TurkishDialect.AZERBAIJANI) {
return this.makeRequest("/lehceler?ara=", word, {
lehce: dialect.toString(),
});
}
// Science and Art Terminology Dictionary
async searchScienceArt(word, dictionary = ScienceArtDict.ALL) {
let endpoint = "/terim?eser_ad=tümü&ara=";
// Special cases for some dictionaries
if (dictionary === ScienceArtDict.NURSING) {
return this.makeRequest("/hemsirelik?ara=", word);
}
else if (dictionary === ScienceArtDict.VETERINARY) {
return this.makeRequest("/veterinerlik?ara=", word);
}
else if (dictionary === ScienceArtDict.LOGIC ||
dictionary === ScienceArtDict.METROLOGY) {
// These might have special endpoints too
endpoint = "/terim?eser_ad=tümü&ara=";
}
return this.makeRequest(endpoint, word);
}
// Simplified methods for common science dictionaries
async searchNursing(word) {
return this.searchScienceArt(word, ScienceArtDict.NURSING);
}
async searchPharmacy(word) {
return this.searchScienceArt(word, ScienceArtDict.PHARMACY);
}
async searchMetrology(word) {
return this.searchScienceArt(word, ScienceArtDict.METROLOGY);
}
async searchVeterinary(word) {
return this.searchScienceArt(word, ScienceArtDict.VETERINARY);
}
// Search all terminology
async searchAllTerminology(word) {
return this.searchScienceArt(word, ScienceArtDict.ALL);
}
}