Hướng dẫn add punctuation to text python - thêm dấu chấm câu vào văn bản python

Đó là năm 2022 và HuggingFace có một số giải pháp học máy dễ cài đặt để thêm dấu câu trở lại vào bảng điểm:

https://huggingface.co/felflare/bert-restore-punctuation

Example:

from rpunct import RestorePuncts
# The default language is 'english'
rpunct = RestorePuncts(use_cuda=False)
rpunct.punctuate("""in 2018 cornell researchers built a high-powered detector that in combination with an algorithm-driven process called ptychography set a world record
by tripling the resolution of a state-of-the-art electron microscope as successful as it was that approach had a weakness it only worked with ultrathin samples that were
a few atoms thick anything thicker would cause the electrons to scatter in ways that could not be disentangled now a team again led by david muller the samuel b eckert
professor of engineering has bested its own record by a factor of two with an electron microscope pixel array detector empad that incorporates even more sophisticated
3d reconstruction algorithms the resolution is so fine-tuned the only blurring that remains is the thermal jiggling of the atoms themselves""")

Mô hình này đã được đào tạo trên 560.000 câu Yelp và có độ chính xác 90%.

Cài đặt ghi chú

Kể từ tháng 9 năm 2022, gói rpunct có lỗi mà nó sẽ không chạy mà không có GPU, nhưng có một chiếc nĩa được vá hỗ trợ use_cuda=False kwarg trong ví dụ của tôi (chậm hơn nhưng hoạt động trên tất cả các bộ xử lý). Để cài đặt ngã ba này thay thế, hãy làm điều này:

