nlptoolkit-universaldependencyparser 1.0.0
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/dist/Agenda.d.ts +24 -0
- package/dist/Agenda.js +72 -0
- package/dist/Agenda.js.map +1 -0
- package/dist/ArcEagerInstanceGenerator.d.ts +20 -0
- package/dist/ArcEagerInstanceGenerator.js +81 -0
- package/dist/ArcEagerInstanceGenerator.js.map +1 -0
- package/dist/ArcEagerOracle.d.ts +9 -0
- package/dist/ArcEagerOracle.js +39 -0
- package/dist/ArcEagerOracle.js.map +1 -0
- package/dist/ArcEagerTransitionParser.d.ts +9 -0
- package/dist/ArcEagerTransitionParser.js +113 -0
- package/dist/ArcEagerTransitionParser.js.map +1 -0
- package/dist/ArcStandardOracle.d.ts +9 -0
- package/dist/ArcStandardOracle.js +36 -0
- package/dist/ArcStandardOracle.js.map +1 -0
- package/dist/ArcStandardTransitionParser.d.ts +28 -0
- package/dist/ArcStandardTransitionParser.js +129 -0
- package/dist/ArcStandardTransitionParser.js.map +1 -0
- package/dist/Candidate.d.ts +8 -0
- package/dist/Candidate.js +27 -0
- package/dist/Candidate.js.map +1 -0
- package/dist/Command.d.ts +6 -0
- package/dist/Command.js +8 -0
- package/dist/Command.js.map +1 -0
- package/dist/Decision.d.ts +7 -0
- package/dist/Decision.js +25 -0
- package/dist/Decision.js.map +1 -0
- package/dist/InstanceGenerator.d.ts +33 -0
- package/dist/InstanceGenerator.js +77 -0
- package/dist/InstanceGenerator.js.map +1 -0
- package/dist/Oracle.d.ts +49 -0
- package/dist/Oracle.js +115 -0
- package/dist/Oracle.js.map +1 -0
- package/dist/RandomOracle.d.ts +14 -0
- package/dist/RandomOracle.js +45 -0
- package/dist/RandomOracle.js.map +1 -0
- package/dist/RandomScoringOracle.d.ts +5 -0
- package/dist/RandomScoringOracle.js +21 -0
- package/dist/RandomScoringOracle.js.map +1 -0
- package/dist/ScoringOracle.d.ts +4 -0
- package/dist/ScoringOracle.js +17 -0
- package/dist/ScoringOracle.js.map +1 -0
- package/dist/SimpleInstanceGenerator.d.ts +14 -0
- package/dist/SimpleInstanceGenerator.js +67 -0
- package/dist/SimpleInstanceGenerator.js.map +1 -0
- package/dist/StackRelation.d.ts +10 -0
- package/dist/StackRelation.js +30 -0
- package/dist/StackRelation.js.map +1 -0
- package/dist/StackWord.d.ts +11 -0
- package/dist/StackWord.js +42 -0
- package/dist/StackWord.js.map +1 -0
- package/dist/State.d.ts +25 -0
- package/dist/State.js +161 -0
- package/dist/State.js.map +1 -0
- package/dist/TransitionParser.d.ts +73 -0
- package/dist/TransitionParser.js +164 -0
- package/dist/TransitionParser.js.map +1 -0
- package/dist/TransitionSystem.d.ts +4 -0
- package/dist/TransitionSystem.js +6 -0
- package/dist/TransitionSystem.js.map +1 -0
- package/index.js +20 -0
- package/models/en_atis_eager_c45_2.txt +1903 -0
- package/models/en_atis_eager_c45_3.txt +1918 -0
- package/models/en_atis_standard_c45_2.txt +1948 -0
- package/models/en_atis_standard_c45_3.txt +2233 -0
- package/models/tr_atis_eager_c45_2.txt +2208 -0
- package/models/tr_atis_eager_c45_3.txt +2303 -0
- package/models/tr_atis_standard_c45_2.txt +2863 -0
- package/models/tr_atis_standard_c45_3.txt +3413 -0
- package/models/tr_boun_eager_c45_2.txt +1613 -0
- package/models/tr_boun_eager_c45_3.txt +1818 -0
- package/models/tr_boun_standard_c45_2.txt +898 -0
- package/models/tr_boun_standard_c45_3.txt +1138 -0
- package/models/tr_framenet_eager_c45_2.txt +453 -0
- package/models/tr_framenet_eager_c45_3.txt +668 -0
- package/models/tr_framenet_standard_c45_2.txt +363 -0
- package/models/tr_framenet_standard_c45_3.txt +238 -0
- package/models/tr_kenet_eager_c45_2.txt +1788 -0
- package/models/tr_kenet_eager_c45_3.txt +1588 -0
- package/models/tr_kenet_standard_c45_2.txt +1308 -0
- package/models/tr_kenet_standard_c45_3.txt +1378 -0
- package/models/tr_penn_eager_c45_2.txt +2788 -0
- package/models/tr_penn_eager_c45_3.txt +1718 -0
- package/models/tr_penn_standard_c45_2.txt +2203 -0
- package/models/tr_penn_standard_c45_3.txt +1493 -0
- package/models/tr_tourism_eager_c45_2.txt +3468 -0
- package/models/tr_tourism_eager_c45_3.txt +4213 -0
- package/models/tr_tourism_standard_c45_2.txt +3868 -0
- package/models/tr_tourism_standard_c45_3.txt +4728 -0
- package/package.json +30 -0
- package/source/Parser/TransitionBasedParser/Agenda.ts +66 -0
- package/source/Parser/TransitionBasedParser/ArcEagerInstanceGenerator.ts +73 -0
- package/source/Parser/TransitionBasedParser/ArcEagerOracle.ts +30 -0
- package/source/Parser/TransitionBasedParser/ArcEagerTransitionParser.ts +105 -0
- package/source/Parser/TransitionBasedParser/ArcStandardOracle.ts +28 -0
- package/source/Parser/TransitionBasedParser/ArcStandardTransitionParser.ts +123 -0
- package/source/Parser/TransitionBasedParser/Candidate.ts +20 -0
- package/source/Parser/TransitionBasedParser/Command.ts +3 -0
- package/source/Parser/TransitionBasedParser/Decision.ts +16 -0
- package/source/Parser/TransitionBasedParser/InstanceGenerator.ts +85 -0
- package/source/Parser/TransitionBasedParser/Oracle.ts +123 -0
- package/source/Parser/TransitionBasedParser/RandomOracle.ts +37 -0
- package/source/Parser/TransitionBasedParser/RandomScoringOracle.ts +10 -0
- package/source/Parser/TransitionBasedParser/ScoringOracle.ts +5 -0
- package/source/Parser/TransitionBasedParser/SimpleInstanceGenerator.ts +57 -0
- package/source/Parser/TransitionBasedParser/StackRelation.ts +27 -0
- package/source/Parser/TransitionBasedParser/StackWord.ts +38 -0
- package/source/Parser/TransitionBasedParser/State.ts +168 -0
- package/source/Parser/TransitionBasedParser/TransitionParser.ts +187 -0
- package/source/Parser/TransitionBasedParser/TransitionSystem.ts +3 -0
- package/source/tsconfig.json +13 -0
- package/tsconfig.json +15 -0
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nlptoolkit-universaldependencyparser",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Dependency Parsing Algorithms",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "mocha"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/StarlangSoftware/UniversalDependencyParser-Js.git"
|
|
13
|
+
},
|
|
14
|
+
"author": "Olcay Taner YILDIZ",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/StarlangSoftware/UniversalDependencyParser-Js/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/StarlangSoftware/UniversalDependencyParser-Js#readme",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/mocha": "^10.0.1",
|
|
22
|
+
"mocha": "^10.2.0",
|
|
23
|
+
"ts-node": "^10.9.1",
|
|
24
|
+
"typescript": "^5.0.4"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"nlptoolkit-classification": "^1.0.7",
|
|
28
|
+
"nlptoolkit-dependencyparser": "^1.0.8"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {State} from "./State";
|
|
2
|
+
import {ScoringOracle} from "./ScoringOracle";
|
|
3
|
+
|
|
4
|
+
export class Agenda {
|
|
5
|
+
|
|
6
|
+
private agenda: Map<State, number>
|
|
7
|
+
private beamSize: number
|
|
8
|
+
|
|
9
|
+
constructor(beamSize: number) {
|
|
10
|
+
this.agenda = new Map<State, number>()
|
|
11
|
+
this.beamSize = beamSize
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Retrieves the set of states currently in the agenda.
|
|
16
|
+
* @return A set of states that are currently in the agenda.
|
|
17
|
+
*/
|
|
18
|
+
getKeySet(): IterableIterator<State>{
|
|
19
|
+
return this.agenda.keys()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Updates the agenda with a new state if it is better than the worst state
|
|
24
|
+
* currently in the agenda or if there is room in the agenda.
|
|
25
|
+
* @param oracle The ScoringOracle used to score the state.
|
|
26
|
+
* @param current The state to be added to the agenda.
|
|
27
|
+
*/
|
|
28
|
+
updateAgenda(oracle: ScoringOracle, current: State){
|
|
29
|
+
if (this.agenda.has(current)){
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
let point = oracle.score(current)
|
|
33
|
+
if (this.agenda.size < this.beamSize){
|
|
34
|
+
this.agenda.set(current, point)
|
|
35
|
+
} else{
|
|
36
|
+
let worst = null
|
|
37
|
+
let worstValue = Number.MAX_VALUE
|
|
38
|
+
for (let key of this.agenda.keys()){
|
|
39
|
+
if (this.agenda.get(key) < worstValue){
|
|
40
|
+
worstValue = this.agenda.get(key)
|
|
41
|
+
worst = key
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (point > worstValue){
|
|
45
|
+
this.agenda.delete(worst)
|
|
46
|
+
this.agenda.set(current, point)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Retrieves the best state from the agenda based on the highest score.
|
|
53
|
+
* @return The state with the highest score in the agenda.
|
|
54
|
+
*/
|
|
55
|
+
best(): State{
|
|
56
|
+
let best = null
|
|
57
|
+
let bestValue = Number.MIN_VALUE
|
|
58
|
+
for (let key of this.agenda.keys()){
|
|
59
|
+
if (this.agenda.get(key) > bestValue){
|
|
60
|
+
bestValue = this.agenda.get(key)
|
|
61
|
+
best = key
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return best
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {InstanceGenerator} from "./InstanceGenerator";
|
|
2
|
+
import {State} from "./State";
|
|
3
|
+
import {Instance} from "nlptoolkit-classification/dist/Instance/Instance";
|
|
4
|
+
import {
|
|
5
|
+
UniversalDependencyTreeBankWord
|
|
6
|
+
} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyTreeBankWord";
|
|
7
|
+
import {DiscreteIndexedAttribute} from "nlptoolkit-classification/dist/Attribute/DiscreteIndexedAttribute";
|
|
8
|
+
import {
|
|
9
|
+
UniversalDependencyTreeBankFeatures
|
|
10
|
+
} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyTreeBankFeatures";
|
|
11
|
+
import {Attribute} from "nlptoolkit-classification/dist/Attribute/Attribute";
|
|
12
|
+
|
|
13
|
+
export class ArcEagerInstanceGenerator extends InstanceGenerator{
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the given word has a valid relation.
|
|
17
|
+
* @param word The UniversalDependencyTreeBankWord to check.
|
|
18
|
+
* @return true if the relation is valid, false otherwise.
|
|
19
|
+
*/
|
|
20
|
+
private suitable(word: UniversalDependencyTreeBankWord): boolean{
|
|
21
|
+
return word.getRelation() != null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Generates an Instance object based on the provided state, window size, and command.
|
|
26
|
+
* The Instance is populated with attributes derived from the words in the state.
|
|
27
|
+
* @param state The state used to generate the instance.
|
|
28
|
+
* @param windowSize The size of the window used to extract words from the state.
|
|
29
|
+
* @param command The command associated with the instance.
|
|
30
|
+
* @return The generated Instance object.
|
|
31
|
+
*/
|
|
32
|
+
generate(state: State, windowSize: number, command: string): Instance {
|
|
33
|
+
let instance = new Instance(command)
|
|
34
|
+
let attributes = new Array<Attribute>()
|
|
35
|
+
for (let i = 0; i < windowSize; i++) {
|
|
36
|
+
let word = state.getStackWord(i);
|
|
37
|
+
if (word == null) {
|
|
38
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, 18))
|
|
39
|
+
this.addEmptyAttributes(attributes)
|
|
40
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, 59))
|
|
41
|
+
} else {
|
|
42
|
+
if (word.getName() == "root") {
|
|
43
|
+
attributes.push(new DiscreteIndexedAttribute("root", 0, 18))
|
|
44
|
+
this.addEmptyAttributes(attributes)
|
|
45
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, 59))
|
|
46
|
+
} else {
|
|
47
|
+
attributes.push(new DiscreteIndexedAttribute(word.getUpos().toString(), UniversalDependencyTreeBankFeatures.posIndex(word.getUpos().toString()) + 1, 18))
|
|
48
|
+
this.addFeatureAttributes(word, attributes)
|
|
49
|
+
if (this.suitable(word)) {
|
|
50
|
+
attributes.push(new DiscreteIndexedAttribute(word.getRelation().toString(), UniversalDependencyTreeBankFeatures.dependencyIndex(word.getRelation().toString()) + 1, 59))
|
|
51
|
+
} else {
|
|
52
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, 59))
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
for (let i = 0; i < windowSize; i++) {
|
|
58
|
+
let word = state.getWordListWord(i)
|
|
59
|
+
if (word != null) {
|
|
60
|
+
attributes.push(new DiscreteIndexedAttribute(word.getUpos().toString(), UniversalDependencyTreeBankFeatures.posIndex(word.getUpos().toString()) + 1, 18))
|
|
61
|
+
this.addFeatureAttributes(word, attributes)
|
|
62
|
+
} else {
|
|
63
|
+
attributes.push(new DiscreteIndexedAttribute("root", 0, 18))
|
|
64
|
+
this.addEmptyAttributes(attributes)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (let attribute of attributes) {
|
|
68
|
+
instance.addAttribute(attribute)
|
|
69
|
+
}
|
|
70
|
+
return instance
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {Oracle} from "./Oracle";
|
|
2
|
+
import {State} from "./State";
|
|
3
|
+
import {Decision} from "./Decision";
|
|
4
|
+
import {Model} from "nlptoolkit-classification/dist/Model/Model";
|
|
5
|
+
import {SimpleInstanceGenerator} from "./SimpleInstanceGenerator";
|
|
6
|
+
|
|
7
|
+
export class ArcEagerOracle extends Oracle{
|
|
8
|
+
|
|
9
|
+
constructor(model: Model, windowSize: number) {
|
|
10
|
+
super(model, windowSize)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public makeDecision(state: State): Decision {
|
|
14
|
+
let instanceGenerator = new SimpleInstanceGenerator()
|
|
15
|
+
let instance = instanceGenerator.generate(state, this.windowSize, "");
|
|
16
|
+
let best = this.findBestValidEagerClassInfo(this.commandModel.predictProbability(instance), state)
|
|
17
|
+
let decisionCandidate = this.getDecisionCandidate(best)
|
|
18
|
+
if (decisionCandidate.getCommand() == Command.SHIFT) {
|
|
19
|
+
return new Decision(Command.SHIFT, null, 0.0)
|
|
20
|
+
} else if (decisionCandidate.getCommand() == Command.REDUCE) {
|
|
21
|
+
return new Decision(Command.REDUCE, null, 0.0)
|
|
22
|
+
}
|
|
23
|
+
return new Decision(decisionCandidate.getCommand(), decisionCandidate.getUniversalDependencyType(), 0.0)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected scoreDecisions(state: State, transitionSystem: TransitionSystem): Array<Decision> {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {TransitionParser} from "./TransitionParser";
|
|
2
|
+
import {
|
|
3
|
+
UniversalDependencyTreeBankSentence
|
|
4
|
+
} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyTreeBankSentence";
|
|
5
|
+
import {Instance} from "nlptoolkit-classification/dist/Instance/Instance";
|
|
6
|
+
import {Oracle} from "./Oracle";
|
|
7
|
+
import {
|
|
8
|
+
UniversalDependencyTreeBankWord
|
|
9
|
+
} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyTreeBankWord";
|
|
10
|
+
import {ArcEagerInstanceGenerator} from "./ArcEagerInstanceGenerator";
|
|
11
|
+
import {StackWord} from "./StackWord";
|
|
12
|
+
import {State} from "./State";
|
|
13
|
+
import {StackRelation} from "./StackRelation";
|
|
14
|
+
|
|
15
|
+
export class ArcEagerTransitionParser extends TransitionParser{
|
|
16
|
+
|
|
17
|
+
constructor() {
|
|
18
|
+
super()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
dependencyParse(universalDependencyTreeBankSentence: UniversalDependencyTreeBankSentence, oracle: Oracle): UniversalDependencyTreeBankSentence {
|
|
22
|
+
let sentence = this.createResultSentence(universalDependencyTreeBankSentence)
|
|
23
|
+
let state = this.initialState(sentence)
|
|
24
|
+
while (state.wordListSize() > 0 || state.stackSize() > 1) {
|
|
25
|
+
let decision = oracle.makeDecision(state)
|
|
26
|
+
switch (decision.getCommand()) {
|
|
27
|
+
case Command.SHIFT:
|
|
28
|
+
state.applyShift()
|
|
29
|
+
break;
|
|
30
|
+
case Command.LEFTARC:
|
|
31
|
+
state.applyArcEagerLeftArc(decision.getUniversalDependencyType())
|
|
32
|
+
break;
|
|
33
|
+
case Command.RIGHTARC:
|
|
34
|
+
state.applyArcEagerRightArc(decision.getUniversalDependencyType())
|
|
35
|
+
break;
|
|
36
|
+
case Command.REDUCE:
|
|
37
|
+
state.applyReduce()
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return sentence;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
simulateParse(sentence: UniversalDependencyTreeBankSentence, windowSize: number): Array<Instance> {
|
|
47
|
+
let topRelation = null
|
|
48
|
+
let instanceGenerator = new ArcEagerInstanceGenerator()
|
|
49
|
+
let instanceList = new Array<Instance>()
|
|
50
|
+
let wordMap = new Map<number, UniversalDependencyTreeBankWord>()
|
|
51
|
+
let wordList = new Array<StackWord>()
|
|
52
|
+
let stack = new Array<StackWord>()
|
|
53
|
+
for (let j = 0; j < sentence.wordCount(); j++) {
|
|
54
|
+
let word = sentence.getWord(j);
|
|
55
|
+
if (word instanceof UniversalDependencyTreeBankWord){
|
|
56
|
+
let clone = word.clone()
|
|
57
|
+
clone.setRelation(null)
|
|
58
|
+
wordMap.set(j + 1, word)
|
|
59
|
+
wordList.push(new StackWord(clone, j + 1));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
stack.push(new StackWord())
|
|
63
|
+
let state = new State(stack, wordList, new Array<StackRelation>())
|
|
64
|
+
while (wordList.length > 0 || stack.length > 1) {
|
|
65
|
+
let firstRelation
|
|
66
|
+
let first
|
|
67
|
+
if (wordList.length != 0) {
|
|
68
|
+
first = wordList[0].getWord()
|
|
69
|
+
firstRelation = wordMap.get(wordList[0].getToWord()).getRelation()
|
|
70
|
+
} else {
|
|
71
|
+
first = null
|
|
72
|
+
firstRelation = null
|
|
73
|
+
}
|
|
74
|
+
let top = stack[stack.length - 1].getWord()
|
|
75
|
+
if (top.getName() != "root") {
|
|
76
|
+
topRelation = wordMap.get(stack[stack.length - 1].getToWord()).getRelation()
|
|
77
|
+
}
|
|
78
|
+
if (stack.length > 1) {
|
|
79
|
+
if (firstRelation != null && firstRelation.to() == top.getId()) {
|
|
80
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "RIGHTARC(" + firstRelation + ")"))
|
|
81
|
+
let word = wordList.splice(0, 1)[0]
|
|
82
|
+
stack.push(new StackWord(wordMap.get(word.getToWord()), word.getToWord()));
|
|
83
|
+
} else if (first != null && topRelation != null && topRelation.to() == first.getId()) {
|
|
84
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "LEFTARC(" + topRelation + ")"))
|
|
85
|
+
stack.pop()
|
|
86
|
+
} else if (wordList.length > 0) {
|
|
87
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "SHIFT"))
|
|
88
|
+
stack.push(wordList.splice(0, 1)[0])
|
|
89
|
+
} else {
|
|
90
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "REDUCE"))
|
|
91
|
+
stack.pop()
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
if (wordList.length > 0) {
|
|
95
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "SHIFT"))
|
|
96
|
+
stack.push(wordList.splice(0, 1)[0])
|
|
97
|
+
} else {
|
|
98
|
+
break
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return instanceList
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {Oracle} from "./Oracle";
|
|
2
|
+
import {State} from "./State";
|
|
3
|
+
import {Decision} from "./Decision";
|
|
4
|
+
import {Model} from "nlptoolkit-classification/dist/Model/Model";
|
|
5
|
+
import {SimpleInstanceGenerator} from "./SimpleInstanceGenerator";
|
|
6
|
+
|
|
7
|
+
export class ArcStandardOracle extends Oracle{
|
|
8
|
+
|
|
9
|
+
constructor(model: Model, windowSize: number) {
|
|
10
|
+
super(model, windowSize)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public makeDecision(state: State): Decision {
|
|
14
|
+
let instanceGenerator = new SimpleInstanceGenerator()
|
|
15
|
+
let instance = instanceGenerator.generate(state, this.windowSize, "")
|
|
16
|
+
let best = this.findBestValidStandardClassInfo(this.commandModel.predictProbability(instance), state);
|
|
17
|
+
let decisionCandidate = this.getDecisionCandidate(best)
|
|
18
|
+
if (decisionCandidate.getCommand() == Command.SHIFT) {
|
|
19
|
+
return new Decision(Command.SHIFT, null, 0.0)
|
|
20
|
+
}
|
|
21
|
+
return new Decision(decisionCandidate.getCommand(), decisionCandidate.getUniversalDependencyType(), 0.0)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected scoreDecisions(state: State, transitionSystem: TransitionSystem): Array<Decision> {
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {TransitionParser} from "./TransitionParser";
|
|
2
|
+
import {
|
|
3
|
+
UniversalDependencyTreeBankSentence
|
|
4
|
+
} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyTreeBankSentence";
|
|
5
|
+
import {Oracle} from "./Oracle";
|
|
6
|
+
import {Instance} from "nlptoolkit-classification/dist/Instance/Instance";
|
|
7
|
+
import {StackWord} from "./StackWord";
|
|
8
|
+
import {SimpleInstanceGenerator} from "./SimpleInstanceGenerator";
|
|
9
|
+
import {
|
|
10
|
+
UniversalDependencyTreeBankWord
|
|
11
|
+
} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyTreeBankWord";
|
|
12
|
+
import {State} from "./State";
|
|
13
|
+
import {StackRelation} from "./StackRelation";
|
|
14
|
+
|
|
15
|
+
export class ArcStandardTransitionParser extends TransitionParser{
|
|
16
|
+
|
|
17
|
+
constructor() {
|
|
18
|
+
super()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Checks if there are more relations with a specified ID in the list of words.
|
|
23
|
+
* @param wordList The list of words to check.
|
|
24
|
+
* @param id The ID to check for.
|
|
25
|
+
* @return True if no more relations with the specified ID are found; false otherwise.
|
|
26
|
+
*/
|
|
27
|
+
private checkForMoreRelation(wordList: Array<StackWord>, id: number){
|
|
28
|
+
for (let word of wordList) {
|
|
29
|
+
if (word.getWord().getRelation().to() == id) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Performs dependency parsing on the given sentence using the provided oracle.
|
|
38
|
+
* @param universalDependencyTreeBankSentence The sentence to be parsed.
|
|
39
|
+
* @param oracle The oracle used to make parsing decisions.
|
|
40
|
+
* @return The parsed sentence with dependency relations established.
|
|
41
|
+
*/
|
|
42
|
+
dependencyParse(universalDependencyTreeBankSentence: UniversalDependencyTreeBankSentence, oracle: Oracle): UniversalDependencyTreeBankSentence {
|
|
43
|
+
let sentence = this.createResultSentence(universalDependencyTreeBankSentence)
|
|
44
|
+
let state = this.initialState(sentence)
|
|
45
|
+
while (state.wordListSize() > 0 || state.stackSize() > 1) {
|
|
46
|
+
let decision = oracle.makeDecision(state)
|
|
47
|
+
switch (decision.getCommand()) {
|
|
48
|
+
case Command.SHIFT:
|
|
49
|
+
state.applyShift()
|
|
50
|
+
break
|
|
51
|
+
case Command.LEFTARC:
|
|
52
|
+
state.applyLeftArc(decision.getUniversalDependencyType())
|
|
53
|
+
break
|
|
54
|
+
case Command.RIGHTARC:
|
|
55
|
+
state.applyRightArc(decision.getUniversalDependencyType())
|
|
56
|
+
break
|
|
57
|
+
default:
|
|
58
|
+
break
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return sentence;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Simulates the parsing process for a given sentence using the Arc Standard parsing algorithm.
|
|
66
|
+
* @param sentence The sentence to be parsed.
|
|
67
|
+
* @param windowSize The size of the window used for feature generation.
|
|
68
|
+
* @return An ArrayList of {@link Instance} objects representing the parsed actions.
|
|
69
|
+
*/
|
|
70
|
+
simulateParse(sentence: UniversalDependencyTreeBankSentence, windowSize: number): Array<Instance> {
|
|
71
|
+
let instanceGenerator = new SimpleInstanceGenerator()
|
|
72
|
+
let instanceList = new Array<Instance>()
|
|
73
|
+
let wordList = new Array<StackWord>()
|
|
74
|
+
let stack = new Array<StackWord>()
|
|
75
|
+
for (let j = 0; j < sentence.wordCount(); j++) {
|
|
76
|
+
let word = sentence.getWord(j)
|
|
77
|
+
if (word instanceof UniversalDependencyTreeBankWord){
|
|
78
|
+
wordList.push(new StackWord(word, j + 1))
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
stack.push(new StackWord());
|
|
82
|
+
let state = new State(stack, wordList, new Array<StackRelation>())
|
|
83
|
+
if (wordList.length > 0) {
|
|
84
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "SHIFT"));
|
|
85
|
+
stack.push(wordList.splice(0, 1)[0])
|
|
86
|
+
if (wordList.length > 1) {
|
|
87
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "SHIFT"))
|
|
88
|
+
stack.push(wordList.splice(0, 1)[0])
|
|
89
|
+
}
|
|
90
|
+
while (wordList.length > 0 || stack.length > 1) {
|
|
91
|
+
let top = stack[stack.length - 1].getWord()
|
|
92
|
+
let topRelation = top.getRelation()
|
|
93
|
+
if (stack.length > 1) {
|
|
94
|
+
let beforeTop = stack[stack.length - 2].getWord();
|
|
95
|
+
let beforeTopRelation = beforeTop.getRelation()
|
|
96
|
+
if (beforeTop.getId() == topRelation.to() && this.checkForMoreRelation(wordList, top.getId())) {
|
|
97
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "RIGHTARC(" + topRelation + ")"))
|
|
98
|
+
stack.pop();
|
|
99
|
+
} else if (top.getId() == beforeTopRelation.to()) {
|
|
100
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "LEFTARC(" + beforeTopRelation + ")"))
|
|
101
|
+
stack.splice(stack.length - 2, 1)
|
|
102
|
+
} else {
|
|
103
|
+
if (wordList.length > 0) {
|
|
104
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "SHIFT"))
|
|
105
|
+
stack.push(wordList.splice(0, 1)[0])
|
|
106
|
+
} else {
|
|
107
|
+
break
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
if (wordList.length > 0) {
|
|
112
|
+
instanceList.push(instanceGenerator.generate(state, windowSize, "SHIFT"))
|
|
113
|
+
stack.push(wordList.splice(0, 1)[0])
|
|
114
|
+
} else {
|
|
115
|
+
break
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return instanceList
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {UniversalDependencyType} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyType";
|
|
2
|
+
|
|
3
|
+
export class Candidate {
|
|
4
|
+
private readonly command: Command;
|
|
5
|
+
private readonly universalDependencyType: UniversalDependencyType
|
|
6
|
+
|
|
7
|
+
constructor(command: Command, universalDependencyType: UniversalDependencyType) {
|
|
8
|
+
this.command = command
|
|
9
|
+
this.universalDependencyType = universalDependencyType
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
getCommand(): Command{
|
|
13
|
+
return this.command
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getUniversalDependencyType(): UniversalDependencyType{
|
|
17
|
+
return this.universalDependencyType
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {UniversalDependencyType} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyType";
|
|
2
|
+
import {Candidate} from "./Candidate";
|
|
3
|
+
|
|
4
|
+
export class Decision extends Candidate{
|
|
5
|
+
|
|
6
|
+
private readonly point: number
|
|
7
|
+
|
|
8
|
+
constructor(command: Command, universalDependencyType: UniversalDependencyType, point: number) {
|
|
9
|
+
super(command, universalDependencyType);
|
|
10
|
+
this.point = point
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getPoint(): number{
|
|
14
|
+
return this.point
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {State} from "./State";
|
|
2
|
+
import {
|
|
3
|
+
UniversalDependencyTreeBankWord
|
|
4
|
+
} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyTreeBankWord";
|
|
5
|
+
import {Attribute} from "nlptoolkit-classification/dist/Attribute/Attribute";
|
|
6
|
+
import {
|
|
7
|
+
UniversalDependencyTreeBankFeatures
|
|
8
|
+
} from "nlptoolkit-dependencyparser/dist/Universal/UniversalDependencyTreeBankFeatures";
|
|
9
|
+
import {DiscreteIndexedAttribute} from "nlptoolkit-classification/dist/Attribute/DiscreteIndexedAttribute";
|
|
10
|
+
import {Instance} from "nlptoolkit-classification/dist/Instance/Instance";
|
|
11
|
+
|
|
12
|
+
export abstract class InstanceGenerator {
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Abstract method for generating an instance based on the current state, window size, and command.
|
|
16
|
+
* @param state The current state of the parser.
|
|
17
|
+
* @param windowSize The size of the window used for feature extraction.
|
|
18
|
+
* @param command The command to be used for generating the instance.
|
|
19
|
+
* @return The generated {@link Instance} object.
|
|
20
|
+
*/
|
|
21
|
+
abstract generate(state: State, windowSize: number, command: string): Instance
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Adds an attribute for a specific feature type of a given word to the list of attributes.
|
|
25
|
+
* @param word The word whose feature value is used to create the attribute.
|
|
26
|
+
* @param attributes The list of attributes to which the new attribute will be added.
|
|
27
|
+
* @param featureType The type of the feature to be extracted from the word.
|
|
28
|
+
*/
|
|
29
|
+
private addAttributeForFeatureType(word: UniversalDependencyTreeBankWord,
|
|
30
|
+
attributes: Array<Attribute>,
|
|
31
|
+
featureType: string){
|
|
32
|
+
let feature = word.getFeatureValue(featureType)
|
|
33
|
+
let numberOfValues = UniversalDependencyTreeBankFeatures.numberOfValues("tr", featureType) + 1
|
|
34
|
+
if (feature != undefined){
|
|
35
|
+
attributes.push(new DiscreteIndexedAttribute(feature, UniversalDependencyTreeBankFeatures.featureValueIndex("tr", featureType, feature) + 1, numberOfValues))
|
|
36
|
+
} else {
|
|
37
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, numberOfValues))
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Adds a set of default (empty) attributes to the list of attributes. These attributes represent
|
|
43
|
+
* various feature types with default "null" values.
|
|
44
|
+
* @param attributes The list of attributes to which the default attributes will be added.
|
|
45
|
+
*/
|
|
46
|
+
protected addEmptyAttributes(attributes: Array<Attribute>){
|
|
47
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "PronType") + 1))
|
|
48
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "NumType") + 1))
|
|
49
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Number") + 1))
|
|
50
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Case") + 1))
|
|
51
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Definite") + 1))
|
|
52
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Degree") + 1))
|
|
53
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "VerbForm") + 1))
|
|
54
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Mood") + 1))
|
|
55
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Tense") + 1))
|
|
56
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Aspect") + 1))
|
|
57
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Voice") + 1))
|
|
58
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Evident") + 1))
|
|
59
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Polarity") + 1))
|
|
60
|
+
attributes.push(new DiscreteIndexedAttribute("null", 0, UniversalDependencyTreeBankFeatures.numberOfValues("tr", "Person") + 1))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Adds attributes for various feature types of a given word to the list of attributes.
|
|
65
|
+
* @param word The word whose feature values are used to create the attributes.
|
|
66
|
+
* @param attributes The list of attributes to which the new attributes will be added.
|
|
67
|
+
*/
|
|
68
|
+
protected addFeatureAttributes(word: UniversalDependencyTreeBankWord,
|
|
69
|
+
attributes: Array<Attribute>){
|
|
70
|
+
this.addAttributeForFeatureType(word, attributes, "PronType")
|
|
71
|
+
this.addAttributeForFeatureType(word, attributes, "NumType")
|
|
72
|
+
this.addAttributeForFeatureType(word, attributes, "Number")
|
|
73
|
+
this.addAttributeForFeatureType(word, attributes, "Case")
|
|
74
|
+
this.addAttributeForFeatureType(word, attributes, "Definite")
|
|
75
|
+
this.addAttributeForFeatureType(word, attributes, "Degree")
|
|
76
|
+
this.addAttributeForFeatureType(word, attributes, "VerbForm")
|
|
77
|
+
this.addAttributeForFeatureType(word, attributes, "Mood")
|
|
78
|
+
this.addAttributeForFeatureType(word, attributes, "Tense")
|
|
79
|
+
this.addAttributeForFeatureType(word, attributes, "Aspect")
|
|
80
|
+
this.addAttributeForFeatureType(word, attributes, "Voice")
|
|
81
|
+
this.addAttributeForFeatureType(word, attributes, "Evident")
|
|
82
|
+
this.addAttributeForFeatureType(word, attributes, "Polarity")
|
|
83
|
+
this.addAttributeForFeatureType(word, attributes, "Person")
|
|
84
|
+
}
|
|
85
|
+
}
|