| 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|[\!\.\?]|$)/; |
| 4 | const hashtagRegex = /^#[a-zA-Z0-9]+/; |
| 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, ''); |
| 28 | |
| 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 = [ |
| 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.'}, |
| 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(); |
| 73 | const readline = require('readline'); |
| 74 | global.header = ''; |
| 75 | global.standalone = false |
| 76 | |
| 77 | |
| 78 | const rl = readline.createInterface({ |
| 79 | input: process.stdin, |
| 80 | output: process.stdout, |
| 81 | terminal: false, |
| 82 | |
| 83 | }); |
| 84 | |
| 85 | |
| 86 | function parseConllu(line) { |
| 87 | if (line.match('#\\s*foundry')) { |
| 88 | if (line.match('=\\s*base')) { |
| 89 | if (options.sparse) { |
| 90 | global.standalone = true |
| 91 | } |
| 92 | process.stdout.write("# foundry = cmc\n"); |
| 93 | } else { |
| 94 | process.stdout.write(`${line}\n`); |
| 95 | } |
| 96 | return |
| 97 | } |
| 98 | |
| 99 | if (global.standalone) { |
| 100 | if (line.match('^#\\s*filename')) { |
| 101 | global.fileheader = `${line}\n`; |
| 102 | return; |
| 103 | } else if (line.match('^#\\s*text_id')){ |
| 104 | global.fileheader += `${line}\n`; |
| 105 | return; |
| Marc Kupietz | fd92b1d | 2024-03-13 10:51:29 +0100 | [diff] [blame] | 106 | } else if (line.match('^#\\s*eo[ft]')){ |
| 107 | process.stdout.write(`${line}\n`); |
| 108 | return; |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 109 | } else if (line.match('^#')){ |
| 110 | global.header += `${line}\n`; |
| 111 | return; |
| 112 | } else if (line.trim().match('^$')) { |
| 113 | if(global.header == "") { |
| 114 | process.stdout.write("\n"); |
| 115 | } |
| 116 | global.header = ''; |
| 117 | return |
| 118 | } |
| 119 | } else { |
| 120 | if (! line.match('^\\d+')) { |
| 121 | process.stdout.write(`${line}\n`); |
| 122 | return; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | const columns = line.trim().split('\t'); |
| 127 | |
| 128 | const word = columns[1]; |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 129 | // Guard clause: if word is undefined, just output the line as-is |
| 130 | if (!word) { |
| 131 | process.stdout.write(`${line}\n`); |
| 132 | return; |
| 133 | } |
| 134 | |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 135 | var new_tag = null; |
| Marc Kupietz | 7497fc4 | 2025-12-11 15:47:34 +0100 | [diff] [blame] | 136 | if (word.match(wikiEmojiRegex)) { |
| 137 | new_tag = 'EMOWIKI'; |
| 138 | } else if (word.match(emojiRegex)) { |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 139 | new_tag = 'EMOIMG'; |
| 140 | } else if(word.match(actionWordRegex)) { |
| 141 | new_tag = 'AKW'; |
| 142 | } else if(word.match(emoticonRegex)) { |
| 143 | new_tag = 'EMOASC'; |
| 144 | } else if(word.match(hashtagRegex)) { |
| 145 | } else if(word.match(urlRegex)) { |
| 146 | new_tag = 'URL'; |
| 147 | } else if(word.match(emailRegex)) { |
| 148 | new_tag = 'EML'; |
| 149 | } else if(! columns[3].match("^(NE|PROPN)") && word.match(addressRegex)) { |
| 150 | new_tag = 'ADR'; |
| 151 | } |
| 152 | if (new_tag) { |
| Marc Kupietz | 76007d6 | 2025-12-11 17:13:05 +0100 | [diff] [blame] | 153 | columns[4] = new_tag; |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 154 | columns[5] = '_'; |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 155 | // For EMOIMG tokens, set lemma to the base emoji (without modifiers) |
| 156 | if (new_tag === 'EMOIMG') { |
| Marc Kupietz | 30634ff | 2025-12-18 11:39:03 +0100 | [diff] [blame^] | 157 | const base = getBaseEmoji(word); |
| 158 | columns[2] = base; |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 159 | columns[3] = 'EMOIMG'; |
| Marc Kupietz | 30634ff | 2025-12-18 11:39:03 +0100 | [diff] [blame^] | 160 | |
| 161 | // Look up emoji metadata |
| 162 | // Try exact match first, then base emoji |
| 163 | const data = emojiData[word] || emojiData[base]; |
| 164 | if (data) { |
| 165 | // g=group|s=subgroup|q=qualified|v=version|n=name |
| 166 | columns[5] = `g=${data.g}|s=${data.s}|q=${data.q}|v=${data.v}|n=${data.n}`; |
| 167 | } |
| Marc Kupietz | a7934e0 | 2025-12-18 07:25:53 +0100 | [diff] [blame] | 168 | } |
| Marc Kupietz | b43a518 | 2024-02-03 18:09:10 +0100 | [diff] [blame] | 169 | if (global.standalone) { |
| 170 | process.stdout.write(fileheader); |
| 171 | process.stdout.write(header); |
| 172 | new_tag = null; |
| 173 | header = fileheader = ''; |
| 174 | } |
| 175 | process.stdout.write(columns.join('\t') + '\n'); |
| 176 | } else if (! global.standalone) { |
| 177 | process.stdout.write(`${line}\n`); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | rl.on('line', parseConllu); |
| 182 | rl.on('close', () => process.exit(0)); // important to exit, otherwise the process will hang |