Skip to main content
Version: Next 🚧

pos_tag

Label words with their part-of-speech tags.

Usage

from underthesea import pos_tag

text = "Chợ thịt chó nổi tiếng ở Sài Gòn bị truy quét"
tagged = pos_tag(text)
print(tagged)
# [('Chợ', 'N'), ('thịt', 'N'), ('chó', 'N'), ('nổi tiếng', 'A'),
# ('ở', 'E'), ('Sài Gòn', 'Np'), ('bị', 'V'), ('truy quét', 'V')]

Function Signature

def pos_tag(
sentence: str,
format: str = None,
model: str = None
) -> list[tuple[str, str]]

Parameters

ParameterTypeDefaultDescription
sentencestrThe input text to tag
formatstrNoneOutput format (currently only None supported)
modelstrNonePath to custom model

Returns

TypeDescription
list[tuple[str, str]]List of (word, POS tag) tuples

POS Tags

TagDescriptionExample
NNounchợ, thịt, chó
NpProper nounSài Gòn, Việt Nam
VVerbbị, truy quét
AAdjectivenổi tiếng, đẹp
PPronountôi, bạn, nó
RAdverbrất, đang, sẽ
EPrepositionở, trong, trên
CConjunctionvà, hoặc, nhưng
MNumbermột, hai, ba
LDeterminercác, những, mọi
XUnknown-
CHPunctuation. , ? !

Examples

Basic Usage

from underthesea import pos_tag

text = "Chợ thịt chó nổi tiếng ở Sài Gòn bị truy quét"
tagged = pos_tag(text)
for word, tag in tagged:
print(f"{word}: {tag}")
# Chợ: N
# thịt: N
# chó: N
# nổi tiếng: A
# ở: E
# Sài Gòn: Np
# bị: V
# truy quét: V

Filtering by POS Tag

text = "Tôi yêu Việt Nam vì Việt Nam rất đẹp"
tagged = pos_tag(text)

# Get all nouns
nouns = [word for word, tag in tagged if tag in ('N', 'Np')]
print(nouns)
# ['Việt Nam', 'Việt Nam']

# Get all verbs
verbs = [word for word, tag in tagged if tag == 'V']
print(verbs)
# ['yêu']

Processing Multiple Sentences

sentences = [
"Hà Nội là thủ đô của Việt Nam",
"Thành phố Hồ Chí Minh là thành phố lớn nhất"
]

for sentence in sentences:
tagged = pos_tag(sentence)
print(tagged)

Notes

  • Word segmentation is performed automatically before POS tagging
  • The model is trained on Vietnamese treebank data
  • Proper nouns (Np) include names, locations, organizations