首页 > 编程语言 >两种方法实现西里尔哈萨克文到传统哈萨克文的转换(JavaScript代码)

两种方法实现西里尔哈萨克文到传统哈萨克文的转换(JavaScript代码)

时间:2024-06-19 22:23:40浏览次数:31  
标签:word cyrillicToArabicMap 文到 replace char var str 哈萨克 西里尔

废话少说,直接上代码!
一,正则处理法:

function kazakhCyrillicToArabic(str) {
    str = str.replace(/,/g, "،");
    str = str.replace(/;/g, "؛");
    str = str.replace(/\?/g, "؟");
    var cyrillicToArabicMap = {
        "а": "ا", "ә": "ءا", "б": "ب", "в": "ۆ",
        "г": "گ", "ғ": "ع", "д": "د", "е": "ە",
        "ё": "يو", "ж": "ج", "з": "ز", "и": "ي",
        "й": "ي", "к": "ك", "қ": "ق", "л": "ل",
        "м": "م", "н": "ن", "ң": "ڭ", "о": "و",
        "ө": "ءو", "п": "پ", "р": "ر", "с": "س",
        "т": "ت", "у": "ۋ", "ұ": "ۇ", "ү": "ءۇ",
        "ф": "ف", "х": "ح", "һ": "ھ", "ц": "س",
        "ч": "چ", "ш": "ش", "щ": "شش", "ъ": "",
        "ы": "ى", "і": "ءى", "ь": "", "э": "ە",
        "ю": "يۋ", "я": "يا"
    };
    str = str.replace(/[аәбвгғдеёжзийкқлмнңоөпрстуұүфхһцчшщъыіьэюя]+/gi, function (word) {
        for (var char in cyrillicToArabicMap) {
            var arabicChar = cyrillicToArabicMap[char];
            var charPattern = new RegExp(char, "gi");
            word = word.replace(charPattern, arabicChar);
        }
        if (word.match(/ء/g)) {
            if (word.match(/[ەگك]/g)) {
                word = word.replace(/ء/g, "");
            } else {
                word = "ء" + word.replace(/ء/g, "");
            }
        }
        return word;
    });
    return str;
}

二,字符串遍历法(推荐):

function kazakhCyrillicToArabic(str) {
    var strLength = str.length;
    var outputString = "";
    var cyrillicToArabicMap = {
        "а": "ا", "ә": "ا", "б": "ب", "в": "ۆ",
        "г": "گ", "ғ": "ع", "д": "د", "е": "ە",
        "ё": "يو", "ж": "ج", "з": "ز", "и": "ي",
        "й": "ي", "к": "ك", "қ": "ق", "л": "ل",
        "м": "م", "н": "ن", "ң": "ڭ", "о": "و",
        "ө": "و", "п": "پ", "р": "ر", "с": "س",
        "т": "ت", "у": "ۋ", "ұ": "ۇ", "ү": "ۇ",
        "ф": "ف", "х": "ح", "һ": "ھ", "ц": "س",
        "ч": "چ", "ш": "ش", "щ": "شش", "ъ": "",
        "ы": "ى", "і": "ى", "ь": "", "э": "ە",
        "ю": "يۋ", "я": "يا",
        ",": "،", ";": "؛", "?": "؟"
    };
    var isInCyrillic = false;
    var hasFrontVowels = false;
    var hasGEK = false;
    var currentPosition = 0;
    var currentArabicLength = 0;
    var hamzaCount = 0;
    for (var i = 0; i <= strLength; i++) {
        var char = str.substr(i, 1);
        if (char.charCodeAt(0) >= 1024 && char.charCodeAt(0) <= 1279) { // 西里尔
            char = char.toLowerCase();
            if (isInCyrillic == false) {
                isInCyrillic = true;
            }
            if (char == "ә" || char == "ө" || char == "ү" || char == "і") {
                hasFrontVowels = true;
            }
            if (char == "г" || char == "е" || char == "к" || char == "э") {
                hasGEK = true;
            }
            outputString += cyrillicToArabicMap[char];
            currentPosition += cyrillicToArabicMap[char].length;
            currentArabicLength += cyrillicToArabicMap[char].length;
        } else {
            if (isInCyrillic) {
                isInCyrillic = false;
                if (hasFrontVowels && !hasGEK) {
                    outputString = outputString.substring(0, currentPosition - currentArabicLength + hamzaCount) + "\u0621" + outputString.substring(currentPosition - currentArabicLength + hamzaCount);
                    hamzaCount++;
                }
                hasFrontVowels = false;
                hasGEK = false;
                currentArabicLength = 0;
            }
            outputString += char;
            currentPosition++;
        }
    }
    return outputString;
}

标签:word,cyrillicToArabicMap,文到,replace,char,var,str,哈萨克,西里尔
From: https://www.cnblogs.com/noorty/p/18257587

相关文章

  • 哈萨克语打字通(免费版)
    Kazakhkeyboardpractice(free)Introduction:KazakhTypewritingisaneducationalsoftwarespeciallydesignedforuserswhoarelearningKazakhlanguageandcomputerscience.ItaimstohelpuserseasilymasterKazakhlanguagetextinputskills.Since200......
  • 发表一篇SCI论文到底有多难?审稿需要多久?
    SCI论文,是指被SCI即科学引文索引所收录的SCI期刊上刊登的学术期刊论文。发表SCI论文,对科研工作者的综合素质要求比较高,论文写好,将论文投稿出去后,也会有一个漫长的等待过程。一、SCI论文发表多难点1、不同研究方向研究难度不同,有的学科领域天生有优势容易出结果。例如在同样......
  • 在编程中使用中文到底该不该??
    看到知乎上有个热门问题,为什么很多人反对中文在编程中的使用?这个问题有几百万的浏览热度,其中排名第一的回答非常简洁,我深以为然:在国内做开发,用中文写注释、写文档,是非常好的习惯,因为太缺优秀的中文文档了,目之所及很多框架都没有完整的中文文档。除此之外,其他形式用中文......
  • 复制word图文到富文本编辑器
    任务:用户需要将大量word中的图文粘贴到网站富文本编辑器。难点大部分富文本编辑器能做到还原word的文本格式,例如字体字号表格以及布局相关的要素还原。但普遍对图片的支持比较差,都需要手动点击图片上传按钮,然后在光标的当前位置,插入指定的图片。这样用户的体验就非常糟糕,为什......
  • 如何复制word的图文到百度编辑器中自动上传
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......
  • 如何复制word的图文到CKEditor中自动上传
    ​ 在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper。通过知乎提供的思路找到粘贴的原理,通过TheViper找到粘贴图片的方法。其原理为一下步骤:监听粘贴事件;【用于插入图片】获取光标位置;【......
  • 如何复制word的图文到FCKEditor中自动上传
    ​ 如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>......
  • 如何复制word的图文到xhEditor中自动上传
    ​ 1.编辑器修改(可选)1.1在 ueditor/config.json 中添加代码块    /* 上传word配置 */    "wordActionName":"wordupload",/* 执行上传视频的action名称 */    "wordFieldName":"upfile",/* 提交的视频表单名称 */    "wordPathFormat":"/p......
  • 如何复制word的图文到wangEditor中自动上传
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......
  • 如何复制word的图文到Web编辑器中自动上传
    ​ 由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直接解决这个问题考虑到自己除了工作其他时间基本上不使用windows,因此打算使用nodejs来......