Views
No views yet
xlm-roberta fine-tuned to restore punctuation, true-case (capitalize),
and detect sentence boundaries (full stops) in 47 languages.PyPI)
and manual usuage (using the ONNX and SentencePiece models in this repo).punctuators package$ pip install punctuatorspunctuators API is a list (batch) of strings.
Each string will be punctuated, true-cased, and segmented on predicted full stops.
The output will therefore be a list of list of strings: one list of segmented sentences per input text.
To disable full stops, use m.infer(texts, apply_sbd=False).
The output will then be a list of strings: one punctuated, true-cased string per input text.1
2from typing import List
3
4from punctuators.models import PunctCapSegModelONNX
5
6m: PunctCapSegModelONNX = PunctCapSegModelONNX.from_pretrained(
7 "1-800-BAD-CODE/xlm-roberta_punctuation_fullstop_truecase"
8)
9
10input_texts: List[str] = [
11 "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",
12 "hello friend how's it going it's snowing outside right now in connecticut a large storm is moving in",
13 "未來疫苗將有望覆蓋3歲以上全年齡段美國與北約軍隊已全部撤離還有鐵路公路在內的各項基建的來源都將枯竭",
14 "በባለፈው ሳምንት ኢትዮጵያ ከሶማሊያ 3 ሺህ ወታደሮቿንም እንዳስወጣች የሶማሊያው ዳልሳን ሬድዮ ዘግቦ ነበር ጸጥታ ሃይሉና ህዝቡ ተቀናጅቶ በመስራቱ በመዲናዋ ላይ የታቀደው የጥፋት ሴራ ከሽፏል",
15 "こんにちは友人" "調子はどう" "今日は雨の日でしたね" "乾いた状態を保つために一日中室内で過ごしました",
16 "hallo freund wie geht's es war heute ein regnerischer tag nicht wahr ich verbrachte den tag drinnen um trocken zu bleiben",
17 "हैलो दोस्त ये कैसा चल रहा है आज बारिश का दिन था न मैंने सूखा रहने के लिए दिन घर के अंदर बिताया",
18 "كيف تجري الامور كان يومًا ممطرًا اليوم أليس كذلك قضيت اليوم في الداخل لأظل جافًا",
19]
20
21results: List[List[str]] = m.infer(
22 texts=input_texts, apply_sbd=True,
23)
24for input_text, output_texts in zip(input_texts, results):
25 print(f"Input: {input_text}")
26 print(f"Outputs:")
27 for text in output_texts:
28 print(f"\t{text}")
29 print()
301Input: 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
2Outputs:
3 Hola mundo, ¿cómo estás?
4 Estamos bajo el sol y hace mucho calor.
5 Santa Coloma abre los huertos urbanos a las escuelas de la ciudad.
6
7Input: hello friend how's it going it's snowing outside right now in connecticut a large storm is moving in
8Outputs:
9 Hello friend, how's it going?
10 It's snowing outside right now.
11 In Connecticut, a large storm is moving in.
12
13Input: 未來疫苗將有望覆蓋3歲以上全年齡段美國與北約軍隊已全部撤離還有鐵路公路在內的各項基建的來源都將枯竭
14Outputs:
15 未來,疫苗將有望覆蓋3歲以上全年齡段。
16 美國與北約軍隊已全部撤離。
17 還有,鐵路,公路在內的各項基建的來源都將枯竭。
18
19Input: በባለፈው ሳምንት ኢትዮጵያ ከሶማሊያ 3 ሺህ ወታደሮቿንም እንዳስወጣች የሶማሊያው ዳልሳን ሬድዮ ዘግቦ ነበር ጸጥታ ሃይሉና ህዝቡ ተቀናጅቶ በመስራቱ በመዲናዋ ላይ የታቀደው የጥፋት ሴራ ከሽፏል
20Outputs:
21 በባለፈው ሳምንት ኢትዮጵያ ከሶማሊያ 3 ሺህ ወታደሮቿንም እንዳስወጣች የሶማሊያው ዳልሳን ሬድዮ ዘግቦ ነበር።
22 ጸጥታ ሃይሉና ህዝቡ ተቀናጅቶ በመስራቱ በመዲናዋ ላይ የታቀደው የጥፋት ሴራ ከሽፏል።
23
24Input: こんにちは友人調子はどう今日は雨の日でしたね乾いた状態を保つために一日中室内で過ごしました
25Outputs:
26 こんにちは、友人、調子はどう?
27 今日は雨の日でしたね。
28 乾いた状態を保つために、一日中、室内で過ごしました。
29
30Input: hallo freund wie geht's es war heute ein regnerischer tag nicht wahr ich verbrachte den tag drinnen um trocken zu bleiben
31Outputs:
32 Hallo Freund, wie geht's?
33 Es war heute ein regnerischer Tag, nicht wahr?
34 Ich verbrachte den Tag drinnen, um trocken zu bleiben.
35
36Input: हैलो दोस्त ये कैसा चल रहा है आज बारिश का दिन था न मैंने सूखा रहने के लिए दिन घर के अंदर बिताया
37Outputs:
38 हैलो दोस्त, ये कैसा चल रहा है?
39 आज बारिश का दिन था न, मैंने सूखा रहने के लिए दिन घर के अंदर बिताया।
40
41Input: كيف تجري الامور كان يومًا ممطرًا اليوم أليس كذلك قضيت اليوم في الداخل لأظل جافًا
42Outputs:
43 كيف تجري الامور؟
44 كان يومًا ممطرًا اليوم، أليس كذلك؟
45 قضيت اليوم في الداخل لأظل جافًا.
461from typing import List
2
3import numpy as np
4import onnxruntime as ort
5from huggingface_hub import hf_hub_download
6from omegaconf import OmegaConf
7from sentencepiece import SentencePieceProcessor
8
9# Download the models from HF hub. Note: to clean up, you can find these files in your HF cache directory
10spe_path = hf_hub_download(repo_id="1-800-BAD-CODE/xlm-roberta_punctuation_fullstop_truecase", filename="sp.model")
11onnx_path = hf_hub_download(repo_id="1-800-BAD-CODE/xlm-roberta_punctuation_fullstop_truecase", filename="model.onnx")
12config_path = hf_hub_download(
13 repo_id="1-800-BAD-CODE/xlm-roberta_punctuation_fullstop_truecase", filename="config.yaml"
14)
15
16# Load the SP model
17tokenizer: SentencePieceProcessor = SentencePieceProcessor(spe_path) # noqa
18# Load the ONNX graph
19ort_session: ort.InferenceSession = ort.InferenceSession(onnx_path)
20# Load the model config with labels, etc.
21config = OmegaConf.load(config_path)
22# Potential classification labels before each subtoken
23pre_labels: List[str] = config.pre_labels
24# Potential classification labels after each subtoken
25post_labels: List[str] = config.post_labels
26# Special class that means "predict nothing"
27null_token = config.get("null_token", "<NULL>")
28# Special class that means "all chars in this subtoken end with a period", e.g., "am" -> "a.m."
29acronym_token = config.get("acronym_token", "<ACRONYM>")
30# Not used in this example, but if your sequence exceed this value, you need to fold it over multiple inputs
31max_len = config.max_length
32# For reference only, graph has no language-specific behavior
33languages: List[str] = config.languages
34
35# Encode some input text, adding BOS + EOS
36input_text = "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"
37input_ids = [tokenizer.bos_id()] + tokenizer.EncodeAsIds(input_text) + [tokenizer.eos_id()]
38
39# Create a numpy array with shape [B, T], as the graph expects as input.
40# Note that we do not pass lengths to the graph; if you are using a batch, padding should be tokenizer.pad_id() and the
41# graph's attention mechanisms will ignore pad_id() without requiring explicit sequence lengths.
42input_ids_arr: np.array = np.array([input_ids])
43
44# Run the graph, get outputs for all analytics
45pre_preds, post_preds, cap_preds, sbd_preds = ort_session.run(None, {"input_ids": input_ids_arr})
46# Squeeze off the batch dimensions and convert to lists
47pre_preds = pre_preds[0].tolist()
48post_preds = post_preds[0].tolist()
49cap_preds = cap_preds[0].tolist()
50sbd_preds = sbd_preds[0].tolist()
51
52# Segmented sentences
53output_texts: List[str] = []
54# Current sentence, which is built until we hit a sentence boundary prediction
55current_chars: List[str] = []
56# Iterate over the outputs, ignoring the first (BOS) and final (EOS) predictions and tokens
57for token_idx in range(1, len(input_ids) - 1):
58 token = tokenizer.IdToPiece(input_ids[token_idx])
59 # Simple SP decoding
60 if token.startswith("▁") and current_chars:
61 current_chars.append(" ")
62 # Token-level predictions
63 pre_label = pre_labels[pre_preds[token_idx]]
64 post_label = post_labels[post_preds[token_idx]]
65 # If we predict "pre-punct", insert it before this token
66 if pre_label != null_token:
67 current_chars.append(pre_label)
68 # Iterate over each char. Skip SP's space token,
69 char_start = 1 if token.startswith("▁") else 0
70 for token_char_idx, char in enumerate(token[char_start:], start=char_start):
71 # If this char should be capitalized, apply upper case
72 if cap_preds[token_idx][token_char_idx]:
73 char = char.upper()
74 # Append char
75 current_chars.append(char)
76 # if this is an acronym, add a period after every char (p.m., a.m., etc.)
77 if post_label == acronym_token:
78 current_chars.append(".")
79 # Maybe this subtoken ends with punctuation
80 if post_label != null_token and post_label != acronym_token:
81 current_chars.append(post_label)
82
83 # If this token is a sentence boundary, finalize the current sentence and reset
84 if sbd_preds[token_idx]:
85 output_texts.append("".join(current_chars))
86 current_chars.clear()
87
88# Maybe push final sentence, if the final token was not classified as a sentence boundary
89if current_chars:
90 output_texts.append("".join(current_chars))
91
92# Pretty print
93print(f"Input: {input_text}")
94print("Outputs:")
95for text in output_texts:
96 print(f"\t{text}")
971Input: 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
2Outputs:
3 Hola mundo, ¿cómo estás?
4 Estamos bajo el sol y hace mucho calor.
5 Santa Coloma abre los huertos urbanos a las escuelas de la ciudad.
N predictions per subtoken, where N is the number of characters in the subtoken.
In practice, N is the maximum subtoken length and extra predictions are ignored. Essentially, true-casing is
modeled as a multi-label problem. This allows for upper-casing arbitrary characters, e.g., "NATO", "MacDonald", "mRNA", etc.xlm-roberta SentencePiece model was adjusted to correctly encode
the text. Per HF's comments,1# Original fairseq vocab and spm vocab must be "aligned":
2# Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
3# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
4# fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-'
5# spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a'1from sentencepiece import SentencePieceProcessor
2from sentencepiece.sentencepiece_model_pb2 import ModelProto
3
4m = ModelProto()
5m.ParseFromString(open("/path/to/xlmroberta/sentencepiece.bpe.model", "rb").read())
6
7pieces = list(m.pieces)
8pieces = (
9 [
10 ModelProto.SentencePiece(piece="<s>", type=ModelProto.SentencePiece.Type.CONTROL),
11 ModelProto.SentencePiece(piece="<pad>", type=ModelProto.SentencePiece.Type.CONTROL),
12 ModelProto.SentencePiece(piece="</s>", type=ModelProto.SentencePiece.Type.CONTROL),
13 ModelProto.SentencePiece(piece="<unk>", type=ModelProto.SentencePiece.Type.UNKNOWN),
14 ]
15 + pieces[3:]
16 + [ModelProto.SentencePiece(piece="<mask>", type=ModelProto.SentencePiece.Type.USER_DEFINED)]
17)
18del m.pieces[:]
19m.pieces.extend(pieces)
20
21with open("/path/to/new/sp.model", "wb") as f:
22 f.write(m.SerializeToString())| Token | Description | Relevant Languages |
|---|---|---|
| <NULL> | No punctuation | All |
| <ACRONYM> | Every character in this subword is followed by a period | Primarily English, some European |
| . | 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 |
| Token | Description | Relevant Languages |
|---|---|---|
| <NULL> | No punctuation | All |
| ¿ | Inverted question mark | Spanish |
tensorboard log on tensorboard.dev.¿ (see metrics below).
Since ¿ is a rare token, especially in the context of a 47-language model, Spanish questions were over-sampled
by selecting more of these sentences from additional training data that was not used. However, this seems to have
"over-corrected" the problem and a lot of Spanish question marks are predicted.Hola mundo, ¿cómo estás?Hola mundo. ¿Cómo estás?i begins with sentence i and is followed by 10 random
sentences selected from the 3,000 sentence test set.1punct_post test report:
2 label precision recall f1 support
3 <NULL> (label_id: 0) 99.25 98.43 98.84 564908
4 <ACRONYM> (label_id: 1) 63.14 84.67 72.33 613
5 . (label_id: 2) 90.97 93.91 92.42 32040
6 , (label_id: 3) 73.95 84.32 78.79 24271
7 ? (label_id: 4) 79.05 81.94 80.47 1041
8 ? (label_id: 5) 0.00 0.00 0.00 0
9 , (label_id: 6) 0.00 0.00 0.00 0
10 。 (label_id: 7) 0.00 0.00 0.00 0
11 、 (label_id: 8) 0.00 0.00 0.00 0
12 ・ (label_id: 9) 0.00 0.00 0.00 0
13 । (label_id: 10) 0.00 0.00 0.00 0
14 ؟ (label_id: 11) 0.00 0.00 0.00 0
15 ، (label_id: 12) 0.00 0.00 0.00 0
16 ; (label_id: 13) 0.00 0.00 0.00 0
17 ። (label_id: 14) 0.00 0.00 0.00 0
18 ፣ (label_id: 15) 0.00 0.00 0.00 0
19 ፧ (label_id: 16) 0.00 0.00 0.00 0
20 -------------------
21 micro avg 97.60 97.60 97.60 622873
22 macro avg 81.27 88.65 84.57 622873
23 weighted avg 97.77 97.60 97.67 622873cap test report:
label precision recall f1 support
LOWER (label_id: 0) 99.72 99.85 99.78 2134956
UPPER (label_id: 1) 96.33 93.52 94.91 91996
-------------------
micro avg 99.59 99.59 99.59 2226952
macro avg 98.03 96.68 97.34 2226952
weighted avg 99.58 99.59 99.58 2226952seg test report:
label precision recall f1 support
NOSTOP (label_id: 0) 99.99 99.98 99.99 591540
FULLSTOP (label_id: 1) 99.61 99.89 99.75 34333
-------------------
micro avg 99.97 99.97 99.97 625873
macro avg 99.80 99.93 99.87 625873
weighted avg 99.97 99.97 99.97 6258731 punct_pre test report:
2 label precision recall f1 support
3 <NULL> (label_id: 0) 99.94 99.89 99.92 636941
4 ¿ (label_id: 1) 56.73 71.35 63.20 1288
5 -------------------
6 micro avg 99.83 99.83 99.83 638229
7 macro avg 78.34 85.62 81.56 638229
8 weighted avg 99.85 99.83 99.84 638229punct_post test report:
label precision recall f1 support
<NULL> (label_id: 0) 99.19 98.41 98.80 578271
<ACRONYM> (label_id: 1) 30.10 56.36 39.24 55
. (label_id: 2) 91.92 93.12 92.52 30856
, (label_id: 3) 72.98 82.44 77.42 27761
? (label_id: 4) 52.77 71.85 60.85 1286
? (label_id: 5) 0.00 0.00 0.00 0
, (label_id: 6) 0.00 0.00 0.00 0
。 (label_id: 7) 0.00 0.00 0.00 0
、 (label_id: 8) 0.00 0.00 0.00 0
・ (label_id: 9) 0.00 0.00 0.00 0
। (label_id: 10) 0.00 0.00 0.00 0
؟ (label_id: 11) 0.00 0.00 0.00 0
، (label_id: 12) 0.00 0.00 0.00 0
; (label_id: 13) 0.00 0.00 0.00 0
። (label_id: 14) 0.00 0.00 0.00 0
፣ (label_id: 15) 0.00 0.00 0.00 0
፧ (label_id: 16) 0.00 0.00 0.00 0
-------------------
micro avg 97.40 97.40 97.40 638229
macro avg 69.39 80.44 73.77 638229
weighted avg 97.60 97.40 97.48 638229cap test report:
label precision recall f1 support
LOWER (label_id: 0) 99.82 99.86 99.84 2324724
UPPER (label_id: 1) 95.92 94.70 95.30 79266
-------------------
micro avg 99.69 99.69 99.69 2403990
macro avg 97.87 97.28 97.57 2403990
weighted avg 99.69 99.69 99.69 2403990seg test report:
label precision recall f1 support
NOSTOP (label_id: 0) 99.99 99.96 99.98 607057
FULLSTOP (label_id: 1) 99.31 99.88 99.60 34172
-------------------
micro avg 99.96 99.96 99.96 641229
macro avg 99.65 99.92 99.79 641229
weighted avg 99.96 99.96 99.96 6412291punct_post test report:
2 label precision recall f1 support
3 <NULL> (label_id: 0) 99.83 99.28 99.56 729664
4 <ACRONYM> (label_id: 1) 0.00 0.00 0.00 0
5 . (label_id: 2) 0.00 0.00 0.00 0
6 , (label_id: 3) 0.00 0.00 0.00 0
7 ? (label_id: 4) 0.00 0.00 0.00 0
8 ? (label_id: 5) 0.00 0.00 0.00 0
9 , (label_id: 6) 0.00 0.00 0.00 0
10 。 (label_id: 7) 0.00 0.00 0.00 0
11 、 (label_id: 8) 0.00 0.00 0.00 0
12 ・ (label_id: 9) 0.00 0.00 0.00 0
13 । (label_id: 10) 0.00 0.00 0.00 0
14 ؟ (label_id: 11) 0.00 0.00 0.00 0
15 ، (label_id: 12) 0.00 0.00 0.00 0
16 ; (label_id: 13) 0.00 0.00 0.00 0
17 ። (label_id: 14) 91.27 97.90 94.47 25341
18 ፣ (label_id: 15) 61.93 82.11 70.60 5818
19 ፧ (label_id: 16) 67.41 81.73 73.89 1177
20 -------------------
21 micro avg 99.08 99.08 99.08 762000
22 macro avg 80.11 90.26 84.63 762000
23 weighted avg 99.21 99.08 99.13 762000cap test report:
label precision recall f1 support
LOWER (label_id: 0) 98.40 98.03 98.21 1064
UPPER (label_id: 1) 71.23 75.36 73.24 69
-------------------
micro avg 96.65 96.65 96.65 1133
macro avg 84.81 86.69 85.73 1133
weighted avg 96.74 96.65 96.69 1133seg test report:
label precision recall f1 support
NOSTOP (label_id: 0) 99.99 99.85 99.92 743158
FULLSTOP (label_id: 1) 95.20 99.62 97.36 21842
-------------------
micro avg 99.85 99.85 99.85 765000
macro avg 97.59 99.74 98.64 765000
weighted avg 99.85 99.85 99.85 7650001punct_post test report:
2 label precision recall f1 support
3 <NULL> (label_id: 0) 99.53 97.31 98.41 435611
4 <ACRONYM> (label_id: 1) 0.00 0.00 0.00 0
5 . (label_id: 2) 0.00 0.00 0.00 0
6 , (label_id: 3) 0.00 0.00 0.00 0
7 ? (label_id: 4) 0.00 0.00 0.00 0
8 ? (label_id: 5) 81.85 87.31 84.49 1513
9 , (label_id: 6) 74.08 93.67 82.73 35921
10 。 (label_id: 7) 96.51 96.93 96.72 32097
11 、 (label_id: 8) 0.00 0.00 0.00 0
12 ・ (label_id: 9) 0.00 0.00 0.00 0
13 । (label_id: 10) 0.00 0.00 0.00 0
14 ؟ (label_id: 11) 0.00 0.00 0.00 0
15 ، (label_id: 12) 0.00 0.00 0.00 0
16 ; (label_id: 13) 0.00 0.00 0.00 0
17 ። (label_id: 14) 0.00 0.00 0.00 0
18 ፣ (label_id: 15) 0.00 0.00 0.00 0
19 ፧ (label_id: 16) 0.00 0.00 0.00 0
20 -------------------
21 micro avg 97.00 97.00 97.00 505142
22 macro avg 87.99 93.81 90.59 505142
23 weighted avg 97.48 97.00 97.15 505142cap test report:
label precision recall f1 support
LOWER (label_id: 0) 94.89 94.98 94.94 2951
UPPER (label_id: 1) 81.34 81.03 81.18 796
-------------------
micro avg 92.02 92.02 92.02 3747
macro avg 88.11 88.01 88.06 3747
weighted avg 92.01 92.02 92.01 3747seg test report:
label precision recall f1 support
NOSTOP (label_id: 0) 99.99 99.97 99.98 473642
FULLSTOP (label_id: 1) 99.55 99.90 99.72 34500
-------------------
micro avg 99.96 99.96 99.96 508142
macro avg 99.77 99.93 99.85 508142
weighted avg 99.96 99.96 99.96 5081421punct_post test report:
2 label precision recall f1 support
3 <NULL> (label_id: 0) 99.34 95.90 97.59 406341
4 <ACRONYM> (label_id: 1) 0.00 0.00 0.00 0
5 . (label_id: 2) 0.00 0.00 0.00 0
6 , (label_id: 3) 0.00 0.00 0.00 0
7 ? (label_id: 4) 0.00 0.00 0.00 0
8 ? (label_id: 5) 70.55 73.56 72.02 1456
9 , (label_id: 6) 0.00 0.00 0.00 0
10 。 (label_id: 7) 94.38 96.95 95.65 32537
11 、 (label_id: 8) 54.28 87.62 67.03 18610
12 ・ (label_id: 9) 28.18 71.64 40.45 1100
13 । (label_id: 10) 0.00 0.00 0.00 0
14 ؟ (label_id: 11) 0.00 0.00 0.00 0
15 ، (label_id: 12) 0.00 0.00 0.00 0
16 ; (label_id: 13) 0.00 0.00 0.00 0
17 ። (label_id: 14) 0.00 0.00 0.00 0
18 ፣ (label_id: 15) 0.00 0.00 0.00 0
19 ፧ (label_id: 16) 0.00 0.00 0.00 0
20 -------------------
21 micro avg 95.51 95.51 95.51 460044
22 macro avg 69.35 85.13 74.55 460044
23 weighted avg 96.91 95.51 96.00 460044cap test report:
label precision recall f1 support
LOWER (label_id: 0) 92.33 94.03 93.18 4174
UPPER (label_id: 1) 83.51 79.46 81.43 1587
-------------------
micro avg 90.02 90.02 90.02 5761
macro avg 87.92 86.75 87.30 5761
weighted avg 89.90 90.02 89.94 5761seg test report:
label precision recall f1 support
NOSTOP (label_id: 0) 99.99 99.92 99.96 428544
FULLSTOP (label_id: 1) 99.07 99.87 99.47 34500
-------------------
micro avg 99.92 99.92 99.92 463044
macro avg 99.53 99.90 99.71 463044
weighted avg 99.92 99.92 99.92 4630441punct_post test report:
2 label precision recall f1 support
3 <NULL> (label_id: 0) 99.75 99.44 99.59 560358
4 <ACRONYM> (label_id: 1) 0.00 0.00 0.00 0
5 . (label_id: 2) 0.00 0.00 0.00 0
6 , (label_id: 3) 69.55 78.48 73.75 8084
7 ? (label_id: 4) 63.30 87.07 73.31 317
8 ? (label_id: 5) 0.00 0.00 0.00 0
9 , (label_id: 6) 0.00 0.00 0.00 0
10 。 (label_id: 7) 0.00 0.00 0.00 0
11 、 (label_id: 8) 0.00 0.00 0.00 0
12 ・ (label_id: 9) 0.00 0.00 0.00 0
13 । (label_id: 10) 96.92 98.66 97.78 32118
14 ؟ (label_id: 11) 0.00 0.00 0.00 0
15 ، (label_id: 12) 0.00 0.00 0.00 0
16 ; (label_id: 13) 0.00 0.00 0.00 0
17 ። (label_id: 14) 0.00 0.00 0.00 0
18 ፣ (label_id: 15) 0.00 0.00 0.00 0
19 ፧ (label_id: 16) 0.00 0.00 0.00 0
20 -------------------
21 micro avg 99.11 99.11 99.11 600877
22 macro avg 82.38 90.91 86.11 600877
23 weighted avg 99.17 99.11 99.13 600877cap test report:
label precision recall f1 support
LOWER (label_id: 0) 97.19 96.72 96.95 2466
UPPER (label_id: 1) 89.14 90.60 89.86 734
-------------------
micro avg 95.31 95.31 95.31 3200
macro avg 93.17 93.66 93.41 3200
weighted avg 95.34 95.31 95.33 3200seg test report:
label precision recall f1 support
NOSTOP (label_id: 0) 100.00 99.99 99.99 569472
FULLSTOP (label_id: 1) 99.82 99.99 99.91 34405
-------------------
micro avg 99.99 99.99 99.99 603877
macro avg 99.91 99.99 99.95 603877
weighted avg 99.99 99.99 99.99 6038771punct_post test report:
2 label precision recall f1 support
3 <NULL> (label_id: 0) 99.30 96.94 98.10 688043
4 <ACRONYM> (label_id: 1) 93.33 77.78 84.85 18
5 . (label_id: 2) 93.31 93.78 93.54 28175
6 , (label_id: 3) 0.00 0.00 0.00 0
7 ? (label_id: 4) 0.00 0.00 0.00 0
8 ? (label_id: 5) 0.00 0.00 0.00 0
9 , (label_id: 6) 0.00 0.00 0.00 0
10 。 (label_id: 7) 0.00 0.00 0.00 0
11 、 (label_id: 8) 0.00 0.00 0.00 0
12 ・ (label_id: 9) 0.00 0.00 0.00 0
13 । (label_id: 10) 0.00 0.00 0.00 0
14 ؟ (label_id: 11) 65.93 82.79 73.40 860
15 ، (label_id: 12) 44.89 79.20 57.30 20941
16 ; (label_id: 13) 0.00 0.00 0.00 0
17 ። (label_id: 14) 0.00 0.00 0.00 0
18 ፣ (label_id: 15) 0.00 0.00 0.00 0
19 ፧ (label_id: 16) 0.00 0.00 0.00 0
20 -------------------
21 micro avg 96.29 96.29 96.29 738037
22 macro avg 79.35 86.10 81.44 738037
23 weighted avg 97.49 96.29 96.74 738037cap test report:
label precision recall f1 support
LOWER (label_id: 0) 97.10 99.49 98.28 4137
UPPER (label_id: 1) 98.71 92.89 95.71 1729
-------------------
micro avg 97.55 97.55 97.55 5866
macro avg 97.90 96.19 96.99 5866
weighted avg 97.57 97.55 97.52 5866seg test report:
label precision recall f1 support
NOSTOP (label_id: 0) 99.99 99.97 99.98 710456
FULLSTOP (label_id: 1) 99.39 99.85 99.62 30581
-------------------
micro avg 99.97 99.97 99.97 741037
macro avg 99.69 99.91 99.80 741037
weighted avg 99.97 99.97 99.97 7410371from typing import List
2
3from punctuators.models import PunctCapSegModelONNX
4
5m: PunctCapSegModelONNX = PunctCapSegModelONNX.from_pretrained(
6 "1-800-BAD-CODE/xlm-roberta_punctuation_fullstop_truecase"
7)
8
9input_texts = [
10 "the us is a nato member as a nato member the country enjoys security guarantees notably article 5",
11 "the us is a nhtg member as a nhtg member the country enjoys security guarantees notably article 5",
12 "the us is a tuny member as a tuny member the country enjoys security guarantees notably article 5",
13 "connor andrew mcdavid is a canadian professional ice hockey centre and captain of the edmonton oilers of the national hockey league the oilers selected him first overall in the 2015 nhl entry draft mcdavid spent his childhood playing ice hockey against older children",
14 "please rsvp for the party asap preferably before 8 pm tonight",
15]
16
17results: List[List[str]] = m.infer(
18 texts=input_texts, apply_sbd=True,
19)
20for input_text, output_texts in zip(input_texts, results):
21 print(f"Input: {input_text}")
22 print(f"Outputs:")
23 for text in output_texts:
24 print(f"\t{text}")
25 print()
261Input: the us is a nato member as a nato member the country enjoys security guarantees notably article 5
2Outputs:
3 The U.S. is a NATO member.
4 As a NATO member, the country enjoys security guarantees, notably Article 5.
5
6Input: the us is a nhtg member as a nhtg member the country enjoys security guarantees notably article 5
7Outputs:
8 The U.S. is a NHTG member.
9 As a NHTG member, the country enjoys security guarantees, notably Article 5.
10
11Input: the us is a tuny member as a tuny member the country enjoys security guarantees notably article 5
12Outputs:
13 The U.S. is a Tuny member.
14 As a Tuny member, the country enjoys security guarantees, notably Article 5.
15
16Input: connor andrew mcdavid is a canadian professional ice hockey centre and captain of the edmonton oilers of the national hockey league the oilers selected him first overall in the 2015 nhl entry draft mcdavid spent his childhood playing ice hockey against older children
17Outputs:
18 Connor Andrew McDavid is a Canadian professional ice hockey centre and captain of the Edmonton Oilers of the National Hockey League.
19 The Oilers selected him first overall in the 2015 NHL entry draft.
20 McDavid spent his childhood playing ice hockey against older children.
21
22Input: please rsvp for the party asap preferably before 8 pm tonight
23Outputs:
24 Please RSVP for the party ASAP, preferably before 8 p.m. tonight.