当前位置: 首页 > news >正文

营销型企业网站的功能担保公司发展规划

营销型企业网站的功能,担保公司发展规划,wordpress ftp上传文件,91手机用哪个浏览器java 字符串对齐有一阵子#xff0c;我使用了Levenshtein distance的Apache Commons lang StringUtils实现。 它实现了一些众所周知的技巧#xff0c;通过仅挂接到两个数组而不是为备忘录表分配巨大的nxm表来使用较少的内存。 它还仅检查宽度为2 * k 1的“条带”#xff0c;… java 字符串对齐 有一阵子我使用了Levenshtein distance的Apache Commons lang StringUtils实现。 它实现了一些众所周知的技巧通过仅挂接到两个数组而不是为备忘录表分配巨大的nxm表来使用较少的内存。 它还仅检查宽度为2 * k 1的“条带”其中k是最大编辑次数。 在levenshtein的大多数实际用法中您只关心一个字符串是否在另一个字符串的少量编辑1、2、3之内。 这避免了使levenstein变得“昂贵”的大部分n * m计算。 我们发现在ak 3的情况下具有这些技巧的levenshtein的速度比Jaro-Winkler distance快后者是一种近似编辑距离计算被创建为更快的近似值这有很多原因。 不幸的是Apache Commons Lang实现仅计算Levenshtein而不计算可能更有用的Damerau-Levenshtein距离 。 Levenshtein定义了编辑操作的插入删除和替换。 Damerau变体将* transposition *添加到列表中这对于我使用编辑距离的大多数位置都非常有用。 不幸的是DL距离不是真正的度量标准因为它不考虑三角形不等式但是有很多应用不受此影响。 从该维基百科页面可以看到“最佳字符串对齐”和DL距离之间经常会混淆。 实际上OSA是一种更简单的算法并且需要较少的簿记因此运行时间可能略微更快。 我找不到任何使用我在Apache Commons Lang中看到的内存技巧和“条带化”技巧的OSA或DL实现。 因此我使用这些技巧实现了自己的OSA。 在某些时候我还将使用技巧来实现DL并查看性能差异是什么 这是Java中的OSA。 它是公共领域 随意使用。 单元测试如下。 唯一的依赖关系是Guava-但它只是前提条件类和文档注释因此如果您愿意可以轻松删除该依赖关系 package com.github.steveash.util;import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.primitives.Shorts.checkedCast; import static java.lang.Math.abs; import static java.lang.Math.max;import java.util.Arrays;import com.google.common.annotations.VisibleForTesting;/*** Implementation of the OSA which is similar to the Damerau-Levenshtein in that it allows for transpositions to* count as a single edit distance, but is not a true metric and can over-estimate the cost because it disallows* substrings to edited more than once. See wikipedia for more discussion on OSA vs DL* p/* See Algorithms on Strings, Trees and Sequences by Dan Gusfield for more information.* p/* This also has a set of local buffer implementations to avoid allocating new buffers each time, which might be* a premature optimization* p/* author Steve Ash*/ public class OptimalStringAlignment {private static final int threadLocalBufferSize 64;private static final ThreadLocalshort[] costLocal new ThreadLocalshort[]() {Overrideprotected short[] initialValue() {return new short[threadLocalBufferSize];}};private static final ThreadLocalshort[] back1Local new ThreadLocalshort[]() {Overrideprotected short[] initialValue() {return new short[threadLocalBufferSize];}};private static final ThreadLocalshort[] back2Local new ThreadLocalshort[]() {Overrideprotected short[] initialValue() {return new short[threadLocalBufferSize];}};public static int editDistance(CharSequence s, CharSequence t, int threshold) {checkNotNull(s, cannot measure null strings);checkNotNull(t, cannot measure null strings);checkArgument(threshold 0, Threshold must not be negative);checkArgument(s.length() Short.MAX_VALUE, Cannot take edit distance of strings longer than 32k chars);checkArgument(t.length() Short.MAX_VALUE, Cannot take edit distance of strings longer than 32k chars);if (s.length() 1 threadLocalBufferSize || t.length() 1 threadLocalBufferSize)return editDistanceWithNewBuffers(s, t, checkedCast(threshold));short[] cost costLocal.get();short[] back1 back1Local.get();short[] back2 back2Local.get();return editDistanceWithBuffers(s, t, checkedCast(threshold), back2, back1, cost);}VisibleForTestingstatic int editDistanceWithNewBuffers(CharSequence s, CharSequence t, short threshold) {int slen s.length();short[] back1 new short[slen 1]; // up 1 row in tableshort[] back2 new short[slen 1]; // up 2 row in tableshort[] cost new short[slen 1]; // current costreturn editDistanceWithBuffers(s, t, threshold, back2, back1, cost);}private static int editDistanceWithBuffers(CharSequence s, CharSequence t, short threshold,short[] back2, short[] back1, short[] cost) {short slen (short) s.length();short tlen (short) t.length();// if one string is empty, the edit distance is necessarily the length of the otherif (slen 0) {return tlen threshold ? tlen : -1;} else if (tlen 0) {return slen threshold ? slen : -1;}// if lengths are different k, then cant be within edit distanceif (abs(slen - tlen) threshold)return -1;if (slen tlen) {// swap the two strings to consume less memoryCharSequence tmp s;s t;t tmp;slen tlen;tlen (short) t.length();}initMemoiseTables(threshold, back2, back1, cost, slen);for (short j 1; j tlen; j) {cost[0] j; // j is the cost of inserting this many characters// stripe boundsint min max(1, j - threshold);int max min(slen, (short) (j threshold));// at this iteration the left most entry is too much so reset itif (min 1) {cost[min - 1] Short.MAX_VALUE;}iterateOverStripe(s, t, j, cost, back1, back2, min, max);// swap our cost arrays to move on to the next rowshort[] tempCost back2;back2 back1;back1 cost;cost tempCost;}// after exit, the current cost is in back1// if back1[slen] k then we exceeded, so return -1if (back1[slen] threshold) {return -1;}return back1[slen];}private static void iterateOverStripe(CharSequence s, CharSequence t, short j,short[] cost, short[] back1, short[] back2, int min, int max) {// iterates over the stripefor (int i min; i max; i) {if (s.charAt(i - 1) t.charAt(j - 1)) {cost[i] back1[i - 1];} else {cost[i] (short) (1 min(cost[i - 1], back1[i], back1[i - 1]));}if (i 2 j 2) {// possible transposition to check forif ((s.charAt(i - 2) t.charAt(j - 1)) s.charAt(i - 1) t.charAt(j - 2)) {cost[i] min(cost[i], (short) (back2[i - 2] 1));}}}}private static void initMemoiseTables(short threshold, short[] back2, short[] back1, short[] cost, short slen) {// initial starting values for inserting all the lettersshort boundary (short) (min(slen, threshold) 1);for (short i 0; i boundary; i) {back1[i] i;back2[i] i;}// need to make sure that we dont read a default value when looking upArrays.fill(back1, boundary, slen 1, Short.MAX_VALUE);Arrays.fill(back2, boundary, slen 1, Short.MAX_VALUE);Arrays.fill(cost, 0, slen 1, Short.MAX_VALUE);}private static short min(short a, short b) {return (a b ? a : b);}private static short min(short a, short b, short c) {return min(a, min(b, c));} }import org.junit.Testimport static com.github.steveash.util.OptimalStringAlignment.editDistance/*** author Steve Ash*/ class OptimalStringAlignmentTest {Testpublic void shouldBeZeroForEqualStrings() throws Exception {assert 0 editDistance(steve, steve, 1)assert 0 editDistance(steve, steve, 0)assert 0 editDistance(steve, steve, 2)assert 0 editDistance(steve, steve, 100)assert 0 editDistance(s, s, 1)assert 0 editDistance(s, s, 0)assert 0 editDistance(s, s, 2)assert 0 editDistance(s, s, 100)assert 0 editDistance(, , 0)assert 0 editDistance(, , 1)assert 0 editDistance(, , 100)}Testpublic void shouldBeOneForSingleOperation() throws Exception {def a steve;for (int i 0; i 5; i) {assertOneOp(new StringBuilder(a).insert(i, f), a)assertOneOp(new StringBuilder(a).deleteCharAt(i), a)def sb new StringBuilder(a)sb.setCharAt(i, x as char);assertOneOp(sb, a)if (i 1) {sb new StringBuilder(a)char t sb.charAt(i - 1)sb.setCharAt(i - 1, sb.charAt(i))sb.setCharAt(i, t)println comparing sb.toString() - aassertOneOp(sb, a)}}}Testpublic void shouldCountTransposeAsOne() throws Exception {assert 3 editDistance(xxsteve, steev, 4)assert 3 editDistance(xxsteve, steev, 3)assert 3 editDistance(steev, xxsteve, 4)assert 3 editDistance(steev, xxsteve, 3)assert -1 editDistance(steev, xxsteve, 2)assert 4 editDistance(xxtseve, steev, 4)assert 5 editDistance(xxtsevezx, steevxz, 5)assert 6 editDistance(xxtsevezx, steevxzpp, 6)assert 7 editDistance(xxtsfevezx, steevxzpp, 7)assert 4 editDistance(xxtsf, st, 7)assert 4 editDistance(evezx, eevxzpp, 7)assert 7 editDistance(xxtsfevezx, steevxzpp, 7)}Testpublic void shouldCountLeadingCharacterTranspositionsAsOne() throws Exception {assert 1 editDistance(rosa, orsa, 2)}private void assertOneOp(CharSequence a, CharSequence b) {assert 1 editDistance(a, b, 1)assert 1 editDistance(b, a, 1)assert 1 editDistance(a, b, 2)assert 1 editDistance(b, a, 2)}Testpublic void shouldShortCutWhenSpecialCase() throws Exception {assert 1 editDistance(s, , 1)assert 1 editDistance(, s, 1)assert -1 editDistance(s, , 0)assert -1 editDistance(, s, 0)assert -1 editDistance(st, , 1)assert -1 editDistance(, st, 1)assert -1 editDistance(steve, ste, 0)assert -1 editDistance(ste, steve, 0)assert -1 editDistance(stev, steve, 0)assert -1 editDistance(ste, steve, 1)assert -1 editDistance(steve, ste, 1)assert 1 editDistance(steve, stev, 1)assert 1 editDistance(stev, steve, 1)} } 参考来自我们的JCG合作伙伴史蒂夫·阿什Steve Ash的Java实现最佳字符串对齐 该博客来自Many Cups of Coffee博客。 翻译自: https://www.javacodegeeks.com/2013/11/java-implementation-of-optimal-string-alignment.htmljava 字符串对齐
http://www.huolong8.cn/news/404495/

