tdk-api-wrapper 1.2.2 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist/{chunk-SNY3KUCF.mjs → chunk-ACMGCL7T.mjs} +294 -1
- package/dist/cli.js +339 -3
- package/dist/cli.mjs +46 -3
- package/dist/index.d.mts +96 -1
- package/dist/index.d.ts +96 -1
- package/dist/index.js +294 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
- package/src/cli.ts +41 -1
- package/src/tdk.ts +301 -1
- package/src/types.ts +10 -0
package/src/tdk.ts
CHANGED
|
@@ -7,12 +7,15 @@ import type {
|
|
|
7
7
|
WordComparison,
|
|
8
8
|
WordAnalysis,
|
|
9
9
|
TDKRule,
|
|
10
|
+
KubbealtiEntry,
|
|
11
|
+
WiktionaryEntry,
|
|
10
12
|
} from "./types";
|
|
11
13
|
import { TDKValidationError, TDKNetworkError } from "./errors";
|
|
12
14
|
import * as fs from "node:fs";
|
|
13
15
|
import * as path from "node:path";
|
|
14
16
|
import * as os from "node:os";
|
|
15
17
|
import * as https from "node:https";
|
|
18
|
+
import * as tls from "node:tls";
|
|
16
19
|
|
|
17
20
|
/**
|
|
18
21
|
* TDK (Türk Dil Kurumu) API Wrapper
|
|
@@ -20,6 +23,84 @@ import * as https from "node:https";
|
|
|
20
23
|
export class TDK {
|
|
21
24
|
private static readonly BASE_URL = "https://sozluk.gov.tr";
|
|
22
25
|
private static readonly AUDIO_API_HOST = "api.sozluk.gov.tr";
|
|
26
|
+
private static readonly KUBBEALTI_HOST = "eski.lugatim.com";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* `eski.lugatim.com` (Kubbealtı Lugatı's data API) sends only its leaf
|
|
30
|
+
* certificate during the TLS handshake, omitting the intermediates a
|
|
31
|
+
* correctly configured server would include — a server-side misconfiguration,
|
|
32
|
+
* not something we should paper over by disabling verification. These are
|
|
33
|
+
* the two certificates the server *should* be sending (fetched from the
|
|
34
|
+
* leaf's own Authority Information Access URLs), supplied here so Node can
|
|
35
|
+
* still build a full, properly verified chain up to a root it already
|
|
36
|
+
* trusts (ISRG Root X1). If Let's Encrypt rotates this intermediate, this
|
|
37
|
+
* stops working and every Kubbealtı call fails closed to `null` — same
|
|
38
|
+
* fail-closed contract as the rest of this file's fragile integrations.
|
|
39
|
+
*/
|
|
40
|
+
private static readonly KUBBEALTI_EXTRA_CA = [
|
|
41
|
+
`-----BEGIN CERTIFICATE-----
|
|
42
|
+
MIIE2jCCAsKgAwIBAgIQTr0klH4k05SALYSlL9WzGTANBgkqhkiG9w0BAQsFADAu
|
|
43
|
+
MQswCQYDVQQGEwJVUzENMAsGA1UEChMESVNSRzEQMA4GA1UEAxMHUm9vdCBZUjAe
|
|
44
|
+
Fw0yNTA5MDMwMDAwMDBaFw0yODA5MDIyMzU5NTlaMDMxCzAJBgNVBAYTAlVTMRYw
|
|
45
|
+
FAYDVQQKEw1MZXQncyBFbmNyeXB0MQwwCgYDVQQDEwNZUjIwggEiMA0GCSqGSIb3
|
|
46
|
+
DQEBAQUAA4IBDwAwggEKAoIBAQDZ0LxwBppqh84luqMerV/eeL/fXQ7mLQQv1Lnp
|
|
47
|
+
WKZbyvGpx6wh6AfnslAnF6ewTkcHA+gSOoBvm3Dfm06AuGiF+KRut4fAcowqnAQQ
|
|
48
|
+
CW98+QPP/eOv/wug7Iyk4NkOxf2I6g2f55T6nJoOTLFcukeRq80JGQEYan+dPFr9
|
|
49
|
+
OGUgQK2hGKgNkW87pappsOAuUJcroYhRt5uUis4qaZireiseu32gzDJNBAiKtsvd
|
|
50
|
+
6HX4v25bpkRNcS/B/Gtc9kVbUpD+2PLPxdei3Tim55k4tfAEXwD2qyiPTxrTNq6l
|
|
51
|
+
N+AMr5g2c1dNqkOTwjxeV6L5lpP1rGiYvLnRaPlOqyZRPW+5AgMBAAGjge4wgesw
|
|
52
|
+
DgYDVR0PAQH/BAQDAgGGMBMGA1UdJQQMM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7uMsJTnji2UDz9el5XLSv9fqJMuDQtYVb2oTNoKp
|
|
97
|
+
oUqhxPVC0aq4eG5MESaIdn8b5ZGSSeAJLMHXljEdlNza+ncfkviXk1POLnnFdvx8
|
|
98
|
+
/gk6M374WbLWFXw8N141B/Rl/tINGfl1TxOIiqtiMYkL02RSGb1kq34BL9NPP27z
|
|
99
|
+
RGMuHGnzS3hFIrRTfKxrzUZ9RzQWzEG3K6fJ3r2nqSltkeytis9DIBoFY9VmVyjL
|
|
100
|
+
M71DMi+y1+TRSJVClEMwvA4yL++7q9XZx5r5wBRWB4kQTKH5qyoZnDw7iiuh1lID
|
|
101
|
+
yDFx8r7i9vIJU5HS3moZLkYWAOilMaV9N56A9Bgb6dNcHkvg3NoaYA==
|
|
102
|
+
-----END CERTIFICATE-----`,
|
|
103
|
+
];
|
|
23
104
|
|
|
24
105
|
// Cache Mechanism
|
|
25
106
|
private static isCacheEnabled = false;
|
|
@@ -580,15 +661,234 @@ export class TDK {
|
|
|
580
661
|
.replace(/<\/(p|div)>/gi, "\n\n")
|
|
581
662
|
.replace(/<[^>]+>/g, "")
|
|
582
663
|
.replace(/ /gi, " ")
|
|
583
|
-
.replace(/&
|
|
664
|
+
.replace(/</gi, "<")
|
|
665
|
+
.replace(/>/gi, ">")
|
|
584
666
|
.replace(/"/gi, '"')
|
|
585
667
|
.replace(/'|’/gi, "'")
|
|
668
|
+
.replace(/&/gi, "&")
|
|
586
669
|
.replace(/[ \t]+/g, " ")
|
|
587
670
|
.replace(/[ \t]*\n[ \t]*/g, "\n")
|
|
588
671
|
.replace(/\n{3,}/g, "\n\n")
|
|
589
672
|
.trim();
|
|
590
673
|
}
|
|
591
674
|
|
|
675
|
+
/**
|
|
676
|
+
* GETs a JSON path from Kubbealtı Lugatı's data API (`eski.lugatim.com`),
|
|
677
|
+
* supplying `KUBBEALTI_EXTRA_CA` to work around that host's incomplete
|
|
678
|
+
* certificate chain (see the constant's doc comment). Fails closed to
|
|
679
|
+
* `null` on any error — network, TLS, HTTP, or JSON parse.
|
|
680
|
+
*/
|
|
681
|
+
private static fetchKubbealtiJson(path: string): Promise<any> {
|
|
682
|
+
return new Promise((resolve) => {
|
|
683
|
+
const req = https.request(
|
|
684
|
+
{
|
|
685
|
+
hostname: this.KUBBEALTI_HOST,
|
|
686
|
+
path,
|
|
687
|
+
method: "GET",
|
|
688
|
+
ca: [...tls.rootCertificates, ...this.KUBBEALTI_EXTRA_CA],
|
|
689
|
+
headers: {
|
|
690
|
+
"User-Agent":
|
|
691
|
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
|
|
692
|
+
},
|
|
693
|
+
},
|
|
694
|
+
(res) => {
|
|
695
|
+
if (res.statusCode !== 200) {
|
|
696
|
+
res.resume();
|
|
697
|
+
resolve(null);
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
let body = "";
|
|
701
|
+
res.on("data", (chunk) => (body += chunk));
|
|
702
|
+
res.on("end", () => {
|
|
703
|
+
try {
|
|
704
|
+
resolve(JSON.parse(body));
|
|
705
|
+
} catch {
|
|
706
|
+
resolve(null);
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
);
|
|
711
|
+
req.on("error", () => resolve(null));
|
|
712
|
+
req.end();
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* Kubbealtı indexes headwords with full classical Turkish orthography,
|
|
718
|
+
* including letters that a plain-ASCII-ish query tends to drop — most
|
|
719
|
+
* commonly ü/ö/ç/ğ/ş, but also the circumflex ("düzeltme işareti") used in
|
|
720
|
+
* Arabic/Persian loanwords like "rüzgâr". A search for "ruzgar" misses
|
|
721
|
+
* entirely (verified: even "ruzgâr" alone still misses — it's the missing
|
|
722
|
+
* ü, not the missing â, that actually breaks the match). This generates
|
|
723
|
+
* single-letter-substitution variants to retry, one substitution per
|
|
724
|
+
* variant (not combinatorial) — covers the overwhelmingly common case of
|
|
725
|
+
* one "de-Turkished" letter without an explosion of API calls for words
|
|
726
|
+
* with several.
|
|
727
|
+
*/
|
|
728
|
+
private static readonly TURKISH_DEASCII_MAP: Record<string, string[]> = {
|
|
729
|
+
a: ["â"],
|
|
730
|
+
i: ["ı", "î"],
|
|
731
|
+
o: ["ö"],
|
|
732
|
+
u: ["ü", "û"],
|
|
733
|
+
c: ["ç"],
|
|
734
|
+
g: ["ğ"],
|
|
735
|
+
s: ["ş"],
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
private static generateTurkishVariants(word: string): string[] {
|
|
739
|
+
const lower = word.trim().toLocaleLowerCase("tr-TR");
|
|
740
|
+
const variants: string[] = [];
|
|
741
|
+
for (let i = 0; i < lower.length; i++) {
|
|
742
|
+
for (const replacement of this.TURKISH_DEASCII_MAP[lower[i]] ?? []) {
|
|
743
|
+
variants.push(lower.slice(0, i) + replacement + lower.slice(i + 1));
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
return variants;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Returns Kubbealtı Lugatı ("Misalli Büyük Türkçe Sözlük") entries for a
|
|
751
|
+
* word, scraped from the site's own data API — undocumented, and Kubbealtı
|
|
752
|
+
* Lugatı is a commercial dictionary product, unlike TDK's or Wiktionary's
|
|
753
|
+
* openly-published data, so use this in line with their terms. `anlam` is
|
|
754
|
+
* raw HTML (rich typography markup); use `getKubbealtiMeanings()` for
|
|
755
|
+
* plain text. Falls back to `generateTurkishVariants()` if the exact query
|
|
756
|
+
* comes up empty (see its doc comment). Returns `null` on any fetch/parse
|
|
757
|
+
* failure, `[]` if no variant matches either.
|
|
758
|
+
*/
|
|
759
|
+
public static async getKubbealti(word: string): Promise<KubbealtiEntry[] | null> {
|
|
760
|
+
if (!word || word.trim() === "") return null;
|
|
761
|
+
|
|
762
|
+
const data = await this.fetchKubbealtiJson(`/rest/s/${encodeURIComponent(word.trim())}/`);
|
|
763
|
+
if (!data || !Array.isArray(data.content)) return null;
|
|
764
|
+
if (data.content.length > 0) {
|
|
765
|
+
return data.content.map((entry: any) => ({ kelime: entry.kelime, anlam: entry.anlam }));
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
for (const variant of this.generateTurkishVariants(word)) {
|
|
769
|
+
const variantData = await this.fetchKubbealtiJson(`/rest/s/${encodeURIComponent(variant)}/`);
|
|
770
|
+
if (variantData && Array.isArray(variantData.content) && variantData.content.length > 0) {
|
|
771
|
+
return variantData.content.map((entry: any) => ({ kelime: entry.kelime, anlam: entry.anlam }));
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
return [];
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Same as `getKubbealti()` but with each entry's `anlam` HTML stripped to
|
|
779
|
+
* plain text via `htmlToPlainText()`.
|
|
780
|
+
*/
|
|
781
|
+
public static async getKubbealtiMeanings(word: string): Promise<string[] | null> {
|
|
782
|
+
const entries = await this.getKubbealti(word);
|
|
783
|
+
if (!entries) return null;
|
|
784
|
+
return entries.map((e) => this.htmlToPlainText(e.anlam));
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* Autocomplete suggestions from Kubbealtı Lugatı's own typeahead endpoint
|
|
789
|
+
* (separate from `getSuggestions()`, which uses TDK's data).
|
|
790
|
+
*/
|
|
791
|
+
public static async getKubbealtiSuggestions(prefix: string): Promise<string[]> {
|
|
792
|
+
if (!prefix || prefix.trim() === "") return [];
|
|
793
|
+
const data = await this.fetchKubbealtiJson(`/rest/word-search/${encodeURIComponent(prefix.trim())}`);
|
|
794
|
+
if (!Array.isArray(data)) return [];
|
|
795
|
+
return data.map((item: any) => item.display).filter(Boolean);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Returns the etymology paragraph for a word from Nişanyan Sözlük, scraped
|
|
800
|
+
* from that page's server-rendered `<meta name="description">` tag (the
|
|
801
|
+
* page already puts the full etymology text there for SEO, so no need to
|
|
802
|
+
* parse the site's internal SvelteKit data format). Returns `null` if the
|
|
803
|
+
* word isn't found (the page falls back to a generic site tagline in that
|
|
804
|
+
* case) or the request fails.
|
|
805
|
+
*/
|
|
806
|
+
public static async getNisanyan(word: string): Promise<string | null> {
|
|
807
|
+
if (!word || word.trim() === "") return null;
|
|
808
|
+
try {
|
|
809
|
+
const response = await fetch(
|
|
810
|
+
`https://www.nisanyansozluk.com/kelime/${encodeURIComponent(word.trim().toLocaleLowerCase("tr-TR"))}`,
|
|
811
|
+
{ headers: { "User-Agent": "TDK-API-Nodejs-Wrapper/1.0" } }
|
|
812
|
+
);
|
|
813
|
+
if (!response.ok) return null;
|
|
814
|
+
const html = await response.text();
|
|
815
|
+
const match = html.match(/<meta name="description" content="([^"]*)"/);
|
|
816
|
+
if (!match) return null;
|
|
817
|
+
const description = this.htmlToPlainText(match[1]);
|
|
818
|
+
if (description === "Çağdaş Türkçenin Etimolojisi") return null;
|
|
819
|
+
return description;
|
|
820
|
+
} catch {
|
|
821
|
+
return null;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
private static async fetchWiktionaryEntry(title: string): Promise<WiktionaryEntry | null> {
|
|
826
|
+
try {
|
|
827
|
+
const url = `https://tr.wiktionary.org/w/api.php?action=query&prop=extracts&titles=${encodeURIComponent(
|
|
828
|
+
title
|
|
829
|
+
)}&format=json&explaintext=1&formatversion=2`;
|
|
830
|
+
const response = await fetch(url, { headers: { "User-Agent": "TDK-API-Nodejs-Wrapper/1.0" } });
|
|
831
|
+
if (!response.ok) return null;
|
|
832
|
+
const data = await response.json();
|
|
833
|
+
const page = data?.query?.pages?.[0];
|
|
834
|
+
if (!page || page.missing || !page.extract) return null;
|
|
835
|
+
|
|
836
|
+
const raw: string = page.extract;
|
|
837
|
+
const sections: Record<string, string> = {};
|
|
838
|
+
const parts = raw.split(/\n(={2,4})\s*(.+?)\s*\1\n/);
|
|
839
|
+
// parts[0] is text before the first heading (usually empty); after
|
|
840
|
+
// that, headings and their following text alternate in triples.
|
|
841
|
+
for (let i = 1; i < parts.length; i += 3) {
|
|
842
|
+
const title = parts[i + 1]?.trim();
|
|
843
|
+
const content = parts[i + 2]?.trim();
|
|
844
|
+
if (title) sections[title] = content ?? "";
|
|
845
|
+
}
|
|
846
|
+
return { raw, sections };
|
|
847
|
+
} catch {
|
|
848
|
+
return null;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* Returns the Turkish Wiktionary (`tr.wiktionary.org`) entry for a word,
|
|
854
|
+
* via MediaWiki's official Action API (`action=query&prop=extracts`) — no
|
|
855
|
+
* scraping involved, this is a stable, documented public API. `sections`
|
|
856
|
+
* splits the plain-text extract on its `== Heading ==`/`=== Heading ===`
|
|
857
|
+
* markers (e.g. "Köken", "Söyleniş", "Ad") for convenience; `raw` has the
|
|
858
|
+
* unsplit text. This wiki has title capitalization turned off
|
|
859
|
+
* ($wgCapitalLinks=false — common for Wiktionaries, since case is
|
|
860
|
+
* meaningful for a dictionary: "Türkiye" the country vs. a lowercase
|
|
861
|
+
* common word), so an exact-case miss retries with the first letter
|
|
862
|
+
* uppercased (Turkish-locale-aware, so "istanbul" tries "İstanbul", not
|
|
863
|
+
* "Istanbul") before giving up. Returns `null` if neither is found or the
|
|
864
|
+
* request fails.
|
|
865
|
+
*/
|
|
866
|
+
public static async getWiktionary(word: string): Promise<WiktionaryEntry | null> {
|
|
867
|
+
if (!word || word.trim() === "") return null;
|
|
868
|
+
const trimmed = word.trim();
|
|
869
|
+
|
|
870
|
+
const direct = await this.fetchWiktionaryEntry(trimmed);
|
|
871
|
+
if (direct) return direct;
|
|
872
|
+
|
|
873
|
+
const capitalized = trimmed.charAt(0).toLocaleUpperCase("tr-TR") + trimmed.slice(1);
|
|
874
|
+
if (capitalized === trimmed) return null;
|
|
875
|
+
return this.fetchWiktionaryEntry(capitalized);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
/**
|
|
879
|
+
* Convenience filter over `getWiktionary()`: returns just one section's
|
|
880
|
+
* text (e.g. `getWiktionarySection(word, "Köken")` for etymology), matched
|
|
881
|
+
* case-insensitively. Returns `null` if the word or the section isn't found.
|
|
882
|
+
*/
|
|
883
|
+
public static async getWiktionarySection(word: string, sectionName: string): Promise<string | null> {
|
|
884
|
+
const entry = await this.getWiktionary(word);
|
|
885
|
+
if (!entry) return null;
|
|
886
|
+
const key = Object.keys(entry.sections).find(
|
|
887
|
+
(k) => k.toLocaleLowerCase("tr-TR") === sectionName.trim().toLocaleLowerCase("tr-TR")
|
|
888
|
+
);
|
|
889
|
+
return key ? entry.sections[key] : null;
|
|
890
|
+
}
|
|
891
|
+
|
|
592
892
|
/**
|
|
593
893
|
* Returns compound words that contain this word.
|
|
594
894
|
*/
|
package/src/types.ts
CHANGED
|
@@ -123,4 +123,14 @@ export interface WordAnalysis {
|
|
|
123
123
|
origin: string | null;
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
export interface KubbealtiEntry {
|
|
127
|
+
kelime: string;
|
|
128
|
+
anlam: string;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface WiktionaryEntry {
|
|
132
|
+
raw: string;
|
|
133
|
+
sections: Record<string, string>;
|
|
134
|
+
}
|
|
135
|
+
|
|
126
136
|
export type TDKResponse = WordInfo[] | { error: string };
|