- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 PHP 中,我使用函数 levenshtein() 计算 Levenshtein 距离。对于简单字符,它按预期工作,但对于示例中的变音符号字符
echo levenshtein('à', 'a');
它返回“2”。在这种情况下,只需进行一次替换,因此我希望它返回“1”。
我错过了什么吗?
最佳答案
我认为 this comment from the PHP manual 可能会有用作为这个问题的答案发布,所以这里是:-
levenshtein 函数单独处理输入字符串的每个字节。那么对于多字节编码,例如UTF-8,它可能会给出误导性的结果。
带有法语口音的单词示例:- levenshtein('notre', 'votre') = 1- levenshtein('notre', 'nôtre') = 2(哈?!)
您可以轻松找到 levenshtein 函数的多字节兼容 PHP 实现,但它当然会比 C 实现慢得多。
另一个选项是将字符串转换为单字节(无损)编码,以便它们可以提供给快速核心 levenshtein 函数。
这是我在存储 UTF-8 字符串的搜索引擎中使用的转换函数,以及一个快速基准测试。我希望它会有所帮助。
<?php
// Convert an UTF-8 encoded string to a single-byte string suitable for
// functions such as levenshtein.
//
// The function simply uses (and updates) a tailored dynamic encoding
// (in/out map parameter) where non-ascii characters are remapped to
// the range [128-255] in order of appearance.
//
// Thus it supports up to 128 different multibyte code points max over
// the whole set of strings sharing this encoding.
//
function utf8_to_extended_ascii($str, &$map)
{
// find all multibyte characters (cf. utf-8 encoding specs)
$matches = array();
if (!preg_match_all('/[\xC0-\xF7][\x80-\xBF]+/', $str, $matches))
return $str; // plain ascii string
// update the encoding map with the characters not already met
foreach ($matches[0] as $mbc)
if (!isset($map[$mbc]))
$map[$mbc] = chr(128 + count($map));
// finally remap non-ascii characters
return strtr($str, $map);
}
// Didactic example showing the usage of the previous conversion function but,
// for better performance, in a real application with a single input string
// matched against many strings from a database, you will probably want to
// pre-encode the input only once.
//
function levenshtein_utf8($s1, $s2)
{
$charMap = array();
$s1 = utf8_to_extended_ascii($s1, $charMap);
$s2 = utf8_to_extended_ascii($s2, $charMap);
return levenshtein($s1, $s2);
}
?>
结果(约 6000 个调用)- 核心 C 函数引用时间(单字节):30 ms- utf8 到 ext-ascii 转换 + 核心功能:90 ms- 完整的 php 实现:3000 毫秒
关于php - 变音符号的编辑距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26271656/
基本上,我想在音序器播放时实时获取并显示 MIDI 音符(轨道、音符和 Octave )的数据。 我希望能够将其添加到以下代码中: Sequencer sequencer = MidiSystem.g
播放我使用的是 AVAudioUnitSampler: sampler.stopNote(note, onChannel: channel) 但要停止我不想使用:
一个接一个地弹奏音符的最佳方式是什么?这是我现在拥有的最实用的代码。 let majorScale = [60, 62, 64, 65, 67, 69, 71, 72] for i i
我正在尝试构建一个程序,使用 Java Sound API (javax.sound.midi) 在合成器播放时在屏幕上显示 MIDI 音符。 MIDI 文件需要从磁盘加载,但我在将音序器连接到合成器
我已经搜索过并且已经完成了一个可以播放 MIDI 音符的 OS X 应用程序,但是当我在 iOS 中尝试时,没有任何反应。这是核心代码: AUGraph graph; AudioUnit synthU
我正在开发一个 iOS 音乐应用程序(用 C++ 编写),我的模型看起来或多或少像这样: --Song ----Track ----Track ------Pattern ------Pattern
我正在尝试构建一个系统,该系统将能够处理某人吹口哨的记录并输出注释。 谁能推荐一个开源平台,我可以将其用作音符/音高识别和波形文件分析的基础? 提前致谢 最佳答案 正如其他许多人已经说过的那样,FFT
我正在尝试使用 AUSampler 音频单元 在 sf2 文件的帮助下播放 midi 音符。我能够使用 MusicDeviceMIDIEvent 函数开始和停止音符,如下所示。 noteCommand
我需要在我的应用中同时播放多个轨道。音轨可以是普通音频文件或一组 MIDI 音符。因此,我配置了音频图并将各种音频单元(文件播放器 AU 和采样器 AU)连接到多 channel 混音器,其输出连接到
我正在开发一个适用于 iOS 的琶音器应用程序,它能够将无限的音符序列发送到外部 MIDI 兼容的硬件合成器。我使用 AudioKit 来接收和发送 MIDI 信息。 我的工作室设置包括 E-RM 多
我正在对一种似乎是带有音色库的 MIDI 的音乐格式进行逆向工程。 但是我无法使我的实现听起来正确,因为我不知道如何以给定的 MIDI 音符播放音库中的样本(我只是以它们的原始 khz 播放它们)。
澄清一下:我不想生成 MIDI 文件,也不想播放 MIDI 文件,我希望即时播放 MIDI 音符。 我尝试使用 https://github.com/mudcube/MIDI.js作为 MIDI 库,
我正在开发一个具有多个“触摸区域”的玩具,这些区域用作我的合成器的输入。 使用 oscillatorBank 我可以轻松地开始和停止音符。 在常规键盘中,每个 Midi 音符只有 1 个键,因此“重新
正在为 android 开发一个简单的键盘。我发现来自 Google 的 android-midi-lib 非常有用和强大,但在我看来它只能播放 midi 文件,但不能在键盘上按下时播放实时 midi
所以我觉得自己像个白痴,因为我必须在这里发帖来解决可能是简单的修复问题。我正在编写一个将 MIDI 发送到另一个应用程序的 iOS 应用程序。对于我的第一个概念证明,我试图在我的 iPhone 上向
我正在制作一个 C# 程序,它应该从 MIDI 文件中读取每个音符并获取音符的长度和速度,但我不知道该怎么做... 我正在使用 DryWetMidi图书馆,但还有其他图书馆吗? 最佳答案 以 DryW
我正在制作一个 C# 程序,它应该从 MIDI 文件中读取每个音符并获取音符的长度和速度,但我不知道该怎么做... 我正在使用 DryWetMidi图书馆,但还有其他图书馆吗? 最佳答案 以 DryW
我希望使用 Swift 在 iOS 应用程序上播放单个 midi 音符。 这是我目前所拥有的,但不幸的是,这会因错误而崩溃。 func initAudio(){ let engine = AV
我儿子弹奏的弦乐器听起来非常走调。我更希望他学习一些简单的 Python 编程来演奏真正听起来像 do-re-mi 的 do-re-mi 音符。 首先,我想在 Python 上玩 do-re-mi,每
我正在使用 Arduino Yun 和 MIDI 转 USB 接口(interface),我的目标是在已安装的 Linux 操作系统上读取传入的 MIDI 音符。 目标是让 Yun 上的计算机读取音符
我是一名优秀的程序员,十分优秀!