相关文章:

  • 和平网站建设优化seo用wix做外贸网站
  • 浙江建设职业技术学院招生网站wordpress游戏充值
  • 网站建设 讲话wordpress主题绕过激活码
  • 网站建设的通知网站维护分工win2008系统asp网站建设
  • 网站帮助文档怎么写网站建设考题
  • 四大门户网站的区别那个网站做代买
  • 做网站报价明细表绿色农产品网站 模板
  • 网站开发培训网站建设工程网站有哪些
  • 深圳seo优化排名公司赣州网络优化
  • 网站管理后台下载做业帮网站
  • 科创纵横 网站建设wordpress 微信导航菜单
  • 像素时代网站建设手机站设计天助网站
  • 举报网站平台安徽网站建站系统哪家好
  • 网站如何做百度搜索优化怎么让百度收录我的网站
  • 有哪些做图纸的网站数学 wordpress
  • 戚墅堰做网站做网络推广一个月多少钱
  • 杭州科技网站西宁网站建设嘉荐君博l
  • 广州网站公司推广建设南宁网站建设牛易飞
  • 网站目录层级建设中国网站建设世界排名
  • 网站导航字体大小网站安全建设论文
  • 购物网站开发报告建设一个农家乐网站
  • 本地网站搭建网站绑定微信公众号
  • 宿州网站建设零聚思放心页面模板怎么添加文章
  • 做网站阿里云记录值怎么填做旅游网站的任务
  • 鞍山做网站比较好的公司网站加友情链接
  • 广州做营销型网站网站建设哪些字体没有版权
  • 珠海商城网站制作胖子马wordpress模板 q8免费版
  • WaP网站模块长沙百度推广公司电话
  • 手机网站支付如何制作太原网站建设信息推荐
  • 客户制作网站时的问题个人备案的网站能做盈利吗