psm 各种匹配方法的代码
时间: 2023-09-01 18:01:37 浏览: 286
PSM(Probabilistic Sequence Matching)是一种常用于序列匹配的概率模型,可以用于文本相似度计算、信息抽取、语音识别等领域。下面是几种PSM的匹配方法的代码示例:
1. Hamming Distance(汉明距离)匹配方法:
```python
def hamming_distance(str1, str2):
distance = 0
for i in range(len(str1)):
if str1[i] != str2[i]:
distance += 1
return distance
str1 = "hello"
str2 = "hallo"
distance = hamming_distance(str1, str2)
print("Hamming Distance:", distance)
```
2. Levenshtein Distance(编辑距离)匹配方法:
```python
def levenshtein_distance(str1, str2):
m = len(str1) + 1
n = len(str2) + 1
dp = [[0] * n for _ in range(m)]
for i in range(m):
dp[i][0] = i
for j in range(n):
dp[0][j] = j
for i in range(1, m):
for j in range(1, n):
cost = 0 if str1[i-1] == str2[j-1] else 1
dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + cost)
return dp[m-1][n-1]
str1 = "kitten"
str2 = "sitting"
distance = levenshtein_distance(str1, str2)
print("Levenshtein Distance:", distance)
```
3. Jaccard Similarity(杰卡德相似度)匹配方法:
```python
def jaccard_similarity(set1, set2):
intersection = len(set1.intersection(set2))
union = len(set1.union(set2))
similarity = intersection / union
return similarity
set1 = {'apple', 'banana', 'orange'}
set2 = {'banana', 'grape', 'mango'}
similarity = jaccard_similarity(set1, set2)
print("Jaccard Similarity:", similarity)
```
这些是PSM常用的几种匹配方法的代码示例,根据具体的匹配需求选择适合的方法进行使用。
阅读全文