Skip to content

Commit

Permalink
utilz
Browse files Browse the repository at this point in the history
  • Loading branch information
bendemonium committed Jul 10, 2024
1 parent f7346e9 commit 99bb50f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/core/.ipynb_checkpoints/utils-checkpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import json
import re
from typing import Dict, List, Any

def find_phoneme_context(text: str, phoneme: str, context_size: int = 1) -> List[str]:
"""Find all occurrences of a phoneme in the text and return its context."""
pattern = f"(?=(.{{{context_size}}}{ipa_to_regex(phoneme)}.{{{context_size}}}))"
return re.findall(pattern, text)

def get_language_name(glottocode: str, language_data: Dict[str, Dict[str, str]]) -> str:
"""Get the language name for a given Glottocode."""
return language_data.get(glottocode, {}).get('LanguageName', 'Unknown')

class PhonemeTokenizer:
def __init__(self, phoneme_inventory: List[str]):
self.phoneme_inventory = sorted(phoneme_inventory, key=len, reverse=True)
self.phoneme_pattern = re.compile('|'.join(map(ipa_to_regex, self.phoneme_inventory)))

def tokenize(self, text: str) -> List[str]:
"""Tokenize text into phonemes."""
return self.phoneme_pattern.findall(text)
21 changes: 21 additions & 0 deletions src/core/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import json
import re
from typing import Dict, List, Any

def find_phoneme_context(text: str, phoneme: str, context_size: int = 1) -> List[str]:
"""Find all occurrences of a phoneme in the text and return its context."""
pattern = f"(?=(.{{{context_size}}}{ipa_to_regex(phoneme)}.{{{context_size}}}))"
return re.findall(pattern, text)

def get_language_name(glottocode: str, language_data: Dict[str, Dict[str, str]]) -> str:
"""Get the language name for a given Glottocode."""
return language_data.get(glottocode, {}).get('LanguageName', 'Unknown')

class PhonemeTokenizer:
def __init__(self, phoneme_inventory: List[str]):
self.phoneme_inventory = sorted(phoneme_inventory, key=len, reverse=True)
self.phoneme_pattern = re.compile('|'.join(map(ipa_to_regex, self.phoneme_inventory)))

def tokenize(self, text: str) -> List[str]:
"""Tokenize text into phonemes."""
return self.phoneme_pattern.findall(text)

0 comments on commit 99bb50f

Please sign in to comment.