| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 1 | #!/usr/bin/env node |
| 2 | |
| 3 | const emoticonRegex = /^(\:\w+\:|\<[\/\\]?3|[\(\)\\\D|\*\$][\-\^]?[\:\;\=]|[\:\;\=B8][\-\^]?[3DOPp\@\$\*\\\)\(\/\|])(?=\s|[\!\.\?]|$)/; |
| Marc Kupietz | 3d52509 | 2026-04-11 20:44:32 +0200 | [diff] [blame] | 4 | const hashtagRegex = /^#(?=.*\p{L})[\p{L}\p{M}\p{N}]+$/u; |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 5 | const urlRegex = /^(ftp|http)s?:\/\/[^\s]+/; |
| 6 | const emailRegex = /^\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/; |
| 7 | const addressRegex = /^@[a-zA-Z0-9]+/; |
| Marc Kupietz | 73f1749 | 2024-02-26 09:44:53 +0100 | [diff] [blame] | 8 | const actionWordRegex = /^:[^:]+:$/; |
| Marc Kupietz | 7497fc4 | 2025-12-11 15:47:34 +0100 | [diff] [blame] | 9 | const wikiEmojiRegex = /^\[_EMOJI:[^\]]+\]$/; |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 10 | |
| Marc Kupietz | 30634ff | 2025-12-18 11:39:03 +0100 | [diff] [blame] | 11 | // Load emoji data |
| 12 | let emojiData = {}; |
| 13 | try { |
| 14 | emojiData = require('./emoji_data.json'); |
| 15 | } catch (e) { |
| 16 | // Silent fallback if file doesn't exist (e.g. during initial setup before script run) |
| 17 | } |
| 18 | |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 19 | // Function to strip emoji modifiers and zero-width joiners to get base emoji |
| 20 | function getBaseEmoji(emoji) { |
| 21 | const stripped = emoji |
| 22 | // Remove skin tone modifiers (U+1F3FB-U+1F3FF) |
| 23 | .replace(/[\u{1F3FB}-\u{1F3FF}]/gu, '') |
| 24 | // Remove zero-width joiners (U+200D) |
| 25 | .replace(/\u200D/g, '') |
| 26 | // Remove variation selectors (U+FE0F, U+FE0E) |
| 27 | .replace(/[\uFE0E\uFE0F]/g, ''); |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 28 | |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 29 | // Extract the first emoji character using Array spread to handle multi-byte emoji |
| 30 | const chars = [...stripped]; |
| 31 | return chars.length > 0 ? chars[0] : stripped; |
| 32 | } |
| 33 | |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 34 | const optionDefinitions = [ |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 35 | { name: 'sparse', alias: 's', type: Boolean, description: 'Print only the files, lines that have POS annotations.' }, |
| 36 | { name: 'help', alias: 'h', type: Boolean, description: 'Print this usage guide.' }, |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 37 | ] |
| 38 | |
| 39 | const sections = [ |
| 40 | { |
| Marc Kupietz | 03ba301 | 2025-12-11 16:14:05 +0100 | [diff] [blame] | 41 | header: 'conllu-cmc', |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 42 | content: 'Reads CoNLL-U format from stdin and annotates emojis, emoticons, hashtags, URLs, email addresses, @addresses, and action words. Writes CoNLL-U format to stdout.' |
| 43 | }, |
| 44 | { |
| 45 | header: 'Synopsis', |
| Marc Kupietz | 03ba301 | 2025-12-11 16:14:05 +0100 | [diff] [blame] | 46 | content: '$ conllu-cmc [-s] < input.conllu > output.conllu' |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 47 | }, |
| 48 | { |
| 49 | header: 'Options', |
| 50 | optionList: optionDefinitions |
| 51 | } |
| 52 | ] |
| 53 | |
| 54 | const getUsage = require('command-line-usage') |
| 55 | const commandLineArgs = require('command-line-args') |
| 56 | |
| 57 | var options; |
| 58 | try { |
| 59 | options = commandLineArgs(optionDefinitions) |
| 60 | } catch (e) { |
| 61 | console.error(e.message); |
| 62 | options = { help: true }; |
| 63 | } |
| 64 | |
| 65 | if (options.help) { |
| 66 | const usage = getUsage(sections); |
| 67 | console.log(usage); |
| Marc Kupietz | b75d926 | 2025-12-11 16:31:57 +0100 | [diff] [blame] | 68 | process.exit(0); |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | const EmojiRegex = require('emoji-regex'); |
| 72 | const emojiRegex = EmojiRegex(); |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 73 | const { once } = require('events'); |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 74 | const readline = require('readline'); |
| 75 | global.header = ''; |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 76 | global.fileheader = ''; |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 77 | global.standalone = false |
| 78 | |
| 79 | |
| 80 | const rl = readline.createInterface({ |
| 81 | input: process.stdin, |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 82 | terminal: false, |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 83 | |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 84 | }); |
| 85 | |
| 86 | |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 87 | async function writeOutput(text) { |
| 88 | if (!process.stdout.write(text)) { |
| 89 | await once(process.stdout, 'drain'); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | async function parseConllu(line) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 95 | if (line.match('#\\s*foundry')) { |
| 96 | if (line.match('=\\s*base')) { |
| 97 | if (options.sparse) { |
| 98 | global.standalone = true |
| 99 | } |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 100 | await writeOutput("# foundry = cmc\n"); |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 101 | } else { |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 102 | await writeOutput(`${line}\n`); |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 103 | } |
| 104 | return |
| 105 | } |
| 106 | |
| 107 | if (global.standalone) { |
| 108 | if (line.match('^#\\s*filename')) { |
| 109 | global.fileheader = `${line}\n`; |
| 110 | return; |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 111 | } else if (line.match('^#\\s*text_id')) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 112 | global.fileheader += `${line}\n`; |
| 113 | return; |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 114 | } else if (line.match('^#\\s*eo[ft]')) { |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 115 | await writeOutput(`${line}\n`); |
| Marc Kupietz | fd92b1d | 2024-03-13 10:51:29 +0100 | [diff] [blame] | 116 | return; |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 117 | } else if (line.match('^#')) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 118 | global.header += `${line}\n`; |
| 119 | return; |
| 120 | } else if (line.trim().match('^$')) { |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 121 | if (global.header == "") { |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 122 | await writeOutput("\n"); |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 123 | } |
| 124 | global.header = ''; |
| 125 | return |
| 126 | } |
| 127 | } else { |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 128 | if (!line.match('^\\d+')) { |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 129 | await writeOutput(`${line}\n`); |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | const columns = line.trim().split('\t'); |
| 135 | |
| 136 | const word = columns[1]; |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 137 | // Guard clause: if word is undefined, just output the line as-is |
| 138 | if (!word) { |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 139 | await writeOutput(`${line}\n`); |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 140 | return; |
| 141 | } |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 142 | |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 143 | var new_tag = null; |
| Marc Kupietz | 7497fc4 | 2025-12-11 15:47:34 +0100 | [diff] [blame] | 144 | if (word.match(wikiEmojiRegex)) { |
| 145 | new_tag = 'EMOWIKI'; |
| 146 | } else if (word.match(emojiRegex)) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 147 | new_tag = 'EMOIMG'; |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 148 | } else if (word.match(actionWordRegex)) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 149 | new_tag = 'AKW'; |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 150 | } else if (word.match(emoticonRegex)) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 151 | new_tag = 'EMOASC'; |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 152 | } else if (word.match(hashtagRegex)) { |
| 153 | new_tag = 'HST'; |
| 154 | } else if (word.match(urlRegex)) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 155 | new_tag = 'URL'; |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 156 | } else if (word.match(emailRegex)) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 157 | new_tag = 'EML'; |
| Marc Kupietz | 804750d | 2026-04-10 14:44:13 +0200 | [diff] [blame] | 158 | } else if (word.match(addressRegex)) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 159 | new_tag = 'ADR'; |
| 160 | } |
| 161 | if (new_tag) { |
| Marc Kupietz | 76007d6 | 2025-12-11 17:13:05 +0100 | [diff] [blame] | 162 | columns[4] = new_tag; |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 163 | columns[5] = '_'; |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 164 | // For EMOIMG tokens, set lemma to the base emoji (without modifiers) |
| 165 | if (new_tag === 'EMOIMG') { |
| Marc Kupietz | 30634ff | 2025-12-18 11:39:03 +0100 | [diff] [blame] | 166 | const base = getBaseEmoji(word); |
| 167 | columns[2] = base; |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 168 | |
| Marc Kupietz | 30634ff | 2025-12-18 11:39:03 +0100 | [diff] [blame] | 169 | // Look up emoji metadata |
| 170 | // Try exact match first, then base emoji |
| 171 | const data = emojiData[word] || emojiData[base]; |
| 172 | if (data) { |
| 173 | // g=group|s=subgroup|q=qualified|v=version|n=name |
| 174 | columns[5] = `g=${data.g}|s=${data.s}|q=${data.q}|v=${data.v}|n=${data.n}`; |
| 175 | } |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 176 | } |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 177 | if (global.standalone) { |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 178 | await writeOutput(fileheader); |
| 179 | await writeOutput(header); |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 180 | new_tag = null; |
| 181 | header = fileheader = ''; |
| 182 | } |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 183 | await writeOutput(columns.join('\t') + '\n'); |
| Marc Kupietz | a17c2e5 | 2026-04-10 14:21:27 +0200 | [diff] [blame] | 184 | } else if (!global.standalone) { |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 185 | await writeOutput(`${line}\n`); |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| Marc Kupietz | bbc9b22 | 2026-04-10 15:43:01 +0200 | [diff] [blame] | 189 | async function main() { |
| 190 | for await (const line of rl) { |
| 191 | await parseConllu(line); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | main().catch((error) => { |
| 196 | console.error(error); |
| 197 | process.exit(1); |
| 198 | }); |