Gần đây tôi đã lọc một dòng thời gian Twitter rộng lớn để phân tích nó bằng mạng thần kinh sâu. Như bạn đã biết, các tweet có thể chứa các loại nội dung khác nhau, bao gồm cả biểu tượng cảm xúc. Vì vậy, một trong những bước đầu tiên là làm sạch dữ liệu, trong trường hợp này là xóa tất cả các biểu tượng cảm xúc khỏi dòng thời gian
Mặc dù điều này có thể được thực hiện theo nhiều cách, nhưng tôi sẽ chỉ cách thực hiện với JavaScript vì nó đơn giản và nhanh chóng, vì vậy hãy bắt đầu
Như bạn có thể đoán từ phụ đề của bài đăng này, chúng tôi sẽ sử dụng các biểu thức chính quy để làm điều đó
Các trình duyệt hiện đại hỗ trợ thuộc tính Unicode, cho phép bạn khớp các biểu tượng cảm xúc dựa trên thuộc tính của chúng trong danh mục Emoji Unicode. Ví dụ: bạn có thể sử dụng các ký tự thoát thuộc tính Unicode như
const withEmojis = /\p{Extended_Pictographic}/u5 hoặc
withEmojis.test['😀😀'];
//truewithEmojis.test['ab'];
//falsewithEmojis.test['1'];
//false
const withEmojis = /\p{Extended_Pictographic}/u6 để khớp/không khớp các ký tự biểu tượng cảm xúc. Lưu ý rằng 0123456789#* và các ký tự khác được hiểu là biểu tượng cảm xúc khi sử dụng danh mục Unicode trước đó. Do đó, cách tốt hơn để thực hiện việc này là sử dụng danh mục Unicode
withEmojis.test['😀😀'];
//truewithEmojis.test['ab'];
//falsewithEmojis.test['1'];
//false
const withEmojis = /\p{Extended_Pictographic}/u7 biểu thị tất cả các ký tự thường được hiểu là biểu tượng cảm xúc thay vì danh mục
withEmojis.test['😀😀'];
//truewithEmojis.test['ab'];
//falsewithEmojis.test['1'];
//false
const withEmojis = /\p{Extended_Pictographic}/u8
withEmojis.test['😀😀'];
//truewithEmojis.test['ab'];
//falsewithEmojis.test['1'];
//false
Hãy xem một số ví dụ
Sử dụng \p{} để khớp với các ký tự Unicode
Nếu bạn sử dụng danh mục Unicode “Emoji”, bạn có thể nhận được kết quả không chính xác
const withEmojis = /\p{Emoji}/u
withEmojis.test['😀'];
//truewithEmojis.test['ab'];
//falsewithEmojis.test['1'];
//true opps!
Hỗ trợ dự án này
i Ghét Regex
bởi geon
emoji-regex cung cấp biểu thức chính quy để khớp với tất cả các biểu tượng và chuỗi biểu tượng cảm xúc [bao gồm cả biểu diễn văn bản của biểu tượng cảm xúc] theo Tiêu chuẩn Unicode. Nó dựa trên emoji-test-regex-pattern, tạo ra [tại thời điểm xây dựng] mẫu biểu thức chính quy dựa trên Tiêu chuẩn Unicode. Do đó, biểu tượng cảm xúc-regex có thể dễ dàng được cập nhật bất cứ khi nào biểu tượng cảm xúc mới được thêm vào Unicode
Cài đặt
Qua npm
npm install emoji-regex
trong nút. js
const emojiRegex = require['emoji-regex']; // Note: because the regular expression has the global flag set, this module // exports a function that returns the regex rather than exporting the regular // expression itself, to make it impossible to [accidentally] mutate the // original regular expression. const text = ` \u{231A}: ⌚ default emoji presentation character [Emoji_Presentation] \u{2194}\u{FE0F}: ↔️ default text presentation character rendered as emoji \u{1F469}: 👩 emoji modifier base [Emoji_Modifier_Base] \u{1F469}\u{1F3FF}: 👩🏿 emoji modifier base followed by a modifier `; const regex = emojiRegex[]; for [const match of text.matchAll[regex]] { const emoji = match[0]; console.log[`Matched sequence ${ emoji } — code points: ${ [...emoji].length }`]; }
Đầu ra bảng điều khiển
Matched sequence ⌚ — code points: 1
Matched sequence ⌚ — code points: 1
Matched sequence ↔️ — code points: 2
Matched sequence ↔️ — code points: 2
Matched sequence 👩 — code points: 1
Matched sequence 👩 — code points: 1
Matched sequence 👩🏿 — code points: 2
Matched sequence 👩🏿 — code points: 2
Dành cho người bảo trì
Cách cập nhật biểu tượng cảm xúc-regex sau khi phát hành Tiêu chuẩn Unicode mới
Cập nhật emoji-test-regex-pattern như được mô tả trong kho lưu trữ của nó
Chuyển sự phụ thuộc của emoji-test-regex-pattern sang phiên bản mới nhất
Cập nhật phần phụ thuộc dữ liệu Unicode trong
const noEmojis = /\P{Extended_Pictographic}/u
0 bằng cách chạy các lệnh sau
noEmojis.test['😀'];
//falsenoEmojis.test['1212'];
//false# Example: updating from Unicode v13 to Unicode v14. npm uninstall @unicode/unicode-13.0.0 npm install @unicode/unicode-14.0.0 --save-dev
Tạo đầu ra mới
________số 8
Xác minh rằng các bài kiểm tra vẫn vượt qua
npm test
Cách xuất bản một bản phát hành mới
Trên nhánh
const noEmojis = /\P{Extended_Pictographic}/u
1, gõ số phiên bản biểu tượng cảm xúc-regex trong
noEmojis.test['😀'];
//falsenoEmojis.test['1212'];
//falseconst noEmojis = /\P{Extended_Pictographic}/u
0
noEmojis.test['😀'];
//falsenoEmojis.test['1212'];
//falseconst withEmojis = /\p{Extended_Pictographic}/u
0
withEmojis.test['😀😀'];
//truewithEmojis.test['ab'];
//falsewithEmojis.test['1'];
//falseThay vì
const emojiRegex = require['emoji-regex']; // Note: because the regular expression has the global flag set, this module // exports a function that returns the regex rather than exporting the regular // expression itself, to make it impossible to [accidentally] mutate the // original regular expression. const text = ` \u{231A}: ⌚ default emoji presentation character [Emoji_Presentation] \u{2194}\u{FE0F}: ↔️ default text presentation character rendered as emoji \u{1F469}: 👩 emoji modifier base [Emoji_Modifier_Base] \u{1F469}\u{1F3FF}: 👩🏿 emoji modifier base followed by a modifier `; const regex = emojiRegex[]; for [const match of text.matchAll[regex]] { const emoji = match[0]; console.log[`Matched sequence ${ emoji } — code points: ${ [...emoji].length }`]; }
1, hãy sử dụngconst emojiRegex = require['emoji-regex']; // Note: because the regular expression has the global flag set, this module // exports a function that returns the regex rather than exporting the regular // expression itself, to make it impossible to [accidentally] mutate the // original regular expression. const text = ` \u{231A}: ⌚ default emoji presentation character [Emoji_Presentation] \u{2194}\u{FE0F}: ↔️ default text presentation character rendered as emoji \u{1F469}: 👩 emoji modifier base [Emoji_Modifier_Base] \u{1F469}\u{1F3FF}: 👩🏿 emoji modifier base followed by a modifier `; const regex = emojiRegex[]; for [const match of text.matchAll[regex]] { const emoji = match[0]; console.log[`Matched sequence ${ emoji } — code points: ${ [...emoji].length }`]; }
2 hoặcconst emojiRegex = require['emoji-regex']; // Note: because the regular expression has the global flag set, this module // exports a function that returns the regex rather than exporting the regular // expression itself, to make it impossible to [accidentally] mutate the // original regular expression. const text = ` \u{231A}: ⌚ default emoji presentation character [Emoji_Presentation] \u{2194}\u{FE0F}: ↔️ default text presentation character rendered as emoji \u{1F469}: 👩 emoji modifier base [Emoji_Modifier_Base] \u{1F469}\u{1F3FF}: 👩🏿 emoji modifier base followed by a modifier `; const regex = emojiRegex[]; for [const match of text.matchAll[regex]] { const emoji = match[0]; console.log[`Matched sequence ${ emoji } — code points: ${ [...emoji].length }`]; }
3 nếu cầnLưu ý rằng điều này tạo ra một cam kết Git + thẻ
Đẩy cam kết phát hành và gắn thẻ
const withEmojis = /\p{Extended_Pictographic}/u
4
withEmojis.test['😀😀'];
//truewithEmojis.test['ab'];
//falsewithEmojis.test['1'];
//falseCI của chúng tôi sau đó sẽ tự động xuất bản bản phát hành mới lên npm
Tác giả
Mathias BynenGiấy phép
biểu tượng cảm xúc-regex có sẵn theo giấy phép MIT