Model Overview
Model Features
Model Capabilities
Use Cases
🚀 Generic Model for Text2Text Generation
This model accepts lower-cased, unpunctuated, unsegmented text in 47 languages and performs punctuation restoration, true-casing, and sentence boundary detection.
🚀 Quick Start
The easy way to use this model is to install punctuators
:
pip install punctuators
Running the following script should load this model and run some texts:
Example Usage
```python from punctuators.models import PunctCapSegModelONNXInstantiate this model
This will download the ONNX and SPE models. To clean up, delete this model from your HF cache directory.
m = PunctCapSegModelONNX.from_pretrained("pcs_47lang")
Define some input texts to punctuate
input_texts: List[str] = [ "hola mundo cómo estás estamos bajo el sol y hace mucho calor santa coloma abre los huertos urbanos a las escuelas de la ciudad", "hello friend how's it going it's snowing outside right now in connecticut a large storm is moving in", "未來疫苗將有望覆蓋3歲以上全年齡段美國與北約軍隊已全部撤離還有鐵路公路在內的各項基建的來源都將枯竭", "በባለፈው ሳምንት ኢትዮጵያ ከሶማሊያ 3 ሺህ ወታደሮቿንም እንዳስወጣች የሶማሊያው ዳልሳን ሬድዮ ዘግቦ ነበር ጸጥታ ሃይሉና ህዝቡ ተቀናጅቶ በመስራቱ በመዲናዋ ላይ የታቀደው የጥፋት ሴራ ከሽፏል", "all human beings are born free and equal in dignity and rights they are endowed with reason and conscience and should act towards one another in a spirit of brotherhood", "सभी मनुष्य जन्म से मर्यादा और अधिकारों में स्वतंत्र और समान होते हैं वे तर्क और विवेक से संपन्न हैं तथा उन्हें भ्रातृत्व की भावना से परस्पर के प्रति कार्य करना चाहिए", "wszyscy ludzie rodzą się wolni i równi pod względem swej godności i swych praw są oni obdarzeni rozumem i sumieniem i powinni postępować wobec innych w duchu braterstwa", "tous les êtres humains naissent libres et égaux en dignité et en droits ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité", ] results: List[List[str]] = m.infer(input_texts) for input_text, output_texts in zip(input_texts, results): print(f"Input: {input_text}") print(f"Outputs:") for text in output_texts: print(f"\t{text}") print()
</details>
<details open>
<summary>Expected Output</summary>
```text
Input: hola mundo cómo estás estamos bajo el sol y hace mucho calor santa coloma abre los huertos urbanos a las escuelas de la ciudad
Outputs:
Hola Mundo, ¿cómo estás?
Estamos bajo el sol y hace mucho calor.
Santa Coloma abre los huertos urbanos a las escuelas de la ciudad.
Input: hello friend how's it going it's snowing outside right now in connecticut a large storm is moving in
Outputs:
Hello Friend, how's it going?
It's snowing outside right now.
In Connecticut, a large storm is moving in.
Input: 未來疫苗將有望覆蓋3歲以上全年齡段美國與北約軍隊已全部撤離還有鐵路公路在內的各項基建的來源都將枯竭
Outputs:
未來,疫苗將有望覆蓋3歲以上全年齡段。
美國與北約軍隊已全部撤離。
還有鐵路公路在內的各項基建的來源都將枯竭。
Input: በባለፈው ሳምንት ኢትዮጵያ ከሶማሊያ 3 ሺህ ወታደሮቿንም እንዳስወጣች የሶማሊያው ዳልሳን ሬድዮ ዘግቦ ነበር ጸጥታ ሃይሉና ህዝቡ ተቀናጅቶ በመስራቱ በመዲናዋ ላይ የታቀደው የጥፋት ሴራ ከሽፏል
Outputs:
በባለፈው ሳምንት ኢትዮጵያ ከሶማሊያ 3 ሺህ ወታደሮቿንም እንዳስወጣች የሶማሊያው ዳልሳን ሬድዮ ዘግቦ ነበር።
ጸጥታ ሃይሉና ህዝቡ ተቀናጅቶ በመስራቱ በመዲናዋ ላይ የታቀደው የጥፋት ሴራ ከሽፏል።
Input: all human beings are born free and equal in dignity and rights they are endowed with reason and conscience and should act towards one another in a spirit of brotherhood
Outputs:
All human beings are born free and equal in dignity and rights.
They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.
Input: सभी मनुष्य जन्म से मर्यादा और अधिकारों में स्वतंत्र और समान होते हैं वे तर्क और विवेक से संपन्न हैं तथा उन्हें भ्रातृत्व की भावना से परस्पर के प्रति कार्य करना चाहिए
Outputs:
सभी मनुष्य जन्म से मर्यादा और अधिकारों में स्वतंत्र और समान होते हैं।
वे तर्क और विवेक से संपन्न हैं तथा उन्हें भ्रातृत्व की भावना से परस्पर के प्रति कार्य करना चाहिए।
Input: wszyscy ludzie rodzą się wolni i równi pod względem swej godności i swych praw są oni obdarzeni rozumem i sumieniem i powinni postępować wobec innych w duchu braterstwa
Outputs:
Wszyscy ludzie rodzą się wolni i równi pod względem swej godności i swych praw.
Są oni obdarzeni rozumem i sumieniem i powinni postępować wobec innych w duchu braterstwa.
Input: tous les êtres humains naissent libres et égaux en dignité et en droits ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité
Outputs:
Tous les êtres humains naissent libres et égaux, en dignité et en droits.
Ils sont doués de raison et de conscience et doivent agir les uns envers les autres.
Dans un esprit de fraternité.
Note that "Mundo" and "Friend" are proper nouns in this usage, which is why the model consistently upper-cases similar tokens in multiple languages.
✨ Features
- Accepts lower-cased, unpunctuated, unsegmented text in 47 languages.
- Performs punctuation restoration, true-casing, and sentence boundary detection.
- Processes all languages with the same algorithm without the need for language tags or language-specific branches.
📦 Installation
pip install punctuators
💻 Usage Examples
Basic Usage
from punctuators.models import PunctCapSegModelONNX
# Instantiate this model
# This will download the ONNX and SPE models. To clean up, delete this model from your HF cache directory.
m = PunctCapSegModelONNX.from_pretrained("pcs_47lang")
# Define some input texts to punctuate
input_texts: List[str] = [
"hola mundo cómo estás estamos bajo el sol y hace mucho calor santa coloma abre los huertos urbanos a las escuelas de la ciudad",
"hello friend how's it going it's snowing outside right now in connecticut a large storm is moving in",
"未來疫苗將有望覆蓋3歲以上全年齡段美國與北約軍隊已全部撤離還有鐵路公路在內的各項基建的來源都將枯竭",
"በባለፈው ሳምንት ኢትዮጵያ ከሶማሊያ 3 ሺህ ወታደሮቿንም እንዳስወጣች የሶማሊያው ዳልሳን ሬድዮ ዘግቦ ነበር ጸጥታ ሃይሉና ህዝቡ ተቀናጅቶ በመስራቱ በመዲናዋ ላይ የታቀደው የጥፋት ሴራ ከሽፏል",
"all human beings are born free and equal in dignity and rights they are endowed with reason and conscience and should act towards one another in a spirit of brotherhood",
"सभी मनुष्य जन्म से मर्यादा और अधिकारों में स्वतंत्र और समान होते हैं वे तर्क और विवेक से संपन्न हैं तथा उन्हें भ्रातृत्व की भावना से परस्पर के प्रति कार्य करना चाहिए",
"wszyscy ludzie rodzą się wolni i równi pod względem swej godności i swych praw są oni obdarzeni rozumem i sumieniem i powinni postępować wobec innych w duchu braterstwa",
"tous les êtres humains naissent libres et égaux en dignité et en droits ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité",
]
results: List[List[str]] = m.infer(input_texts)
for input_text, output_texts in zip(input_texts, results):
print(f"Input: {input_text}")
print(f"Outputs:")
for text in output_texts:
print(f"\t{text}")
print()
📚 Documentation
Model Details
This model generally follows the graph shown below, with brief descriptions for each step following.
- Encoding:
The model begins by tokenizing the text with a subword tokenizer. The tokenizer used here is a
SentencePiece
model with a vocabulary size of 64k. Next, the input sequence is encoded with a base-sized Transformer, consisting of 6 layers with a model dimension of 512. - Post-punctuation: The encoded sequence is then fed into a classification network to predict "post" punctuation tokens. Post punctuation are punctuation tokens that may appear after a word, basically most normal punctuation. Post punctuation is predicted once per subword - further discussion is below.
- Re-encoding All subsequent tasks (true-casing, sentence boundary detection, and "pre" punctuation) are dependent on "post" punctuation. Therefore, we must conditional all further predictions on the post punctuation tokens. For this task, predicted punctation tokens are fed into an embedding layer, where embeddings represent each possible punctuation token. Each time step is mapped to a 4-dimensional embeddings, which is concatenated to the 512-dimensional encoding. The concatenated joint representation is re-encoded to confer global context to each time step to incorporate punctuation predictions into subsequent tasks.
- Pre-punctuation
After the re-encoding, another classification network predicts "pre" punctuation, or punctuation tokens that may appear before a word. In practice, this means the inverted question mark for Spanish and Asturian,
¿
. Note that a¿
can only appear if a?
is predicted, hence the conditioning. - Sentence boundary detection Parallel to the "pre" punctuation, another classification network predicts sentence boundaries from the re-encoded text. In all languages, sentence boundaries can occur only if a potential full stop is predicted, hence the conditioning.
- Shift and concat sentence boundaries
In many languages, the first character of each sentence should be upper-cased. Thus, we should feed the sentence boundary information to the true-case classification network. Since the true-case classification network is feed-forward and has no context, each time step must embed whether it is the first word of a sentence. Therefore, we shift the binary sentence boundary decisions to the right by one: if token
N - 1
is a sentence boundary, tokenN
is the first word of a sentence. Concatenating this with the re-encoded text, each time step contains whether it is the first word of a sentence as predicted by the SBD head. - True-case prediction
Armed with the knowledge of punctuation and sentence boundaries, a classification network predicts true-casing. Since true-casing should be done on a per-character basis, the classification network makes
N
predictions per token, whereN
is the length of the subtoken. (In practice,N
is the longest possible subword, and the extra predictions are ignored). This scheme captures acronyms, e.g., "NATO", as well as bi-capitalized words, e.g., "MacDonald".
Post-Punctuation Tokens
This model predicts the following set of "post" punctuation tokens after each subword:
Token | Description | Relevant Languages |
---|---|---|
. | Latin full stop | Many |
, | Latin comma | Many |
? | Latin question mark | Many |
? | Full-width question mark | Chinese, Japanese |
, | Full-width comma | Chinese, Japanese |
。 | Full-width full stop | Chinese, Japanese |
、 | Ideographic comma | Chinese, Japanese |
・ | Middle dot | Japanese |
। | Danda | Hindi, Bengali, Oriya |
؟ | Arabic question mark | Arabic |
; | Greek question mark | Greek |
። | Ethiopic full stop | Amharic |
፣ | Ethiopic comma | Amharic |
፧ | Ethiopic question mark | Amharic |
Pre-Punctuation Tokens
This model predicts the following set of "pre" punctuation tokens before each subword:
Token | Description | Relevant Languages |
---|---|---|
¿ | Inverted question mark | Spanish |
Training Details
This model was trained in the NeMo framework.
Training Data
This model was trained with News Crawl data from WMT. 1M lines of text for each language was used, except for a few low-resource languages which may have used less. Languages were chosen based on whether the News Crawl corpus contained enough reliable-quality data as judged by the author.
Limitations
- This model was trained on news data, and may not perform well on conversational or informal data.
- This model predicts punctuation only once per subword. This implies that some acronyms, e.g., 'U.S.', cannot properly be punctuated. This concession was accepted on two grounds:
- Such acronyms are rare, especially in the context of multi-lingual models.
- Punctuated acronyms are typically pronounced as individual characters, e.g., 'U.S.' vs. 'NATO'. Since the expected use-case of this model is the output of an ASR system, it is presumed that such pronunciations would be transcribed as separate tokens, e.g, 'u s' vs. 'us' (though this depends on the model's pre-processing).
- This model is unlikely to be of production quality. It was trained with "only" 1M lines per language, and the dev sets may have been noisy due to the nature of web-scraped news data. This is also a base-sized model with many languages and many tasks, so capacity may be limited.
- This model's maximum sequence length is 128, which is relatively short for an NLP problem.
After analyzing the limitations of this version, a future version of this model will attempt to improve the following points:
- Longer maximum length
- More training data
- More training steps
Evaluation
In these metrics, keep in mind that:
- The data is noisy.
- Sentence boundaries and true-casing are conditioned on predicted punctuation, which is the most difficult task and sometimes incorrect. When conditioning on reference punctuation, true-casing and SBD is practically 100% for most languages.
- Punctuation can be subjective. E.g.,
Hola mundo, ¿cómo estás?
orHola mundo. ¿Cómo estás?
When the sentences are longer and more practical, these ambiguities abound and affect all 3 analytics.
Test Data and Example Generation
Each test example was generated using the following procedure:
- Concatenate 5 random sentences.
- Lower-case the concatenated sentence.
- Remove all punctuation. The data is a held-out portion of News Crawl, which has been deduplicated. 2,000 lines of data per language was used, generating 2,000 unique examples of 5 sentences each. The last 4 sentences of each example were randomly sampled from the 2,000 and may be duplicated. Examples longer than the model's maximum length were truncated. The number of affected sentences can be estimated from the "full stop" support: with 2,000 sentences and 5 sentences per example, we expect 10,000 full stop targets total.
Selected Language Evaluation Reports
This model will likely be updated soon, so only a few languages are reported below.
English
``` punct_post test report: label precision recall f1 supportcap test report: label precision recall f1 support LOWER (label_id: 0) 99.63 99.49 99.56 526612 UPPER (label_id: 1) 89.19 91.84 90.50 24161 ------------------- micro avg 99.15 99.15 99.15 550773 macro avg 94.41 95.66 95.03 550773 weighted avg 99.17 99.15 99.16 550773
seg test report: label precision recall f1 support NOSTOP (label_id: 0) 99.37 99.42 99.39 162044 FULLSTOP (label_id: 1) 89.75 88.84 89.29 9222 ------------------- micro avg 98.85 98.85 98.85 171266 macro avg 94.56 94.13 94.34 171266 weighted avg 98.85 98.85 98.85 171266
</details>
<details>
<summary>Spanish</summary>
punct_pre test report:
label precision recall f1 support
punct_post test report:
label precision recall f1 support
cap test report: label precision recall f1 support LOWER (label_id: 0) 99.62 99.60 99.61 555041 UPPER (label_id: 1) 90.60 91.06 90.83 23538 ------------------- micro avg 99.25 99.25 99.25 578579 macro avg 95.11 95.33 95.22 578579 weighted avg 99.25 99.25 99.25 578579
</details>
## 📄 License
This project is licensed under the Apache-2.0 license.






