r"""A dict structure holding parsed annotations. A document is a subclass of ``dict`` and it supports every interface of ``dict``\. Additionally, it supports interfaces to deal with various linguistic structures. Its ``str`` and ``dict`` representations are made to be compatible with
(self, *args, **kwargs)
| 16 | |
| 17 | class Document(dict): |
| 18 | def __init__(self, *args, **kwargs) -> None: |
| 19 | r"""A dict structure holding parsed annotations. A document is a subclass of ``dict`` and it supports every |
| 20 | interface of ``dict``\. Additionally, it supports interfaces to deal with various linguistic structures. Its |
| 21 | ``str`` and ``dict`` representations are made to be compatible with JSON serialization. |
| 22 | |
| 23 | Args: |
| 24 | *args: An iterator of key-value pairs. |
| 25 | **kwargs: Arguments from ``**`` operator. |
| 26 | |
| 27 | Examples:: |
| 28 | |
| 29 | # Create a document |
| 30 | doc = Document( |
| 31 | tok=[["晓美焰", "来到", "北京", "立方庭", "参观", "自然", "语义", "科技", "公司"]], |
| 32 | pos=[["NR", "VV", "NR", "NR", "VV", "NN", "NN", "NN", "NN"]], |
| 33 | ner=[[["晓美焰", "PERSON", 0, 1], ["北京立方庭", "LOCATION", 2, 4], |
| 34 | ["自然语义科技公司", "ORGANIZATION", 5, 9]]], |
| 35 | dep=[[[2, "nsubj"], [0, "root"], [4, "name"], [2, "dobj"], [2, "conj"], |
| 36 | [9, "compound"], [9, "compound"], [9, "compound"], [5, "dobj"]]] |
| 37 | ) |
| 38 | |
| 39 | # print(doc) or str(doc) to get its JSON representation |
| 40 | print(doc) |
| 41 | |
| 42 | # Access an annotation by its task name |
| 43 | print(doc['tok']) |
| 44 | |
| 45 | # Get number of sentences |
| 46 | print(f'It has {doc.count_sentences()} sentence(s)') |
| 47 | |
| 48 | # Access the n-th sentence |
| 49 | print(doc.squeeze(0)['tok']) |
| 50 | |
| 51 | # Pretty print it right in your console or notebook |
| 52 | doc.pretty_print() |
| 53 | |
| 54 | # To save the pretty prints in a str |
| 55 | pretty_text: str = '\n\n'.join(doc.to_pretty()) |
| 56 | |
| 57 | """ |
| 58 | super().__init__(*args, **kwargs) |
| 59 | for k, v in list(self.items()): |
| 60 | if not v: |
| 61 | continue |
| 62 | if k == 'con': |
| 63 | if isinstance(v, Tree) or isinstance(v[0], Tree): |
| 64 | continue |
| 65 | flat = isinstance(v[0], str) |
| 66 | if flat: |
| 67 | v = [v] |
| 68 | ls = [] |
| 69 | for each in v: |
| 70 | if not isinstance(each, Tree): |
| 71 | ls.append(list_to_tree(each)) |
| 72 | if flat: |
| 73 | ls = ls[0] |
| 74 | self[k] = ls |
| 75 | elif k == 'amr': |
nothing calls this directly
no test coverage detected