-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
discover, test, document wordnet node relationships
- Loading branch information
1 parent
09a2859
commit a4e9341
Showing
6 changed files
with
240 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
{ | ||
"tasks": { | ||
"check": "deno fmt && deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx", | ||
"test": "deno test --allow-read=. --allow-write=. --allow-net" | ||
}, | ||
"imports": { | ||
"~/": "./", | ||
"$std/": "https://deno.land/[email protected]/", | ||
"@dbushell/xml-streamify": "jsr:@dbushell/xml-streamify@^0.4.0", | ||
"zod": "https://deno.land/x/[email protected]/mod.ts" | ||
}, | ||
"exclude": ["./bun/**"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { existsSync } from "$std/fs/exists.ts"; | ||
import { Node, parse } from "@dbushell/xml-streamify"; | ||
import { LexiconNode } from "~/parse_node_helpers.ts"; | ||
|
||
export const version = "2023"; | ||
export const fileName = `english-wordnet-${version}.xml`; | ||
export const localFileName = `./data/${fileName}`; | ||
|
||
const testFilePath = async () => { | ||
const path = await Deno.realPath(localFileName); | ||
return path; | ||
}; | ||
|
||
const testFileExists = async () => { | ||
if (existsSync(localFileName)) { | ||
const path = await Deno.realPath(localFileName); | ||
const stat = await Deno.stat(path); | ||
return stat.isFile; | ||
} | ||
return false; | ||
}; | ||
|
||
const fetchTestFile = async () => { | ||
const src = await fetch( | ||
`https://en-word.net/static/${fileName}.gz`, | ||
); | ||
const dest = await Deno.open(localFileName, { | ||
create: true, | ||
write: true, | ||
}); | ||
if (src.body == null) return; | ||
await src.body | ||
.pipeThrough(new DecompressionStream("gzip")) | ||
.pipeTo(dest.writable); | ||
}; | ||
|
||
export const testFileParser = async () => { | ||
if (!await testFileExists()) { | ||
console.log("unzipping"); | ||
await fetchTestFile(); | ||
} | ||
const p = await testFilePath(); | ||
|
||
const parser = parse(`file:///${p.replace("\\", "/")}`, { | ||
ignoreDeclaration: false, | ||
silent: false, | ||
}); | ||
return parser; | ||
}; | ||
|
||
export const parseLexicon = async ( | ||
parser: AsyncGenerator<Node, void | Node, void>, | ||
) => { | ||
for await (const node of parser) { | ||
if (node.type == "Lexicon") { | ||
return LexiconNode(node); | ||
} | ||
} | ||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import { assert } from "$std/assert/assert.ts"; | ||
import { parseLexicon, testFileParser } from "~/parse_wordnet.ts"; | ||
import { | ||
Definition, | ||
Example, | ||
Form, | ||
ILIDefinition, | ||
Lemma, | ||
LexicalEntry, | ||
Pronunciation, | ||
Sense, | ||
SenseRelation, | ||
Synset, | ||
SynsetRelation, | ||
} from "~/wordnet_types.ts"; | ||
|
||
type IdRegistry = Map<string, void>; | ||
type RefRegistry = Map<string, void>; | ||
|
||
type IdsPack = { | ||
synsetIds: IdRegistry; | ||
senseIds: IdRegistry; | ||
lexicalEntryIds: IdRegistry; | ||
syntacticBehaviorsIds: IdRegistry; | ||
}; | ||
|
||
type RefsPack = { | ||
senseSynsetRefs: RefRegistry; | ||
senseSubCatRefs: RefRegistry; | ||
senseRelationTargetRefs: RefRegistry; | ||
synsetMembersRefs: RefRegistry; | ||
synsetRelationTargetRefs: RefRegistry; | ||
}; | ||
|
||
Deno.test("wordnet node relationships", async () => { | ||
const parser = await testFileParser(); | ||
const lexicon = await parseLexicon(parser); | ||
assert(lexicon != undefined); | ||
|
||
const synsetIds: IdRegistry = new Map(); | ||
const senseIds: IdRegistry = new Map(); | ||
const lexicalEntryIds: IdRegistry = new Map(); | ||
const syntacticBehaviorsIds: IdRegistry = new Map(); | ||
|
||
const senseSynsetRefs: RefRegistry = new Map(); | ||
const senseSubCatRefs: RefRegistry = new Map(); | ||
const senseRelationTargetRefs: RefRegistry = new Map(); | ||
const synsetMembersRefs: RefRegistry = new Map(); | ||
const synsetRelationTargetRefs: RefRegistry = new Map(); | ||
|
||
lexicon.id; | ||
lexicon.label; | ||
lexicon.language; | ||
lexicon.email; | ||
lexicon.license; | ||
lexicon.version; | ||
lexicon.citation; | ||
lexicon.url; | ||
lexicon.lexicalEntries.forEach( | ||
(le: LexicalEntry) => { | ||
lexicalEntryIds.set(le.id); | ||
le.lemmas.forEach((l: Lemma) => { | ||
l.writtenForm; // | ||
l.partOfSpeech; // | ||
l.pronunciations.forEach((p: Pronunciation) => { | ||
p.variety; | ||
p.inner; | ||
}); | ||
}); | ||
le.senses.forEach((s: Sense) => { | ||
senseIds.set(s.id); | ||
senseSynsetRefs.set(s.synset); | ||
if (s.subCat) senseSubCatRefs.set(s.subCat); | ||
s.adjPosition; // | ||
s.senseRelations.forEach((sr: SenseRelation) => { | ||
sr.relType; | ||
sr.dcType; | ||
senseRelationTargetRefs.set(sr.target); | ||
}); | ||
}); | ||
le.forms.forEach((f: Form) => { | ||
f.writtenForm; | ||
}); | ||
}, | ||
); | ||
lexicon.synsets.forEach((s: Synset) => { | ||
synsetIds.set(s.id); | ||
s.ili; | ||
s.members.forEach((m) => { | ||
synsetMembersRefs.set(m); | ||
}); | ||
s.partOfSpeech; | ||
s.lexfile; | ||
s.dcSource; | ||
s.definitions.forEach((d: Definition) => { | ||
d.inner; | ||
}); | ||
s.examples.forEach((e: Example) => { | ||
e.inner; | ||
e.dcSource; | ||
}); | ||
s.iliDefinitions.forEach((i: ILIDefinition) => { | ||
i.inner; | ||
}); | ||
s.synsetRelations.forEach((s: SynsetRelation) => { | ||
s.relType; | ||
synsetRelationTargetRefs.set(s.target); | ||
}); | ||
}); | ||
lexicon.syntacticBehaviors.forEach((s) => syntacticBehaviorsIds.set(s.id)); | ||
|
||
assertAllowedRelationships( | ||
{ | ||
synsetIds, | ||
senseIds, | ||
lexicalEntryIds, | ||
syntacticBehaviorsIds, | ||
} satisfies IdsPack, | ||
{ | ||
senseSynsetRefs, | ||
senseSubCatRefs, | ||
senseRelationTargetRefs, | ||
synsetMembersRefs, | ||
synsetRelationTargetRefs, | ||
} satisfies RefsPack, | ||
new Map([ | ||
["senseSubCatRefs > syntacticBehaviorsIds", undefined], | ||
["senseRelationTargetRefs > senseIds", undefined], | ||
["synsetMembersRefs > lexicalEntryIds", undefined], | ||
["synsetRelationTargetRefs > synsetIds", undefined], | ||
["senseSynsetRefs > synsetIds", undefined], | ||
]), | ||
); | ||
}); | ||
|
||
const assertAllowedRelationships = ( | ||
idsPack: IdsPack, | ||
refsPack: RefsPack, | ||
allowed: Map<string, void>, | ||
) => { | ||
const found = collectRelationships(idsPack, refsPack); | ||
found.forEach((_v, k) => { | ||
assert(allowed.has(k), "Disallowed relation: " + k); | ||
}); | ||
}; | ||
|
||
const collectRelationships = (idsPack: IdsPack, refsPack: RefsPack) => { | ||
const result: Map<string, void> = new Map(); | ||
Object.entries(refsPack).forEach(([refPackKey, refPackRegistry]) => { | ||
Object.entries(idsPack).forEach(([idPackKey, idPackRegistry]) => { | ||
for (const ref of refPackRegistry.keys()) { | ||
if (idPackRegistry.has(ref)) { | ||
result.set(refPackKey + " > " + idPackKey); | ||
} | ||
} | ||
}); | ||
}); | ||
return result; | ||
/* | ||
senseSynsetRefs > synsetIds | ||
senseSubCatRefs > syntacticBehaviorsIds | ||
senseRelationTargetRefs > senseIds | ||
synsetMembersRefs > lexicalEntryIds | ||
synsetRelationTargetRefs > synsetIds | ||
*/ | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters