Punctuation Fullstop Truecase English
模型概述
模型特點
模型能力
使用案例
🚀 文本標點與大小寫處理模型
該模型可接收小寫、無標點的英文文本,一次性完成標點恢復、大小寫修正(首字母大寫)和句子邊界檢測(分割)任務。
🚀 快速開始
本模型接受小寫、無標點的英文文本作為輸入,能夠一次性完成標點恢復、大小寫修正(首字母大寫)和句子邊界檢測(分割)。與許多類似模型不同的是,該模型可以通過特殊的“首字母縮略詞”類別預測帶標點的首字母縮略詞(如“U.S.”),並通過多標籤大小寫預測處理任意大小寫的單詞(如“NATO”、“McDonald's”等)。
⚠️ 重要提示
文本生成小部件似乎不支持換行。相反,管道會在模型預測的句子邊界處插入換行符
\n
。
✨ 主要特性
- 多功能處理:一次性完成標點恢復、大小寫修正和句子邊界檢測。
- 特殊類別支持:能預測帶標點的首字母縮略詞和任意大小寫的單詞。
- 長文本處理:通過特定包可處理任意長度的輸入。
📦 安裝指南
使用此模型的簡便方法是安裝 punctuators:
pip install punctuators
如果這個包出現問題,請在社區板塊告知我(我會為每個模型更新它,但也經常把它弄壞!)。
💻 使用示例
基礎用法
from typing import List
from punctuators.models import PunctCapSegModelONNX
# 實例化這個模型
# 這將下載ONNX和SPE模型。若要清理,可從你的HF緩存目錄中刪除該模型。
m = PunctCapSegModelONNX.from_pretrained("pcs_en")
# 定義一些需要添加標點的輸入文本
input_texts: List[str] = [
# 我週末的真實經歷
"i woke up at 6 am and took the dog for a hike in the metacomet mountains we like to take morning adventures on the weekends",
"despite being mid march it snowed overnight and into the morning here in connecticut it was snowier up in the mountains than in the farmington valley where i live",
"when i got home i trained this model on the lambda cloud on an a100 gpu with about 10 million lines of text the total budget was less than 5 dollars",
# 我編造的包含首字母縮略詞的句子
"george hw bush was the president of the us for 8 years",
"i saw mr smith at the store he was shopping for a new lawn mower i suggested he get one of those new battery operated ones they're so much quieter",
# 看看模型對編造的首字母縮略詞的處理效果
"i went to the fgw store and bought a new tg optical scope",
# 維基百科今日特色文章摘要的前幾句話
"it's that man again itma was a radio comedy programme that was broadcast by the bbc for twelve series from 1939 to 1949 featuring tommy handley in the central role itma was a character driven comedy whose satirical targets included officialdom and the proliferation of minor wartime regulations parts of the scripts were rewritten in the hours before the broadcast to ensure topicality"
]
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()
具體輸出可能會因模型版本而異,以下是當前輸出:
預期輸出
In: i woke up at 6 am and took the dog for a hike in the metacomet mountains we like to take morning adventures on the weekends
Out: I woke up at 6 a.m. and took the dog for a hike in the Metacomet Mountains.
Out: We like to take morning adventures on the weekends.
In: despite being mid march it snowed overnight and into the morning here in connecticut it was snowier up in the mountains than in the farmington valley where i live
Out: Despite being mid March, it snowed overnight and into the morning.
Out: Here in Connecticut, it was snowier up in the mountains than in the Farmington Valley where I live.
In: when i got home i trained this model on the lambda cloud on an a100 gpu with about 10 million lines of text the total budget was less than 5 dollars
Out: When I got home, I trained this model on the Lambda Cloud.
Out: On an A100 GPU with about 10 million lines of text, the total budget was less than 5 dollars.
In: george hw bush was the president of the us for 8 years
Out: George H.W. Bush was the president of the U.S. for 8 years.
In: i saw mr smith at the store he was shopping for a new lawn mower i suggested he get one of those new battery operated ones they're so much quieter
Out: I saw Mr. Smith at the store he was shopping for a new lawn mower.
Out: I suggested he get one of those new battery operated ones.
Out: They're so much quieter.
In: i went to the fgw store and bought a new tg optical scope
Out: I went to the FGW store and bought a new TG optical scope.
In: it's that man again itma was a radio comedy programme that was broadcast by the bbc for twelve series from 1939 to 1949 featuring tommy handley in the central role itma was a character driven comedy whose satirical targets included officialdom and the proliferation of minor wartime regulations parts of the scripts were rewritten in the hours before the broadcast to ensure topicality
Out: It's that man again.
Out: ITMA was a radio comedy programme that was broadcast by the BBC for Twelve Series from 1939 to 1949, featuring Tommy Handley.
Out: In the central role, ITMA was a character driven comedy whose satirical targets included officialdom and the proliferation of minor wartime regulations.
Out: Parts of the scripts were rewritten in the hours before the broadcast to ensure topicality.
📚 詳細文檔
模型詳情
該模型實現瞭如下所示的流程圖,以下是每個步驟的簡要描述:
-
編碼: 模型首先使用子詞分詞器對文本進行分詞。這裡使用的分詞器是一個詞彙量為32k的
SentencePiece
模型。接著,輸入序列由一個基礎大小的Transformer進行編碼,該Transformer由6層組成,模型維度為512。 -
標點預測: 編碼後的序列被輸入到一個前饋分類網絡中,以預測標點符號。標點符號是針對每個子詞進行預測的,這樣可以正確處理首字母縮略詞。按子詞預測的一個間接好處是,允許模型在連續書寫語言(如中文)的通用圖中運行。
-
句子邊界檢測: 對於句子邊界檢測,我們通過嵌入對標點符號進行條件約束。每個標點預測用於為該標記選擇一個嵌入,該嵌入與編碼表示連接起來。句子邊界檢測(SBD)頭分析未加標點的序列編碼和標點預測,並預測哪些標記是句子邊界。
-
句子邊界移位和拼接: 在英語中,每個句子的第一個字符應該大寫。因此,我們應該將句子邊界信息傳遞給大小寫分類網絡。由於大小寫分類網絡是前饋的,沒有時間上下文,每個時間步必須嵌入它是否是句子的第一個單詞。因此,我們將二進制句子邊界決策向右移動一位:如果標記
N - 1
是句子邊界,則標記N
是句子的第一個單詞。將此信息與編碼文本拼接後,每個時間步都包含了SBD頭預測的是否是句子第一個單詞的信息。 -
大小寫預測: 在掌握了標點符號和句子邊界的信息後,一個分類網絡預測正確的大小寫。由於大小寫應該逐字符進行,分類網絡為每個標記進行
N
次預測,其中N
是子標記的長度。(實際上,N
是最長可能的子詞,多餘的預測會被忽略)。這種方案可以處理首字母縮略詞(如“NATO”)以及雙大寫單詞(如“MacDonald”)。
由於訓練嵌入的限制,模型的最大長度為256個子詞。不過,如上所述的 punctuators 包將透明地對長輸入的重疊子段進行預測,並在返回輸出之前合併結果,從而允許輸入任意長。
標點符號標記
該模型預測以下一組標點符號標記:
標記 | 描述 |
---|---|
NULL | 預測無標點符號 |
ACRONYM | 這個子詞中的每個字符都以句號結尾 |
. | 拉丁句號 |
, | 拉丁逗號 |
? | 拉丁問號 |
訓練詳情
訓練框架
該模型在 NeMo 框架的一個分支上進行訓練。
訓練數據
該模型使用了WMT的新聞爬取數據進行訓練。 大約使用了2021年和2012年的1000萬行數據。使用2012年的數據是為了嘗試減少偏差:年度新聞通常由少數話題主導,而2021年的新聞主要圍繞COVID討論。
侷限性
領域適用性
該模型在新聞數據上進行訓練,在對話或非正式數據上的表現可能不佳。
訓練數據噪聲
訓練數據存在噪聲,且未進行手動清理。
首字母縮略詞和縮寫
首字母縮略詞和縮寫的噪聲尤其大;下表顯示了每個標記在訓練數據中出現的不同變體數量。
標記 | 數量 |
---|---|
Mr | 115232 |
Mr. | 108212 |
標記 | 數量 |
---|---|
U.S. | 85324 |
US | 37332 |
U.S | 354 |
U.s | 108 |
u.S. | 65 |
因此,模型對首字母縮略詞和縮寫的預測可能有點不可預測。
句子邊界檢測目標
句子邊界檢測目標的一個假設是,輸入數據的每一行恰好是一個句子。然而,訓練數據中有相當一部分每行包含多個句子。因此,如果句子邊界與訓練數據中看到的錯誤相似,SBD頭可能會錯過明顯的句子邊界。
評估
在這些指標中,請記住以下幾點:
- 數據存在噪聲。
- 句子邊界和大小寫修正依賴於預測的標點符號,而標點預測是最困難的任務,有時可能會出錯。當以參考標點符號為條件時,大小寫修正和SBD指標相對於參考目標要高得多。
- 標點符號可能具有主觀性。例如:
Hello Frank, how's it going?
或者
Hello Frank. How's it going?
當句子更長、更實際時,這些歧義會大量存在,並影響所有三個分析指標。
測試數據和示例生成
每個測試示例都是使用以下步驟生成的:
- 連接10個隨機句子。
- 將連接後的句子轉換為小寫。
- 去除所有標點符號。
數據是新聞爬取數據的一個保留部分,已經進行了去重處理。使用了3000行數據,生成了3000個由10個句子組成的唯一示例。
評估結果
標點符號報告
label precision recall f1 support
<NULL> (label_id: 0) 98.83 98.49 98.66 446496
<ACRONYM> (label_id: 1) 74.15 94.26 83.01 697
. (label_id: 2) 90.64 92.99 91.80 30002
, (label_id: 3) 77.19 79.13 78.15 23321
? (label_id: 4) 76.58 74.56 75.56 1022
-------------------
micro avg 97.21 97.21 97.21 501538
macro avg 83.48 87.89 85.44 501538
weighted avg 97.25 97.21 97.23 501538
大小寫修正報告
# 使用預測的標點符號(與目標不一致)
label precision recall f1 support
LOWER (label_id: 0) 99.76 99.72 99.74 2020678
UPPER (label_id: 1) 93.32 94.20 93.76 83873
-------------------
micro avg 99.50 99.50 99.50 2104551
macro avg 96.54 96.96 96.75 2104551
weighted avg 99.50 99.50 99.50 2104551
# 使用參考標點符號(標點符號與目標匹配)
label precision recall f1 support
LOWER (label_id: 0) 99.83 99.81 99.82 2020678
UPPER (label_id: 1) 95.51 95.90 95.71 83873
-------------------
micro avg 99.66 99.66 99.66 2104551
macro avg 97.67 97.86 97.76 2104551
weighted avg 99.66 99.66 99.66 2104551
句子邊界檢測報告
# 使用預測的標點符號(與目標不一致)
label precision recall f1 support
NOSTOP (label_id: 0) 99.59 99.45 99.52 471608
FULLSTOP (label_id: 1) 91.47 93.53 92.49 29930
-------------------
micro avg 99.09 99.09 99.09 501538
macro avg 95.53 96.49 96.00 501538
weighted avg 99.10 99.09 99.10 501538
# 使用參考標點符號(標點符號與目標匹配)
label precision recall f1 support
NOSTOP (label_id: 0) 100.00 99.97 99.98 471608
FULLSTOP (label_id: 1) 99.63 99.93 99.78 32923
-------------------
micro avg 99.97 99.97 99.97 504531
macro avg 99.81 99.95 99.88 504531
weighted avg 99.97 99.97 99.97 504531
有趣的發現
嵌入分析
讓我們檢查嵌入(見上圖),看看模型是否有效地利用了它們。
這裡展示了每個標記嵌入之間的餘弦相似度:
NULL | ACRONYM | . | , | ? | |
---|---|---|---|---|---|
NULL | 1.00 | ||||
ACRONYM | -0.49 | 1.00 | |||
. | -1.00 | 0.48 | 1.00 | ||
, | 1.00 | -0.48 | -1.00 | 1.00 | |
? | -1.00 | 0.49 | 1.00 | -1.00 | 1.00 |
請記住,這些嵌入用於預測句子邊界……因此我們應該期望句號會聚集在一起。
實際上,我們看到NULL
和“,”是完全相同的,因為它們都對句子邊界沒有影響。
接下來,我們看到“.”和“?”是完全相同的,因為就句子邊界檢測而言,它們是完全相同的:強烈暗示句子結束。(不過,考慮到“.”在縮寫(如“Mr.”)後被預測,而這些縮寫不是完整的句子,我們可能會期望這些標記之間存在一些差異。)
此外,我們看到“.”和“?”與NULL
完全相反。這是可以預期的,因為這些標記通常暗示句子邊界,而NULL
和“,”則從不暗示。
最後,我們看到ACRONYM
與句號“.”和“?”相似,但不完全相同,與NULL
和“,”相差較遠,但不是相反。直覺表明,這是因為首字母縮略詞可以是完整的句子(“I live in the northern U.S. It's cold here.”),也可以不是(“It's 5 a.m. and I'm tired.”)。
📄 許可證
本項目採用Apache-2.0許可證。








