Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有几个地方区域的读音存在疑惑 #256

Closed
M69W opened this issue Jul 3, 2024 · 3 comments · Fixed by #260
Closed

有几个地方区域的读音存在疑惑 #256

M69W opened this issue Jul 3, 2024 · 3 comments · Fixed by #260
Labels
dictionary Problem of directory

Comments

@M69W
Copy link

M69W commented Jul 3, 2024

问题描述

请输入遇到的问题...

伯明翰 【 baiminghan bominghan 】
伯力 【 baili boli 】
新西伯利亚 【 xinxibailiya xinxiboliya 】

昌都 【 changdou changdu 】
加德满都 【 jiademandou jiademandu 】
内比都 【 neibidou neibidu 】

底特律 【 ditelü ditelv 】
吕梁 【 lüliang lvliang 】

上面这些用哪些才是“对”的

@zh-lx
Copy link
Owner

zh-lx commented Jul 8, 2024

感谢反馈,上面的 后面的拼音为正确的,其中 需要本库更正其默认读音, 作为地名,后续会出一个地名包添加进去。

下面的 底特律吕梁,汉语拼音中不存在 v 这个符号,只有 ü。因为书写方便很多网站可能使用 v 代替 ü,也可以对 pinyin api 添加 v: true 的设置将结果中的 ü 替换为 v

@zh-lx zh-lx added the dictionary Problem of directory label Jul 8, 2024
@M69W
Copy link
Author

M69W commented Jul 9, 2024

customPinyin.js

import {
  pinyin,
  // addDict, // addDict 是用来让用户自定义词典的 api,在拼音转换时,会根据通过 addDict 添加的词的权重来定义优先级。
  customPinyin // 如果要强制定义所有单字的读音,可以使用 customPinyin api,命中 customPinyin 的字词都会优先走这个 api 定义的读音
} from "pinyin-pro";

/**
 * 强制定义某些单字的读音
 */
export function customPinyinFunc() {
  // 国家/地区名
  const arr = [
    {
      伯: "bo",
      都: "du"
    }
  ];
  for (let index = 0; index < arr.length; index++) {
    // addDict(arr[index]);
    customPinyin(arr[index]);
  }
}


/**
 * 为什么要单独写,编译后使用 CDN 会报 En.pinyin is not a function
 * @param {*} text
 * @param {*} options
 * @returns
 */
export function pinyinR(text, options) {
  const pinyinValue = pinyin(text, options);
  return pinyinValue;
}

index.js

// import { pinyin } from "pinyin-pro";
import { customPinyinFunc, pinyinR } from "./customPinyin";

let index = 0;

/**
 * 强制定义某些单字的读音
 * @param {String} text 需要进行拼音转换的字符串
 * @param {String} options 转换输出的内容及格式
 */
export function customPinyin(text, options) {
  index === 0 && customPinyinFunc();
  index += 1;
  if (text) {
    // return pinyin(text, options);
    return pinyinR(text, options);
  } else {
    console.warn("请输入需要转换拼音的字符串");
    return false;
  }
}

index.vue

const proValue = customPinyin(chnName, {
          toneType: "none",
          nonZh: "consecutive",
          v: true
        })
          .replace(/\s*/g, "")
          .toUpperCase();

目前的做法能解决以上所说的

使用中还遇到两个问题
1、如果是使用CDN引用 pinyin-pro ,报错 En.pinyin is not a function,
解决方法是再次在自定义函数里面再调用一次,【pinyinR】的作用在于此。
2、如果输入 【hongkong 或 hk】去搜索 香港【HONG KONG】,同时支持中文、英文名、声母、全拼,
根据上面的设置,需要去空把 .replace(/\s*/g, "").toUpperCase(); 上面的设置只支持想要功能中的一种,
除了每个功能都设置一遍来实现这几个功能,还有没有更好的方法实现这种特定的功能?

@zh-lx
Copy link
Owner

zh-lx commented Jul 9, 2024

customPinyin.js

import {
  pinyin,
  // addDict, // addDict 是用来让用户自定义词典的 api,在拼音转换时,会根据通过 addDict 添加的词的权重来定义优先级。
  customPinyin // 如果要强制定义所有单字的读音,可以使用 customPinyin api,命中 customPinyin 的字词都会优先走这个 api 定义的读音
} from "pinyin-pro";

/**
 * 强制定义某些单字的读音
 */
export function customPinyinFunc() {
  // 国家/地区名
  const arr = [
    {
      伯: "bo",
      都: "du"
    }
  ];
  for (let index = 0; index < arr.length; index++) {
    // addDict(arr[index]);
    customPinyin(arr[index]);
  }
}


/**
 * 为什么要单独写,编译后使用 CDN 会报 En.pinyin is not a function
 * @param {*} text
 * @param {*} options
 * @returns
 */
export function pinyinR(text, options) {
  const pinyinValue = pinyin(text, options);
  return pinyinValue;
}

index.js

// import { pinyin } from "pinyin-pro";
import { customPinyinFunc, pinyinR } from "./customPinyin";

let index = 0;

/**
 * 强制定义某些单字的读音
 * @param {String} text 需要进行拼音转换的字符串
 * @param {String} options 转换输出的内容及格式
 */
export function customPinyin(text, options) {
  index === 0 && customPinyinFunc();
  index += 1;
  if (text) {
    // return pinyin(text, options);
    return pinyinR(text, options);
  } else {
    console.warn("请输入需要转换拼音的字符串");
    return false;
  }
}

index.vue

const proValue = customPinyin(chnName, {
          toneType: "none",
          nonZh: "consecutive",
          v: true
        })
          .replace(/\s*/g, "")
          .toUpperCase();

目前的做法能解决以上所说的

使用中还遇到两个问题 1、如果是使用CDN引用 pinyin-pro ,报错 En.pinyin is not a function, 解决方法是再次在自定义函数里面再调用一次,【pinyinR】的作用在于此。 2、如果输入 【hongkong 或 hk】去搜索 香港【HONG KONG】,同时支持中文、英文名、声母、全拼, 根据上面的设置,需要去空把 .replace(/\s*/g, "").toUpperCase(); 上面的设置只支持想要功能中的一种, 除了每个功能都设置一遍来实现这几个功能,还有没有更好的方法实现这种特定的功能?

  1. 第一个看起来像是你打包的问题,不是库本身的问题,有例子可以看下吗?
  2. 第二个如果你是要做搜索匹配,可以结合 addDictmatch api 实现比较方便,我给你写了个例子,可以看下:example

@zh-lx zh-lx linked a pull request Jul 15, 2024 that will close this issue
@zh-lx zh-lx closed this as completed Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dictionary Problem of directory
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants