sepideh hosseinkhani
Làm theo
29 Tháng Năm, 2021
·
2 phút đọc
Loại bỏ tất cả các loại biểu tượng cảm xúc bằng gói “demoji” Python
chào các cậu. Tôi là Sepideh. đây là câu chuyện đầu tiên của tôi trên Medium. ]
đây là một mã đơn giản cho những ai muốn xóa biểu tượng cảm xúc khỏi văn bản
Đầu tiên. cài đặt demoji với
pip cài đặt demoji
Thứ hai. nhập gói này vào bài kiểm tra của bạn. tập tin py
Những thay đổi chính trong phiên bản 1. x
Phiên bản 1. x của demoji
hiện gói dữ liệu Unicode trong gói khi cài đặt thay vì yêu cầu tải xuống mã từ unicode. org trong thời gian chạy. Vui lòng xem THAY ĐỔI. md để biết chi tiết và làm quen với các thay đổi trước khi cập nhật từ 0. x đến 1. x
Để báo cáo bất kỳ hồi quy nào, vui lòng mở vấn đề GitHub
Sử dụng cơ bản
demoji
xuất một số chức năng liên quan đến văn bản cho chức năng tìm và thay thế bằng biểu tượng cảm xúc
>>> tweet = """\ .. #startspreadingthenews yankees win great start by 🎅🏾 going 5strong innings with 5k’s🔥 🐂 .. solo homerun 🌋🌋 with 2 solo homeruns and👹 3run homerun… 🤡 🚣🏼 👨🏽⚖️ with rbi’s … 🔥🔥 .. 🇲🇽 and 🇳🇮 to close the game🔥🔥!!!…. .. WHAT A GAME!!.. .. """ >>> demoji.findall[tweet] { "🔥": "fire", "🌋": "volcano", "👨🏽\u200d⚖️": "man judge: medium skin tone", "🎅🏾": "Santa Claus: medium-dark skin tone", "🇲🇽": "flag: Mexico", "👹": "ogre", "🤡": "clown face", "🇳🇮": "flag: Nicaragua", "🚣🏼": "person rowing boat: medium-light skin tone", "🐂": "ox", }
Xem API chức năng
Sử dụng dòng lệnh
Bạn có thể sử dụng demoji
hoặc
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:1 để thay thế biểu tượng cảm xúc trong [các] tệp hoặc stdin bằng
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:2 tương đương của chúng
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:
Thẩm quyền giải quyết
findall[string: str] -> Dict[str, str]
Tìm biểu tượng cảm xúc trong
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:3. Trả về ánh xạ của
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:4________số 8_______
Tìm biểu tượng cảm xúc trong
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:3. Trả về một danh sách [có thể trùng lặp]
Nếu
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:6 là True, danh sách chứa mã mô tả. Nếu
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:6 là Sai, danh sách chứa các biểu tượng cảm xúc
replace[string: str, repl: str = ""] -> str
Thay thế biểu tượng cảm xúc trong
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:3 bằng
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:9
replace_with_desc[string: str, sep: str = ":"] -> str
Thay thế biểu tượng cảm xúc trong
$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:3 bằng mã mô tả của chúng. Các mã được bao quanh bởi
findall[string: str] -> Dict[str, str]1
last_downloaded_timestamp[] -> datetime.datetime
Hiển thị dấu thời gian của lần tải xuống cuối cùng cho dữ liệu biểu tượng cảm xúc đi kèm với gói
chú thích cuối trang. chuỗi biểu tượng cảm xúc
Nhiều biểu tượng cảm xúc trông giống như các ký tự Unicode đơn lẻ thực sự là các chuỗi nhiều ký tự. ví dụ
- Keycap 2️⃣ thực tế là 3 ký tự, U+0032 [chữ số ASCII 2], U+FE0F [bộ chọn biến thể] và U+20E3 [kết hợp keycap kèm theo]
- Quốc kỳ Scotland 7 ký tự thành phần,
findall[string: str] -> Dict[str, str]
2 trong ký hiệu bỏ qua đầy đủ
[Bạn có thể thấy bất kỳ thứ nào trong số này thông qua
findall[string: str] -> Dict[str, str]3. ]
demoji
cẩn thận xử lý việc này và nên tìm các chuỗi đầy đủ thay vì các thành phần phụ không đầy đủ của chúng
Cách nó thực hiện điều này là sắp xếp các mã biểu tượng cảm xúc theo độ dài của chúng, sau đó biên dịch một biểu thức chính quy được nối sẽ tham lam tìm kiếm các biểu tượng cảm xúc dài hơn trước, sau đó quay lại các biểu tượng cảm xúc ngắn hơn nếu không tìm thấy. Đây hoàn toàn không phải là một cách tìm kiếm siêu tối ưu vì nó có thuộc tính O[N2], mà trọng tâm là tính chính xác và đầy đủ
>>> from pprint import pprint >>> seq = """\ .. I bet you didn't know that 🙋, 🙋♂️, and 🙋♀️ are three different emojis. .. """ >>> pprint[seq.encode['unicode-escape']] # Python 3 [b"I bet you didn't know that \\U0001f64b, \\U0001f64b\\u200d\\u2642\\ufe0f," b' and \\U0001f64b\\u200d\\u2640\\ufe0f are three different emojis.\\n']
1. 1. 0
- Thêm một
findall[string: str] -> Dict[str, str]
5 để cho phép chạy$ cat out.txt All done! ✨ 🍰 ✨ $ demoji out.txt All done! :sparkles: :shortcake: :sparkles: $ echo 'All done! ✨ 🍰 ✨' | demoji All done! :sparkles: :shortcake: :sparkles: $ demoji - we didnt start the 🔥 we didnt start the :fire:
1; . Đóng góp của @jap
1. 0. 0
Đây là bản phát hành không tương thích ngược với một số thay đổi đáng kể
Thay đổi lớn nhất là demoji
giờ đây gói một bản sao tĩnh của dữ liệu biểu tượng cảm xúc Unicode với gói khi cài đặt, thay vì yêu cầu tải xuống mã trong thời gian chạy từ unicode. tổ chức