File size: 5,602 Bytes
e4b9a7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | # -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""
Thank https://dev.to/ton_ami/text-data-augmentation-synonym-replacement-4h8l
"""
__all__ = [
"WordNetAug",
"postype2wordnet",
]
import itertools
from collections import OrderedDict
from typing import List
from nltk.corpus import wordnet as wn
from pythainlp.corpus import wordnet
from pythainlp.tag import pos_tag
from pythainlp.tokenize import word_tokenize
orchid = {
"": "",
# NOUN
"NOUN": wn.NOUN,
"NCMN": wn.NOUN,
"NTTL": wn.NOUN,
"CNIT": wn.NOUN,
"CLTV": wn.NOUN,
"CMTR": wn.NOUN,
"CFQC": wn.NOUN,
"CVBL": wn.NOUN,
# VERB
"VACT": wn.VERB,
"VSTA": wn.VERB,
# PROPN
"PROPN": "",
"NPRP": "",
# ADJ
"ADJ": wn.ADJ,
"NONM": wn.ADJ,
"VATT": wn.ADJ,
"DONM": wn.ADJ,
# ADV
"ADV": wn.ADV,
"ADVN": wn.ADV,
"ADVI": wn.ADV,
"ADVP": wn.ADV,
"ADVS": wn.ADV,
# INT
"INT": "",
# PRON
"PRON": "",
"PPRS": "",
"PDMN": "",
"PNTR": "",
# DET
"DET": "",
"DDAN": "",
"DDAC": "",
"DDBQ": "",
"DDAQ": "",
"DIAC": "",
"DIBQ": "",
"DIAQ": "",
# NUM
"NUM": "",
"NCNM": "",
"NLBL": "",
"DCNM": "",
# AUX
"AUX": "",
"XVBM": "",
"XVAM": "",
"XVMM": "",
"XVBB": "",
"XVAE": "",
# ADP
"ADP": "",
"RPRE": "",
# CCONJ
"CCONJ": "",
"JCRG": "",
# SCONJ
"SCONJ": "",
"PREL": "",
"JSBR": "",
"JCMP": "",
# PART
"PART": "",
"FIXN": "",
"FIXV": "",
"EAFF": "",
"EITT": "",
"AITT": "",
"NEG": "",
# PUNCT
"PUNCT": "",
"PUNC": "",
}
def postype2wordnet(pos: str, corpus: str):
"""
Convert part-of-speech type to wordnet type
:param str pos: POS type
:param str corpus: part-of-speech corpus
**Options for corpus**
* *orchid* - Orchid Corpus
"""
if corpus not in ["orchid"]:
return None
return orchid[pos]
class WordNetAug:
"""
Text Augment using wordnet
"""
def __init__(self):
pass
def find_synonyms(
self, word: str, pos: str = None, postag_corpus: str = "orchid"
) -> List[str]:
"""
Find synonyms using wordnet
:param str word: word
:param str pos: part-of-speech type
:param str postag_corpus: name of POS tag corpus
:return: list of synonyms
:rtype: List[str]
"""
self.synonyms = []
if pos is None:
self.list_synsets = wordnet.synsets(word)
else:
self.p2w_pos = postype2wordnet(pos, postag_corpus)
if self.p2w_pos != "":
self.list_synsets = wordnet.synsets(word, pos=self.p2w_pos)
else:
self.list_synsets = wordnet.synsets(word)
for self.synset in wordnet.synsets(word):
for self.syn in self.synset.lemma_names(lang="tha"):
self.synonyms.append(self.syn)
self.synonyms_without_duplicates = list(
OrderedDict.fromkeys(self.synonyms)
)
return self.synonyms_without_duplicates
def augment(
self,
sentence: str,
tokenize: object = word_tokenize,
max_syn_sent: int = 6,
postag: bool = True,
postag_corpus: str = "orchid",
) -> List[List[str]]:
"""
Text Augment using wordnet
:param str sentence: Thai sentence
:param object tokenize: function for tokenizing words
:param int max_syn_sent: maximum number of synonymous sentences
:param bool postag: use part-of-speech
:param str postag_corpus: name of POS tag corpus
:return: list of synonyms
:rtype: List[Tuple[str]]
:Example:
::
from pythainlp.augment import WordNetAug
aug = WordNetAug()
aug.augment("เราชอบไปโรงเรียน")
# output: [('เรา', 'ชอบ', 'ไป', 'ร.ร.'),
('เรา', 'ชอบ', 'ไป', 'รร.'),
('เรา', 'ชอบ', 'ไป', 'โรงเรียน'),
('เรา', 'ชอบ', 'ไป', 'อาคารเรียน'),
('เรา', 'ชอบ', 'ไปยัง', 'ร.ร.'),
('เรา', 'ชอบ', 'ไปยัง', 'รร.')]
"""
new_sentences = []
self.list_words = tokenize(sentence)
self.list_synonym = []
self.p_all = 1
if postag:
self.list_pos = pos_tag(self.list_words, corpus=postag_corpus)
for word, pos in self.list_pos:
self.temp = self.find_synonyms(word, pos, postag_corpus)
if not self.temp:
self.list_synonym.append([word])
else:
self.list_synonym.append(self.temp)
self.p_all *= len(self.temp)
else:
for word in self.list_words:
self.temp = self.find_synonyms(word)
if not self.temp:
self.list_synonym.append([word])
else:
self.list_synonym.append(self.temp)
self.p_all *= len(self.temp)
if max_syn_sent > self.p_all:
max_syn_sent = self.p_all
for x in list(itertools.product(*self.list_synonym))[0:max_syn_sent]:
new_sentences.append(x)
return new_sentences
|