Python3将相似字符串分组在一起

2024-05-16 01:09:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做的是把一个小说网站上的字符串组合在一起。这些帖子的标题通常采用如下格式:

titles = ['Series Name: Part 1 - This is the chapter name',
    '[OC] Series Name - Part 2 - Another name with the word chapter and extra oc at the start',
    "[OC] Series Name = part 3 = punctuation could be not matching, so we can't always trust common substrings",
    '{OC} Another cool story - Part I - This is the chapter name',
    '{OC} another cool story: part II: another post title',
    '{OC} another cool story part III but the author forgot delimiters',
    "this is a one-off story, so it doesn't have any friends"]

分隔符等并不总是存在,可能存在一些变化

我首先将字符串规范化为字母数字字符

import re
from pprint import pprint as pp

titles = []  # from above

normalized = []
for title in titles:
    title = re.sub(r'\bOC\b', '', title)
    title = re.sub(r'[^a-zA-Z0-9\']+', ' ', title)
    title = title.strip()
    normalized.append(title)

pp(normalized)

   ['Series Name Part 1 This is the chapter name',
 'Series Name Part 2 Another name with the word chapter and extra oc at the start',
 "Series Name part 3 punctuation could be not matching so we can't always trust common substrings",
 'Another cool story Part I This is the chapter name',
 'another cool story part II another post title',
 'another cool story part III but the author forgot delimiters',
 "this is a one off story so it doesn't have any friends"]

我希望的结果是:

['Series Name', 
'Another cool story', 
"this is a one-off story, so it doesn't have any friends"]  # last element optional

我知道一些比较字符串的不同方法

difflib.SequenceMatcher.ratio()

Levenshtein edit distance

我也听说过雅罗·温克勒和模糊模糊模糊

但真正重要的是,我们可以得到一个数字,显示字符串之间的相似性

我想我需要拿出(大部分)一个2D矩阵来比较每个字符串。但一旦我有了这些,我就无法思考如何将他们真正地分成小组

我发现another post似乎已经完成了第一部分。。。但我不知道如何继续下去

scipy.cluster起初看起来很有希望。。。但后来我被挡在了头上

另一个想法是以某种方式将itertools.combinations()functools.reduce()与上述距离度量之一结合起来

我是不是想得太多了?看起来这应该很简单,但在我的脑海里却没有任何意义


Tags: the字符串nametitleisanotherserieschapter
2条回答

您的任务属于所谓的semantic similarity。我建议你采取以下行动:

  1. 通过Glove/Word2vec或流行的BERT获取字符串的映射。这将为您提供标题的数字表示
  2. 然后从scikit的k-means开始执行聚类,然后可以使用高级聚类方法

这是CKM答复中提出的想法的一个实现:https://stackoverflow.com/a/61671971/42346

首先去掉标点符号,使用以下答案对你的目的不重要:https://stackoverflow.com/a/15555162/42346

然后我们将使用这里描述的技术之一:https://blog.eduonix.com/artificial-intelligence/clustering-similar-sentences-together-using-machine-learning/对类似的句子进行聚类

from nltk.tokenize import RegexpTokenizer

tokenizer = RegexpTokenizer(r'\w+') # only alphanumeric characters

lol_tokenized = []
for title in titles:
    lol_tokenized.append(tokenizer.tokenize(title))

然后获取标题的数字表示形式:

import numpy as np 
from gensim.models import Word2Vec

m = Word2Vec(lol_tokenized,size=50,min_count=1,cbow_mean=1)  
def vectorizer(sent,m): 
    vec = [] 
    numw = 0 
    for w in sent: 
        try: 
            if numw == 0: 
                vec = m[w] 
            else: 
                vec = np.add(vec, m[w]) 
            numw += 1 
        except Exception as e: 
            print(e) 
    return np.asarray(vec) / numw 

l = []
for i in lol_tokenized:
    l.append(vectorizer(i,m))

X = np.array(l)

哇,真是太多了。
现在您必须进行集群

from sklearn.cluster import KMeans

clf = KMeans(n_clusters=2,init='k-means++',n_init=100,random_state=0)
labels = clf.fit_predict(X)
print(labels)
for index, sentence in enumerate(lol_tokenized):
    print(str(labels[index]) + ":" + str(sentence))

[1 1 0 1 0 0 0]
1:['Series', 'Name', 'Part', '1', 'This', 'is', 'the', 'chapter', 'name']
1:['OC', 'Series', 'Name', 'Part', '2', 'Another', 'name', 'with', 'the', 'word', 'chapter', 'and', 'extra', 'oc', 'at', 'the', 'start']
0:['OC', 'Series', 'Name', 'part', '3', 'punctuation', 'could', 'be', 'not', 'matching', 'so', 'we', 'can', 't', 'always', 'trust', 'common', 'substrings']
1:['OC', 'Another', 'cool', 'story', 'Part', 'I', 'This', 'is', 'the', 'chapter', 'name']
0:['OC', 'another', 'cool', 'story', 'part', 'II', 'another', 'post', 'title']
0:['OC', 'another', 'cool', 'story', 'part', 'III', 'but', 'the', 'author', 'forgot', 'delimiters']
0:['this', 'is', 'a', 'one', 'off', 'story', 'so', 'it', 'doesn', 't', 'have', 'any', 'friends']

然后您可以拉出索引=1的值:

for index, sentence in enumerate(lol_tokenized): 
    if labels[index] == 1: 
        print(sentence) 

['Series', 'Name', 'Part', '1', 'This', 'is', 'the', 'chapter', 'name']
['OC', 'Series', 'Name', 'Part', '2', 'Another', 'name', 'with', 'the', 'word', 'chapter', 'and', 'extra', 'oc', 'at', 'the', 'start']
['OC', 'Another', 'cool', 'story', 'Part', 'I', 'This', 'is', 'the', 'chapter', 'name']

相关问题 更多 >