Skip to content

Commit

Permalink
fix tibetan ref (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsam authored Jan 30, 2025
2 parents 1b5452c + c465656 commit 1950be1
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 20 deletions.
54 changes: 52 additions & 2 deletions reader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2805,13 +2805,63 @@ def _internal_do_post(request, uid):

return jsonResponse({"error": "Unsupported HTTP method."})

def convert_tibetan_text_segment_number(input_string):

english_numerals = None
# Extract the Tibetan text (non-numeral part)
tibetan_text = re.sub(r'[༠-༩]+[:.,]?[༠-༩]*', '', input_string).strip()

# Extract the Tibetan numerals (both integer and decimal parts)
tibetan_numerals_match = re.findall(r'[༠-༩]+[:.,]?[༠-༩]*', input_string)

if tibetan_numerals_match:
tibetan_numerals = tibetan_numerals_match[0]

def get_name_completions(name, limit, ref_only, topic_override=False):
lang = "he" if has_tibetan(name) else "en"
# Mapping Tibetan numerals to English numerals
tibetan_to_english = {
'༠': '0', '༡': '1', '༢': '2', '༣': '3', '༤': '4',
'༥': '5', '༦': '6', '༧': '7', '༨': '8', '༩': '9'
}

# Convert Tibetan numerals to English numerals
english_numerals = ''.join([tibetan_to_english.get(char, char) for char in tibetan_numerals])

# Replace Tibetan separators with English decimal point
english_numerals = english_numerals.replace(':', '.').replace(',', '.')

# Split into integer and decimal parts (if any)
if '.' in english_numerals:
integer_part, decimal_part = english_numerals.split('.')
# Remove leading zeros in the integer part
integer_part = integer_part.lstrip('0') or '0'
# Remove leading zeros in the decimal part
decimal_part = decimal_part.lstrip('0') or '0'
# Combine the parts
english_numerals = f"{integer_part}.{decimal_part}" if decimal_part != '0' else integer_part
else:
# Remove leading zeros in the integer part
english_numerals = english_numerals.lstrip('0') or '0'

# Return the Tibetan text and the converted number as a string in a list
return [tibetan_text, english_numerals]

def get_name_completions(sting_name, limit, ref_only, topic_override=False):
name = sting_name
lang = "he" if has_tibetan(name) else "en"
completer = library.ref_auto_completer(lang) if ref_only else library.full_auto_completer(lang)
object_data = None
ref = None
topic = None


if lang == "he":
segment = convert_tibetan_text_segment_number(name)
if segment[1]:
enRef = Ref(segment[0])
name = f"{enRef}.{segment[1]}"
else :
ref = None

if topic_override:
topic_set = TopicSet({"titles.text": re.compile(fr'^{re.escape(name)}$', flags=re.IGNORECASE)},
sort=[("numSources", -1)], limit=1)
Expand Down
5 changes: 3 additions & 2 deletions sefaria/model/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ def full_regex(self, title, lang, anchored=True, compiled=True, capture_title=Fa
"""
parentheses = kwargs.get("parentheses", False)
prefixes = 'בכ|וב|וה|וכ|ול|ומ|וש|כב|ככ|כל|כמ|כש|לכ|מב|מה|מכ|מל|מש|שב|שה|שכ|של|שמ|ב|כ|ל|מ|ש|ה|ו|ד' if lang == 'he' else ''
prefixes = '' if lang == 'he' else ''
prefix_group = rf'(?:{prefixes})?'
key = (title, lang, anchored, compiled, kwargs.get("for_js"), kwargs.get("match_range"), kwargs.get("strict"),
kwargs.get("terminated"), kwargs.get("escape_titles"), parentheses)
Expand Down Expand Up @@ -2599,7 +2599,8 @@ def _core_regex(self, lang, group_id=None, **kwargs):
if lang == "en":
reg += r"\d+)"
elif lang == "he":
reg += self.hebrew_number_regex() + r")"
# reg += self.hebrew_number_regex() + r")"
reg += r"\d+)" # todo: implement tibetan number regex

return reg

Expand Down
2 changes: 2 additions & 0 deletions static/css/s2.css
Original file line number Diff line number Diff line change
Expand Up @@ -12167,6 +12167,7 @@ span.purim-emoji img {
}

.addInterfaceInput {
padding: 10px;
pointer-events: none;
display: inline-block;
}
Expand Down Expand Up @@ -12221,6 +12222,7 @@ span.purim-emoji img {
.addInterfaceInput select.suggestionBox {
overflow: scroll;
font-size: 22px;
pointer-events: auto;
}


Expand Down
2 changes: 1 addition & 1 deletion static/js/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ const AddInterfaceInput = ({ inputType, resetInterface }) => {
getSuggestions={getSuggestions}
inputValue={inputValue}
changeInputValue={setInputValue}
inputPlaceholder="Search for a Text or Commentator."
inputPlaceholder={Sefaria._('sheet.editor.source.input_field.placeholder')}
buttonTitle="Add Source"
autocompleteClassNames="addInterfaceInput"
showSuggestionsOnSelect={true}
Expand Down
7 changes: 3 additions & 4 deletions static/js/Misc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,7 @@ class TextBlockLink extends Component {
}
const subtitle = displayValue ? (
<span className="blockLinkSubtitle">
<span className={elang}>{displayValue}</span>
<span className={hlang}>{heDisplayValue}</span>
<span className={elang}>{Sefaria.interfaceLang != 'hebrew' ? displayValue: heDisplayValue}</span>
</span>
) : null;

Expand Down Expand Up @@ -828,8 +827,7 @@ class TextBlockLink extends Component {
}
return (
<a href={url} className={classes} data-ref={sref} data-ven={currVersions.en} data-vhe={currVersions.he} data-position={position} style={style}>
<span className={elang}>{title}</span>
<span className={hlang}>{heTitle}</span>
<span >{Sefaria.interfaceLang != 'hebrew'? title: heTitle }</span>
{subtitle}
</a>
);
Expand Down Expand Up @@ -3234,6 +3232,7 @@ const Autocompleter = ({getSuggestions, showSuggestionsOnSelect, inputPlaceholde


const generatePreviewText = (ref) => {
console.log("getText", ref)
Sefaria.getText(ref, {context:1, stripItags: 1}).then(text => {
let segments = Sefaria.makeSegments(text, true);
segments = Sefaria.stripImagesFromSegments(segments);
Expand Down
2 changes: 1 addition & 1 deletion static/js/SourceEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const SourceEditor = ({topic, close, origData={}}) => {
getSuggestions={getSuggestions}
inputValue={displayRef}
changeInputValue={handleChange}
inputPlaceholder="Search for a Text or Commentator."
inputPlaceholder={Sefaria._('sheet.editor.source.input_field.placeholder')}
buttonTitle="Select Source"
autocompleteClassNames="addInterfaceInput"
showSuggestionsOnSelect={true}
Expand Down
8 changes: 3 additions & 5 deletions static/js/UserStats.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const UserDataBlock = ({user_data, site_data}) => (
const OverallActivityBlock = ({user_data}) => (
<div>
<h2>
<InterfaceText>"profile.user_state.overall_activities</InterfaceText>
<InterfaceText>profile.user_state.overall_activities</InterfaceText>
</h2>
<div className="statcardRow">
<StatCard icon_file="book-icon-black.svg" number={user_data.textsRead} name={Sefaria._("profile.text_read")} />
Expand Down Expand Up @@ -175,8 +175,7 @@ const YourFavoriteTextsBlock = ({user_data}) => (
user_data.mostViewedRefs.length ?
<div className="yourFavoriteTextsBlock">
<h2>
<span className="int-en">Your Favorite Texts</span>
<span className="int-he">ཁྱེད་རང་དགའ་ཤོས་ཀྱི་ཡིག་ཆ།</span>
<InterfaceText>profile.buddhist_tracker.favorite_texts</InterfaceText>
</h2>
<NBox n={3} content={user_data.mostViewedRefs.map((r,i) =>
<TextBlockLink key={i} sref={r.en} title={r.en} heTitle={r.he} book={r.book} intlang={true}/>)}/>
Expand All @@ -187,8 +186,7 @@ const YourFavoriteSheetsBlock = ({user_data}) => (
user_data.mostViewedSheets.length ?
<div className="yourFavoriteSheetsBlock">
<h2>
<span className="int-en">Your Favorite Sheets</span>
<span className="int-he">דפי מקורות מועדפים</span>
<InterfaceText>profile.buddhist_tracker.favorite_sheets</InterfaceText>
</h2>
<div className="story">
<StorySheetList sheets={user_data.mostViewedSheets} compact={true} smallfonts={true}/>
Expand Down
3 changes: 3 additions & 0 deletions static/js/sefaria/localizationLanguage/chinese.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@
"header.site_language": "网站语言",
"header.profileMenu.profile": "个人资料",
"header.profileMenu.create_New_Sheet": "创建新表单",
"profile.buddhist_tracker.favorite_texts": "您最喜爱的经文",
"profile.buddhist_tracker.favorite_sheets": "您最喜爱的表格",
"header.profileMenu.account_settings": "账户设置",
"header.profileMenu.log_out": "登出",
"profile.tab.collection.description": "您可以使用收藏来整理您的表单或公开您喜欢的表单。收藏可以私下分享或在Sefaria上公开",
Expand Down Expand Up @@ -470,6 +472,7 @@
"sheet.box_sources": "框选资源",
"sheet.sheet_language": "表单语言",
"sheet.editor.write_something": "Write something...",
"sheet.editor.source.input_field.placeholder": "搜尋文字或註解者。",
"sheet.sheet_layout": "表单布局",
"sheet.stacked": "堆叠",
"sheet.side_by_side": "并排",
Expand Down
3 changes: 3 additions & 0 deletions static/js/sefaria/localizationLanguage/english.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@
"header.profileMenu.create_New_Sheet": "Create a New Sheet",
"header.profileMenu.account_settings": "Account Settings",
"header.profileMenu.log_out": "Logout",
"profile.buddhist_tracker.favorite_texts": "Your Favorite Texts",
"profile.buddhist_tracker.favorite_sheets": "Your Favorite Sheets",
"profile.tab.collection.description": "You can use collections to organize your sheets or public sheets you like. Collections can be shared privately or made public on Pecha",
"profile.buddhish_text_tracker":"Buddhist Text Tracker",
"profile.previous_year":"Previous Year",
Expand Down Expand Up @@ -470,6 +472,7 @@
"sheet.format": "Format",
"sheet.number_sources": "Number Sources",
"sheet.box_sources": "Box Sources",
"sheet.editor.source.input_field.placeholder": "Search for a Text or Commentator.",
"sheet.sheet_language": "Sheet Language",
"sheet.sheet_layout": "Sheet Layout",
"sheet.stacked": "Stacked",
Expand Down
3 changes: 3 additions & 0 deletions static/js/sefaria/localizationLanguage/tibetan.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@
"header.profileMenu.profile": "ངོ་སྤྲོད་སྙིང་བསྡུས།",
"header.profileMenu.create_New_Sheet": "ཤོག་ངོས་གསར་པ་ཞིག་བཟོས།",
"header.profileMenu.account_settings": "ཁ་བྱང་སྒྲིག་འགོད།",
"profile.buddhist_tracker.favorite_texts": "ཁྱེད་རང་དགའ་ཤོས་ཀྱི་ཡིག་ཆ།",
"profile.buddhist_tracker.favorite_sheets": "ཁྱེད་རང་དགའ་ཤོས་ཀྱི་ཤོག་ལེབ།",
"header.profileMenu.log_out": "ཕྱིར་ཐོན།",
"profile.tab.collection.description": "ཟིན་བྲིས་བེད་སྤྱོད་བཏང་ནས། ལུང་འདྲེན་ཕྱོགས་བསྡུ་དང་། དེའི་སྐོར་ལ་རྩོམ་འབྲི་བ། སློབ་ཚན་སྒྲིག་པ། དཔྱད་རྩོམ་འབྲི་བ་ལ་སོགས་པ་གནང་ཐུབ་པ་ཡིན།",
"profile.buddhish_text_tracker":"སྤྱི་བསྡོམས་རྩིས་ཁྲ།",
Expand Down Expand Up @@ -471,6 +473,7 @@
"sheet.sheet_layout": "ཟིན་བྲིས་ཀྱི་རྣམ་པ།",
"sheet.stacked": "གོང་འོག།",
"sheet.side_by_side": "གཡས་གཡོན།",
"sheet.editor.source.input_field.placeholder": "ཡི་གེ ཡང་ན བསམ་བཀོད་མཛོད་པའི་མིང་འཚོལ།",
"sheet.side_by_side_layout": "གཡས་གཡོན་སྒྲིག་འགོད།",
"sheet.source_sheet_copied": "ཟིན་བྲིས་ངོ་བཤུ་བྱས་ཟིན།",
"sheet.view_copy": "ངོ་བཤུས་ལ་ལྟ།",
Expand Down
14 changes: 14 additions & 0 deletions static/js/sefaria/sefaria.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,17 @@ Sefaria = extend(Sefaria, {
}
return null;
},
isTibetan: (text) => {
// Tibetan Unicode range: U+0F00 to U+0FFF
const tibetanPattern = /^[\u0F00-\u0FFF]+$/;
return tibetanPattern.test(text);
},

isEnglish: (text) => {
// English uses basic Latin alphabet (A-Z, a-z) and spaces
const englishPattern = /^[A-Za-z\s]+$/;
return englishPattern.test(text);
},
getText: function(ref, settings) {
// returns a promise
settings = this._complete_text_settings(settings);
Expand Down Expand Up @@ -1208,6 +1219,9 @@ Sefaria = extend(Sefaria, {
// If no open calls found, call the texts API.
// Called with context:1 because this is our most common mode, maximize change of saving an API Call
return Sefaria.getText(ref, {context: 1});
},
getEnRefForBO: (boRef) => {

},
ref: function(ref, callback) {
if (callback) {
Expand Down
10 changes: 5 additions & 5 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@
</script>
<script src="https://www.gstatic.com/charts/loader.js"></script>

<link rel="stylesheet" href="{% static 'css/common.css' %}">
<link rel="stylesheet" href="{% static 'font-awesome/css/font-awesome.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'css/keyboard.css' %}">
<link rel="stylesheet" href="{% versioned_static 'css/common.css' %}">
<link rel="stylesheet" href="{% versioned_static 'font-awesome/css/font-awesome.css' %}">
<link rel="stylesheet" type="text/css" href="{% versioned_static 'css/keyboard.css' %}">
<link rel="stylesheet" href="{% versioned_static 'css/s2.css' %}">
<link rel="stylesheet" href="{% static 'css/s2-print.css' %}" media="print" />
<link rel="stylesheet" href="{% versioned_static 'css/s2-print.css' %}" media="print" />

{% block static_css %}
{% if not html %}
<link rel="stylesheet" href="{% static 'css/static.css' %}">
<link rel="stylesheet" href="{% versioned_static 'css/static.css' %}">
{% endif %}
{% endblock %}

Expand Down

0 comments on commit 1950be1

Please sign in to comment.