|
| 1 | +import CorefMention from './coref-mention'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @class |
| 5 | + * @classdesc Class representing an CorefChain |
| 6 | + */ |
| 7 | +class CorefChain { |
| 8 | + /** |
| 9 | + * Create an CorefChain |
| 10 | + * @param {Array.<CorefMention>} mentions |
| 11 | + */ |
| 12 | + constructor(mentions) { |
| 13 | + this._mentions = mentions; |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Retrieves all the contained CorefMention instances |
| 18 | + * @returns {Array.<CorefMention>} mentions |
| 19 | + */ |
| 20 | + mentions() { |
| 21 | + return this._mentions; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Retrieves a CorefMention at the index specified |
| 26 | + * @param {number} index |
| 27 | + * @returns {CorefMention} mention |
| 28 | + */ |
| 29 | + mention(index) { |
| 30 | + return this._mentions[index]; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Retrieves the first representative mention |
| 35 | + * @returns {CorefMention} mention |
| 36 | + */ |
| 37 | + representative() { |
| 38 | + return this._mentions.find(mention => mention.isRepresentativeMention()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Retrieves all the non-representative mentions |
| 43 | + * @returns {Array.<CorefMention>} mentions |
| 44 | + */ |
| 45 | + nonRepresentatives() { |
| 46 | + return this._mentions.filter(mention => !mention.isRepresentativeMention()); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Gets or sets a Document reference for the current coref-chain |
| 51 | + * @param {Document} doc |
| 52 | + * @returns {Document} doc |
| 53 | + */ |
| 54 | + document(doc = null) { |
| 55 | + if (doc) { |
| 56 | + this._document = doc; |
| 57 | + } |
| 58 | + |
| 59 | + return this._document; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Update an instance of CorefChain with Document references to Sentence(s) and their Token(s) |
| 64 | + * @param {Document} doc - a Document object, the same one used to generate corefs annotations |
| 65 | + * @returns {CorefChain} chain - The current chain instance |
| 66 | + */ |
| 67 | + fromDocument(doc) { |
| 68 | + this._mentions.forEach((mention) => { |
| 69 | + const sentence = doc.sentence(mention.sentNum() - 1); |
| 70 | + const token = sentence.token(mention.startIndex() - 1); |
| 71 | + mention.sentence(sentence); |
| 72 | + mention.token(token); |
| 73 | + }); |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Update an instance of CorefChain with data provided by a JSON |
| 79 | + * @param {Array.<CorefMentionJSON>} data - A sentence corefs mentions chain, as |
| 80 | + * returned by CoreNLP API service |
| 81 | + * @returns {CorefChain} chain - The current chain instance |
| 82 | + */ |
| 83 | + fromJSON(data) { |
| 84 | + this._mentions = data.map(mention => CorefMention.fromJSON(mention)); |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + toJSON() { |
| 89 | + return [...this._mentions]; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get an instance of CorefChain from a given JSON of sentence corefs |
| 94 | + * @param {Array.<CorefMentionJSON>} data - The sentence corefs data, as |
| 95 | + * returned by CoreNLP API service |
| 96 | + * @returns {CorefChain} sentenchain - A new CorefChain instance |
| 97 | + */ |
| 98 | + static fromJSON(data) { |
| 99 | + const instance = new this(); |
| 100 | + return instance.fromJSON(data); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export default CorefChain; |
0 commit comments