AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

terms

简介

Yuka.js 中的 terms 是一个模糊匹配词汇的工具类。它提供了常用的方法来进行字符串的模糊匹配,例如 jaccard 系数、overlap 系数等。

方法

1. jaccard

jaccard(a, b) 方法用于计算两个字符串之间的 Jaccard 系数,其计算公式为:

J(A, B) = |A ∩ B| / |A ∪ B|

其中,A 和 B 分别表示两个字符串,|A ∩ B| 表示两个字符串中共同出现的字符的个数,|A ∪ B| 表示两个字符串中出现的所有字符的个数。

该方法返回值为一个数字,0 <= value <= 1,数值越大表示字符串相似度越高。

参数说明:

  • a:字符串 A。
  • b:字符串 B。

2. overlap

overlap(a, b, n) 方法用于计算两个字符串的重叠系数,其计算公式为:

O(A,B,n) = levenshtein(A,B)/min(n,|A|,|B|)

其中,A 和 B 分别表示两个字符串,n 表示 AB 中需要比较的最大字符数目,levenshtein(A,B) 表示字符串 A 和 B 之间的 Levenshtein 距离(编辑距离),min(n,|A|,|B|) 表示 nAB 字符串长度中的最小值。

该方法返回值为一个数字,0 <= value <= 1,数值越大表示字符串相似度越高。

参数说明:

  • a:字符串 A。
  • b:字符串 B。
  • n:最大字符数目。

3. contains

contains(str, substr) 方法用于判断一个字符串是否包含指定的子字符串。

该方法返回值为一个布尔值,true 表示包含,false 表示不包含。

参数说明:

  • str:需要检查的字符串。
  • substr:需要查找的子字符串。

4. equals

equals(a, b) 方法用于判断两个字符串是否相等。

该方法返回值为一个布尔值,true 表示相等,false 表示不相等。

参数说明:

  • a:字符串 A。
  • b:字符串 B。

使用示例

import { terms } from 'yuka';

const stringA = 'hello world';
const stringB = 'world hello';
const stringC = 'welcome to yuka.js';

// 计算 Jaccard 系数
const jaccardValue = terms.jaccard(stringA, stringB);
console.log(jaccardValue); // 0.5

// 计算重叠系数
const overlapValue = terms.overlap(stringA, stringB, 100);
console.log(overlapValue); // 0.7

// 包含
const containsResult = terms.contains(stringC, 'to');
console.log(containsResult); // true

// 相等
const equalsResult = terms.equals(stringA, stringB);
console.log(equalsResult); // false