>>> from deepmultilingualpunctuation import PunctuationModel
>>> model = PunctuationModel()
Downloading config.json: 100%|█████████████████████████████████████████████████████████████████| 892/892 [00:00<00:00, 335kB/s]
Downloading pytorch_model.bin: 100%|██████████████████████████████████████████████████████| 2.08G/2.08G [04:54<00:00, 7.60MB/s]
Downloading tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 406/406 [00:00<00:00, 216kB/s]
Downloading sentencepiece.bpe.model: 100%|████████████████████████████████████████████████| 4.83M/4.83M [00:00<00:00, 8.08MB/s]
Downloading special_tokens_map.json: 100%|█████████████████████████████████████████████████████| 239/239 [00:00<00:00, 158kB/s]
/opt/anaconda3/envs/punct/lib/python3.9/site-packages/transformers/pipelines/token_classification.py:135: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="none"` instead.
  warnings.warn(
>>> text = "My name is Clara and I live in Berkeley California Ist das eine Frage Frau Müller"
>>> result = model.restore_punctuation(text)
>>> print(result)
My name is Clara and I live in Berkeley, California. Ist das eine Frage, Frau Müller?
0

Tùy chọn 2 (tốt hơn)

Đây là một mô hình HuggingFace khác với độ chính xác tương tự và được cài đặt chính xác lần đầu tiên

https://huggingface.co/oliverguhr/fullstop-punctuation-multilang-large

>>> from deepmultilingualpunctuation import PunctuationModel
>>> model = PunctuationModel()
Downloading config.json: 100%|█████████████████████████████████████████████████████████████████| 892/892 [00:00<00:00, 335kB/s]
Downloading pytorch_model.bin: 100%|██████████████████████████████████████████████████████| 2.08G/2.08G [04:54<00:00, 7.60MB/s]
Downloading tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 406/406 [00:00<00:00, 216kB/s]
Downloading sentencepiece.bpe.model: 100%|████████████████████████████████████████████████| 4.83M/4.83M [00:00<00:00, 8.08MB/s]
Downloading special_tokens_map.json: 100%|█████████████████████████████████████████████████████| 239/239 [00:00<00:00, 158kB/s]
/opt/anaconda3/envs/punct/lib/python3.9/site-packages/transformers/pipelines/token_classification.py:135: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="none"` instead.
  warnings.warn(
>>> text = "My name is Clara and I live in Berkeley California Ist das eine Frage Frau Müller"
>>> result = model.restore_punctuation(text)
>>> print(result)
My name is Clara and I live in Berkeley, California. Ist das eine Frage, Frau Müller?
1

usage:

>>> from deepmultilingualpunctuation import PunctuationModel
>>> model = PunctuationModel()
Downloading config.json: 100%|█████████████████████████████████████████████████████████████████| 892/892 [00:00<00:00, 335kB/s]
Downloading pytorch_model.bin: 100%|██████████████████████████████████████████████████████| 2.08G/2.08G [04:54<00:00, 7.60MB/s]
Downloading tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 406/406 [00:00<00:00, 216kB/s]
Downloading sentencepiece.bpe.model: 100%|████████████████████████████████████████████████| 4.83M/4.83M [00:00<00:00, 8.08MB/s]
Downloading special_tokens_map.json: 100%|█████████████████████████████████████████████████████| 239/239 [00:00<00:00, 158kB/s]
/opt/anaconda3/envs/punct/lib/python3.9/site-packages/transformers/pipelines/token_classification.py:135: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="none"` instead.
  warnings.warn(
>>> text = "My name is Clara and I live in Berkeley California Ist das eine Frage Frau Müller"
>>> result = model.restore_punctuation(text)
>>> print(result)
My name is Clara and I live in Berkeley, California. Ist das eine Frage, Frau Müller?

Tôi đã xác minh điều này thực sự đã làm việc ngoài hộp trong một ống dẫn mới ở trên. Mô hình thứ 2 này cũng tự động phát hiện và hỗ trợ 4 ngôn ngữ.

Hướng dẫn add punctuation to text python - thêm dấu chấm câu vào văn bản python

Đây là một cái nĩa của Ottokar Tilk's Punctuator2 đã được dọn sạch thành một gói Python3 chính thức với thử nghiệm.

Bản demo và demo2 and DEMO2

Một mô hình mạng thần kinh tái phát hai chiều với cơ chế chú ý để khôi phục dấu câu liên từ bị thiếu trong văn bản không phân đoạn.

Mô hình có thể được đào tạo trong hai giai đoạn (giai đoạn thứ hai là tùy chọn):

  1. Giai đoạn đầu tiên được đào tạo về văn bản chú thích dấu câu. Ở đây mô hình học cách khôi phục puncuting chỉ dựa trên các tính năng văn bản.
  2. Giai đoạn thứ hai tùy chọn có thể được đào tạo về dấu câu và tạm dừng văn bản chú thích. Trong giai đoạn này, mô hình học cách kết hợp thời lượng tạm dừng với các tính năng và điều chỉnh văn bản với miền đích. Nếu tạm dừng được bỏ qua thì chỉ thích ứng được thực hiện. Giai đoạn thứ hai với thời lượng tạm dừng có thể được sử dụng ví dụ để khôi phục dấu câu trong đầu ra hệ thống nhận dạng giọng nói tự động.

Cài đặt

Để cài đặt:

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator

Ngoài ra, bạn sẽ cần một mô hình được đào tạo. Bạn có thể tạo các hướng dẫn của riêng mình dưới đây hoặc bạn có thể sử dụng mô hình được đào tạo trước từ đây.

Đặt các mô hình này vào thư mục

>>> from deepmultilingualpunctuation import PunctuationModel
>>> model = PunctuationModel()
Downloading config.json: 100%|█████████████████████████████████████████████████████████████████| 892/892 [00:00<00:00, 335kB/s]
Downloading pytorch_model.bin: 100%|██████████████████████████████████████████████████████| 2.08G/2.08G [04:54<00:00, 7.60MB/s]
Downloading tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 406/406 [00:00<00:00, 216kB/s]
Downloading sentencepiece.bpe.model: 100%|████████████████████████████████████████████████| 4.83M/4.83M [00:00<00:00, 8.08MB/s]
Downloading special_tokens_map.json: 100%|█████████████████████████████████████████████████████| 239/239 [00:00<00:00, 158kB/s]
/opt/anaconda3/envs/punct/lib/python3.9/site-packages/transformers/pipelines/token_classification.py:135: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="none"` instead.
  warnings.warn(
>>> text = "My name is Clara and I live in Berkeley California Ist das eine Frage Frau Müller"
>>> result = model.restore_punctuation(text)
>>> print(result)
My name is Clara and I live in Berkeley, California. Ist das eine Frage, Frau Müller?
2, mặc định là
>>> from deepmultilingualpunctuation import PunctuationModel
>>> model = PunctuationModel()
Downloading config.json: 100%|█████████████████████████████████████████████████████████████████| 892/892 [00:00<00:00, 335kB/s]
Downloading pytorch_model.bin: 100%|██████████████████████████████████████████████████████| 2.08G/2.08G [04:54<00:00, 7.60MB/s]
Downloading tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 406/406 [00:00<00:00, 216kB/s]
Downloading sentencepiece.bpe.model: 100%|████████████████████████████████████████████████| 4.83M/4.83M [00:00<00:00, 8.08MB/s]
Downloading special_tokens_map.json: 100%|█████████████████████████████████████████████████████| 239/239 [00:00<00:00, 158kB/s]
/opt/anaconda3/envs/punct/lib/python3.9/site-packages/transformers/pipelines/token_classification.py:135: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="none"` instead.
  warnings.warn(
>>> text = "My name is Clara and I live in Berkeley California Ist das eine Frage Frau Müller"
>>> result = model.restore_punctuation(text)
>>> print(result)
My name is Clara and I live in Berkeley, California. Ist das eine Frage, Frau Müller?
3.

Ví dụ: để tải xuống

>>> from deepmultilingualpunctuation import PunctuationModel
>>> model = PunctuationModel()
Downloading config.json: 100%|█████████████████████████████████████████████████████████████████| 892/892 [00:00<00:00, 335kB/s]
Downloading pytorch_model.bin: 100%|██████████████████████████████████████████████████████| 2.08G/2.08G [04:54<00:00, 7.60MB/s]
Downloading tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 406/406 [00:00<00:00, 216kB/s]
Downloading sentencepiece.bpe.model: 100%|████████████████████████████████████████████████| 4.83M/4.83M [00:00<00:00, 8.08MB/s]
Downloading special_tokens_map.json: 100%|█████████████████████████████████████████████████████| 239/239 [00:00<00:00, 158kB/s]
/opt/anaconda3/envs/punct/lib/python3.9/site-packages/transformers/pipelines/token_classification.py:135: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="none"` instead.
  warnings.warn(
>>> text = "My name is Clara and I live in Berkeley California Ist das eine Frage Frau Müller"
>>> result = model.restore_punctuation(text)
>>> print(result)
My name is Clara and I live in Berkeley, California. Ist das eine Frage, Frau Müller?
4, kích hoạt môi trường ảo của bạn và chạy:

. .env/bin/activate
mkdir -p ~/.punctuator
cd ~/.punctuator
gdown https://drive.google.com/uc?id=0B7BsN5f2F1fZd1Q0aXlrUDhDbnM

Để tải xuống các tệp mô hình khác, hãy tìm ID Google Drive thông qua liên kết chia sẻ và thay thế cho lệnh trên.

Cách sử dụng

Để sử dụng từ dòng lệnh:

cat input.txt | python punctuator.py model.pcl output.txt

Để sử dụng từ Python:

from punctuator import Punctuator
p = Punctuator('model.pcl')
print(p.punctuate('some text'))

Nó hoạt động tốt như thế nào?

  • Một bản demo đang hoạt động có thể được nhìn thấy ở đây: http://bark.phon.ioc.ee/punctuator
  • Bạn có thể cố gắng cạnh tranh với mô hình này ở đây: http://bark.phon.ioc.ee/puncuator/game

Hãy nhớ rằng tất cả các điểm được đưa ra dưới đây là trên văn bản chưa được phân đoạn và chúng tôi không sử dụng các tính năng thịnh vượng, vì vậy, trong số những thứ khác, mô hình phải phát hiện ranh giới câu ngoài loại biên (? Câu hỏi,. về các tính năng văn bản. Điểm số được tính toán trên bộ kiểm tra.

Tốc độ đào tạo với các cài đặt mặc định, cài đặt Theano tối ưu và GPU hiện đại phải là khoảng 10000 từ mỗi giây.

Các mô hình trước có thể được tải xuống tại đây (mô hình demo + 2 từ giấy Interspeech).

Tiếng Anh Ted nói chuyện

Thiết lập đào tạo Kích thước: từ 2,1m. Giai đoạn đầu tiên chỉ. Thông tin chi tiết có thể được tìm thấy trong bài báo này. Để so sánh, mô hình trước đây của chúng tôi có điểm số F1 tổng thể là 50,8.

CHẤM CÂUĐỘ CHÍNH XÁCGỢI LẠIF-SCORE
, Dấu phẩy64.4 45.2 53.1
?DẤU CHẤM HỎI67.5 58.7 62.8
.GIAI ĐOẠN72.3 71.5 71.9
Tổng thể68.9 58.1 63.1

Tiếng Anh Europarl v7

Bộ đào tạo Kích thước: 40m từ. Giai đoạn đầu tiên chỉ. Chi tiết trong ./example.

Bạn có thể cố gắng cạnh tranh với mô hình này ở đây.

CHẤM CÂUĐỘ CHÍNH XÁCGỢI LẠIF-SCORE
?DẤU CHẤM HỎI77.7 73.2 75.4
.GIAI ĐOẠN50.0 0.1 0.1
, Dấu phẩy68.9 72.0 70.4
?DẤU CHẤM HỎI55.9 8.8 15.2
.GIAI ĐOẠN60.9 23.8 34.2
Tổng thể44.7 1.1 2.2
.GIAI ĐOẠN84.7 84.1 84.4
Tổng thể75.7 73.9 74.8

Tiếng Anh Europarl v7

  • Bộ đào tạo Kích thước: 40m từ. Giai đoạn đầu tiên chỉ. Chi tiết trong ./example.
  • Bạn có thể cố gắng cạnh tranh với mô hình này ở đây.
  • !DẤU CHẤM THAN

-Dash

  • :ĐẠI TRÀNG

    ; Dấu chấm phẩy

  • Yêu cầu

    Ví dụ:

    >>> from deepmultilingualpunctuation import PunctuationModel
    >>> model = PunctuationModel()
    Downloading config.json: 100%|█████████████████████████████████████████████████████████████████| 892/892 [00:00<00:00, 335kB/s]
    Downloading pytorch_model.bin: 100%|██████████████████████████████████████████████████████| 2.08G/2.08G [04:54<00:00, 7.60MB/s]
    Downloading tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 406/406 [00:00<00:00, 216kB/s]
    Downloading sentencepiece.bpe.model: 100%|████████████████████████████████████████████████| 4.83M/4.83M [00:00<00:00, 8.08MB/s]
    Downloading special_tokens_map.json: 100%|█████████████████████████████████████████████████████| 239/239 [00:00<00:00, 158kB/s]
    /opt/anaconda3/envs/punct/lib/python3.9/site-packages/transformers/pipelines/token_classification.py:135: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="none"` instead.
      warnings.warn(
    >>> text = "My name is Clara and I live in Berkeley California Ist das eine Frage Frau Müller"
    >>> result = model.restore_punctuation(text)
    >>> print(result)
    My name is Clara and I live in Berkeley, California. Ist das eine Frage, Frau Müller?
    
    7

    Dữ liệu pha thứ hai cũng có thể không có chú thích tạm dừng để thực hiện thích ứng miền nhắm mục tiêu.

Hãy chắc chắn rằng các từ đầu tiên của câu không viết hoa chữ cái đầu tiên. Điều này sẽ cung cấp cho mô hình gợi ý không công bằng về các địa điểm thời kỳ. Ngoài ra, các tệp văn bản bạn sử dụng để đào tạo và xác thực phải đủ lớn (ít nhất là minibatch_size x squence_length của các từ, là 128x50 = 6400 từ với cài đặt mặc định), nếu không bạn có thể gặp lỗi.

Cấu hình

Kích thước từ vựng, mã thông báo dấu câu và ánh xạ của chúng và vị trí dữ liệu được chuyển đổi có thể được cấu hình trong tiêu đề của dữ liệu.py. Một số siêu phế mô hình có thể được cấu hình trong các tiêu đề của main.py và main2.py. Tỷ lệ học tập và kích thước lớp ẩn có thể được thông qua như là đối số.

Cách sử dụng

Bước đầu tiên là chuyển đổi dữ liệu. Giả sử rằng được xử lý trước và làm sạch *.Train.txt, *.dev.txt và *.test.txt được đặt trong

>>> from deepmultilingualpunctuation import PunctuationModel
>>> model = PunctuationModel()
Downloading config.json: 100%|█████████████████████████████████████████████████████████████████| 892/892 [00:00<00:00, 335kB/s]
Downloading pytorch_model.bin: 100%|██████████████████████████████████████████████████████| 2.08G/2.08G [04:54<00:00, 7.60MB/s]
Downloading tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 406/406 [00:00<00:00, 216kB/s]
Downloading sentencepiece.bpe.model: 100%|████████████████████████████████████████████████| 4.83M/4.83M [00:00<00:00, 8.08MB/s]
Downloading special_tokens_map.json: 100%|█████████████████████████████████████████████████████| 239/239 [00:00<00:00, 158kB/s]
/opt/anaconda3/envs/punct/lib/python3.9/site-packages/transformers/pipelines/token_classification.py:135: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="none"` instead.
  warnings.warn(
>>> text = "My name is Clara and I live in Berkeley California Ist das eine Frage Frau Müller"
>>> result = model.restore_punctuation(text)
>>> print(result)
My name is Clara and I live in Berkeley, California. Ist das eine Frage, Frau Müller?
8, việc chuyển đổi có thể được bắt đầu bằng:

>>> from deepmultilingualpunctuation import PunctuationModel
>>> model = PunctuationModel()
Downloading config.json: 100%|█████████████████████████████████████████████████████████████████| 892/892 [00:00<00:00, 335kB/s]
Downloading pytorch_model.bin: 100%|██████████████████████████████████████████████████████| 2.08G/2.08G [04:54<00:00, 7.60MB/s]
Downloading tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 406/406 [00:00<00:00, 216kB/s]
Downloading sentencepiece.bpe.model: 100%|████████████████████████████████████████████████| 4.83M/4.83M [00:00<00:00, 8.08MB/s]
Downloading special_tokens_map.json: 100%|█████████████████████████████████████████████████████| 239/239 [00:00<00:00, 158kB/s]
/opt/anaconda3/envs/punct/lib/python3.9/site-packages/transformers/pipelines/token_classification.py:135: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="none"` instead.
  warnings.warn(
>>> text = "My name is Clara and I live in Berkeley California Ist das eine Frage Frau Müller"
>>> result = model.restore_punctuation(text)
>>> print(result)
My name is Clara and I live in Berkeley, California. Ist das eine Frage, Frau Müller?
9

Nếu bạn cũng có dữ liệu giai đoạn thứ hai thì:

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator
0

Giai đoạn đầu tiên có thể được đào tạo với:

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator
1

ví dụ

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator
2 hoạt động tốt.

Giai đoạn thứ hai có thể được đào tạo với:

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator
3

Văn bản được xử lý trước có thể được nhấn mạnh bằng ví dụ:

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator
4

Hoặc, nếu các chú thích tạm dừng có mặt trong Data.Dev.TXT và bạn có mô hình giai đoạn thứ hai được đào tạo về dữ liệu chú thích tạm dừng, thì:

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator
5

Mã thông báo dấu câu trong data.dev.txt không cần phải xóa - tập lệnh punctuator.py bỏ qua chúng.

Thống kê lỗi trong ví dụ này có thể được tính toán với:

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator
6

Bạn có thể chơi với một mô hình được đào tạo với (giả sử văn bản đầu vào được xử lý tương tự như dữ liệu đào tạo):

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator
7

Hoặc với:

virtualenv -p python3.7 .env
. .env/bin/activate
pip install punctuator
8

Nếu bạn muốn xem, những từ nào mà mô hình coi là unks (oov).

Sự phát triển

Chạy tất cả các bài kiểm tra với:

export TESTNAME=; tox

Chạy một bài kiểm tra cụ thể trong một môi trường cụ thể với:

export TESTNAME=.test_punctuate; tox -e py37

Làm thế nào để bạn thêm dấu câu vào văn bản trong Python?

3 câu trả lời..
Chạy NLTK POS Tagger trên Chuỗi (một khi chúng ta biết các phần của bài phát biểu, chúng ta có thể viết mã quy tắc).
Xác định các mã thông báo có một phần của lời nói phá vỡ các quy tắc ngữ pháp của một câu (câu không nên kết thúc trong giới từ).
Xác định mã thông báo rằng một câu có thể kết thúc dựa trên các quy tắc ngữ pháp (danh từ là một khởi đầu tuyệt vời).

Làm thế nào để bạn xác định dấu câu trong Python?

Dấu câu là một chuỗi được khởi tạo trước được sử dụng làm hằng số chuỗi.Trong Python, chuỗi.Dấu câu sẽ cung cấp cho tất cả các bộ chấm câu.Tham số: Không lấy bất kỳ tham số nào, vì nó không phải là một hàm.. In Python, string. punctuation will give the all sets of punctuation. Parameters : Doesn't take any parameter, since it's not a function.

Làm thế nào để bạn đếm các dấu chấm câu trong Python?

Python..
Đếm = 0 ;.
str = "Chào buổi sáng! Ông James Potter. ....
Đối với i trong phạm vi (0, len (str)):.
#Checks liệu nhân vật đã cho là dấu chấm câu ..
Nếu str [i] in ('!', ",", "\ '", ";", "\" ",". ","-","? "):.
Đếm = đếm + 1 ;.
in ("Tổng số ký tự dấu chấm câu tồn tại trong chuỗi:") ;.
in (đếm) ;.

Google Docs có dấu câu tự động không?

Nhưng có một dấu chấm câu có một dấu ấn kỳ quái đối với các nhà văn.Và sau nhiều năm chờ đợi, Google Docs hiện tự động biến các chuỗi dấu gạch nối thành các dấu gạch ngang và dấu gạch ngang.Có, Microsoft Word đã triển khai tính năng này trong hơn một thập kỷ trước.