• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PyThaiNLP / pythainlp / 7218641444

15 Dec 2023 06:09AM UTC coverage: 84.897% (-0.6%) from 85.496%
7218641444

push

github

wannaphong
Fix code block

6234 of 7343 relevant lines covered (84.9%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.0
/pythainlp/parse/core.py
1
# -*- coding: utf-8 -*-
2
# SPDX-FileCopyrightText: Copyright 2016-2023 PyThaiNLP Project
3
# SPDX-License-Identifier: Apache-2.0
4

5
from typing import List, Union
1✔
6

7

8
_tagger = None
1✔
9
_tagger_name = ""
1✔
10

11

12
def dependency_parsing(
1✔
13
    text: str,
14
    model: Union[str, None] = None,
15
    tag: str = "str",
16
    engine: str = "esupar",
17
) -> Union[List[List[str]], str]:
18
    """
19
    Dependency Parsing
20

21
    :param str text: text to apply dependency parsing to
22
    :param str model: model for using with engine \
23
        (for esupar and transformers_ud)
24
    :param str tag: output type (str or list)
25
    :param str engine: the name of dependency parser
26
    :return: str (conllu) or List
27
    :rtype: Union[List[List[str]], str]
28

29
    **Options for engine**
30
        * *esupar* (default) - Tokenizer, POS tagger and Dependency parser \
31
            using BERT/RoBERTa/DeBERTa models. `GitHub \
32
                <https://github.com/KoichiYasuoka/esupar>`_
33
        * *spacy_thai* - Tokenizer, POS tagger, and dependency parser \
34
            for the Thai language, using Universal Dependencies. \
35
            `GitHub <https://github.com/KoichiYasuoka/spacy-thai>`_
36
        * *transformers_ud* - TransformersUD \
37
            `GitHub <https://github.com/KoichiYasuoka/>`_
38
        * *ud_goeswith* - POS tagging and dependency parsing \
39
            using `goeswith` for subwords
40

41
    **Options for model (esupar engine)**
42
        * *th* (default) - KoichiYasuoka/roberta-base-thai-spm-upos model \
43
            `Huggingface \
44
            <https://huggingface.co/KoichiYasuoka/roberta-base-thai-spm-upos>`_
45
        * *KoichiYasuoka/deberta-base-thai-upos* - DeBERTa(V2) model \
46
            pre-trained on Thai Wikipedia texts for POS tagging and \
47
            dependency parsing `Huggingface \
48
            <https://huggingface.co/KoichiYasuoka/deberta-base-thai-upos>`_
49
        * *KoichiYasuoka/roberta-base-thai-syllable-upos* - RoBERTa model \
50
            pre-trained on Thai Wikipedia texts for POS tagging and \
51
            dependency parsing. (syllable level) `Huggingface \
52
            <https://huggingface.co/KoichiYasuoka/roberta-base-thai-syllable-upos>`_
53
        * *KoichiYasuoka/roberta-base-thai-char-upos* - RoBERTa model \
54
            pre-trained on Thai Wikipedia texts for POS tagging \
55
            and dependency parsing. (char level) `Huggingface \
56
            <https://huggingface.co/KoichiYasuoka/roberta-base-thai-char-upos>`_
57

58
    If you want to train models for esupar, you can read \
59
    `Huggingface <https://github.com/KoichiYasuoka/esupar>`_
60

61
    **Options for model (transformers_ud engine)**
62
        * *KoichiYasuoka/deberta-base-thai-ud-head* (default) - \
63
            DeBERTa(V2) model pretrained on Thai Wikipedia texts \
64
            for dependency parsing (head-detection using Universal \
65
            Dependencies) and question-answering, derived from \
66
            deberta-base-thai. \
67
            trained by th_blackboard.conll. `Huggingface \
68
            <https://huggingface.co/KoichiYasuoka/deberta-base-thai-ud-head>`_
69
        * *KoichiYasuoka/roberta-base-thai-spm-ud-head* - \
70
            roberta model pretrained on Thai Wikipedia texts \
71
            for dependency parsing. `Huggingface \
72
            <https://huggingface.co/KoichiYasuoka/roberta-base-thai-spm-ud-head>`_
73

74
    **Options for model (ud_goeswith engine)**
75
        * *KoichiYasuoka/deberta-base-thai-ud-goeswith* (default) - \
76
            This is a DeBERTa(V2) model pre-trained on Thai Wikipedia \
77
            texts for POS tagging and dependency parsing (using goeswith for subwords) \
78
            `Huggingface <https://huggingface.co/KoichiYasuoka/deberta-base-thai-ud-goeswith>`_
79

80
    :Example:
81
    ::
82

83
        from pythainlp.parse import dependency_parsing
84

85
        print(dependency_parsing("ผมเป็นคนดี", engine="esupar"))
86
        # output:
87
        # 1       ผม      _       PRON    _       _       3       nsubj   _       SpaceAfter=No
88
        # 2       เป็น     _       VERB    _       _       3       cop     _       SpaceAfter=No
89
        # 3       คน      _       NOUN    _       _       0       root    _       SpaceAfter=No
90
        # 4       ดี       _       VERB    _       _       3       acl     _       SpaceAfter=No
91

92
        print(dependency_parsing("ผมเป็นคนดี", engine="spacy_thai"))
93
        # output:
94
        # 1       ผม              PRON    PPRS    _       2       nsubj   _       SpaceAfter=No
95
        # 2       เป็น             VERB    VSTA    _       0       ROOT    _       SpaceAfter=No
96
        # 3       คนดี             NOUN    NCMN    _       2       obj     _       SpaceAfter=No
97
    """
98
    global _tagger, _tagger_name
99

100
    if _tagger_name != engine:
1✔
101
        if engine == "esupar":
1✔
102
            from pythainlp.parse.esupar_engine import Parse
1✔
103

104
            _tagger = Parse(model=model)
×
105
        elif engine == "transformers_ud":
×
106
            from pythainlp.parse.transformers_ud import Parse
×
107

108
            _tagger = Parse(model=model)
×
109
        elif engine == "spacy_thai":
×
110
            from pythainlp.parse.spacy_thai_engine import Parse
×
111

112
            _tagger = Parse()
×
113
        elif engine == "ud_goeswith":
×
114
            from pythainlp.parse.ud_goeswith import Parse
×
115

116
            _tagger = Parse(model=model)
×
117
        else:
118
            raise NotImplementedError("The engine doesn't support.")
×
119

120
    _tagger_name = engine
×
121

122
    return _tagger(text, tag=tag)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc