Views
No views yet
CrabInHoney/morphbert-tiny-v2-morpheme-segmentation-ru model, a compact transformer-based system for morpheme segmentation and classification of Russian words. The model classifies each character of a given word into one of several morpheme categories: {0: 'END', 1: 'HYPH', 2: 'LINK', 3: 'POSTFIX', 4: 'PREF', 5: 'ROOT', 6: 'SUFF'}.morphbert-tiny-v2-morpheme-segmentation-ru leverages a lightweight BERT-like architecture, enabling efficient deployment and inference while maintaining high performance on the specific task of morphological analysis at the character level. The model was distilled from a larger teacher model.transformers library. Below is a minimal example using the custom multi-task head as in this repository:1import torch
2import torch.nn as nn
3from transformers import BertTokenizer, BertPreTrainedModel, BertModel
4
5MODEL_DIR = 'CrabInHoney/morphbert-tiny-v2-morpheme-segmentation-ru'
6MAX_LEN = 32
7DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8ID2TAG = {0: 'END', 1: 'HYPH', 2: 'LINK', 3: 'POSTFIX', 4: 'PREF', 5: 'ROOT', 6: 'SUFF'}
9NUM_MORPH_CLASSES = len(ID2TAG)
10
11class BertForMultiTask(BertPreTrainedModel):
12 def __init__(self, config, num_seg_labels=2, num_morph_labels=NUM_MORPH_CLASSES):
13 super().__init__(config)
14 self.bert = BertModel(config)
15 self.dropout = nn.Dropout(config.hidden_dropout_prob)
16 self.seg_head = nn.Linear(config.hidden_size, num_seg_labels)
17 self.cls_head = nn.Linear(config.hidden_size, num_morph_labels)
18 def forward(self, input_ids, attention_mask=None):
19 x = self.dropout(self.bert(input_ids, attention_mask=attention_mask).last_hidden_state)
20 return {"seg_logits": self.seg_head(x), "cls_logits": self.cls_head(x)}
21
22tokenizer = BertTokenizer.from_pretrained(MODEL_DIR)
23model = BertForMultiTask.from_pretrained(MODEL_DIR, num_morph_labels=NUM_MORPH_CLASSES).to(DEVICE).eval()
24
25def analyze_word_compact(word):
26 if not word.strip(): return "Empty word"
27 chars = list(word.lower())
28 enc = tokenizer(" ".join(chars), return_tensors='pt', max_length=MAX_LEN, padding='max_length', truncation=True, add_special_tokens=True)
29 with torch.no_grad():
30 out = model(input_ids=enc['input_ids'].to(DEVICE), attention_mask=enc['attention_mask'].to(DEVICE))
31 n = min(len(chars), MAX_LEN-2)
32 if n <= 0: return "Word too short/truncated"
33 seg = torch.argmax(out['seg_logits'][0,1:1+n], -1).tolist()
34 cls = torch.argmax(out['cls_logits'][0,1:1+n], -1).tolist()
35 print(f"\n--- '{word}' (processed {n} chars) ---")
36 print("Segmentation:", ' '.join([f'{chars[i]}:{seg[i]}' for i in range(n)]))
37 print("Classification:", ' '.join([f'{chars[i]}:{ID2TAG.get(cls[i], f'ID:{cls[i]}')}' for i in range(n)]))
38 morphemes, morph, tag = [], "", -1
39 for i in range(n):
40 if seg[i]==0:
41 if morph: morphemes.append(f"{morph}:{ID2TAG.get(tag, f'ID:{tag}')}")
42 morph = chars[i]
43 tag = cls[i]
44 else:
45 morph += chars[i]
46 if morph: morphemes.append(f"{morph}:{ID2TAG.get(tag, f'ID:{tag}')}")
47 res = " / ".join(morphemes)
48 print(f"Result: {res}\n{'='*30}")
49 return res
50
51example_words = ["масляный", "предчувствий", "тарковский", "кот", "подгон"]
52for w in example_words: analyze_word_compact(w)--- 'масляный' (processed 8 chars) ---
Segmentation: м:0 а:1 с:1 л:1 я:0 н:1 ы:0 й:1
Classification: м:ROOT а:ROOT с:ROOT л:ROOT я:SUFF н:SUFF ы:END й:END
Result: масл:ROOT / ян:SUFF / ый:END
==============================
--- 'предчувствий' (processed 12 chars) ---
Segmentation: п:0 р:1 е:1 д:1 ч:0 у:1 в:0 с:0 т:1 в:1 и:0 й:1
Classification: п:PREF р:PREF е:PREF д:PREF ч:ROOT у:ROOT в:SUFF с:SUFF т:SUFF в:SUFF и:END й:END
Result: пред:PREF / чу:ROOT / в:SUFF / ств:SUFF / ий:END
==============================
--- 'тарковский' (processed 10 chars) ---
Segmentation: т:0 а:1 р:1 к:1 о:0 в:1 с:0 к:1 и:0 й:1
Classification: т:ROOT а:ROOT р:ROOT к:ROOT о:SUFF в:ROOT с:SUFF к:SUFF и:END й:END
Result: тарк:ROOT / ов:SUFF / ск:SUFF / ий:END
==============================
--- 'кот' (processed 3 chars) ---
Segmentation: к:0 о:1 т:1
Classification: к:ROOT о:ROOT т:ROOT
Result: кот:ROOT
==============================
--- 'подгон' (processed 6 chars) ---
Segmentation: п:0 о:1 д:1 г:0 о:1 н:1
Classification: п:PREF о:PREF д:PREF г:ROOT о:ROOT н:ROOT
Result: под:PREF / гон:ROOT
==============================