473,657 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string difference and similarity

I've got a series of data like this:

Long Sleeve White P/C Sm 32/33
Long Sleeve White P/C Med 32/33
....

What I'd like to do is extract the differences and the similarity. In
this case:

similar: Long Sleeve White P/C

difference: Med 32/33
If I were writing a function, I'd probably compare increasingly
longer substrings, but I'm thinking that php probably already has
functions for that. What is that?

I found "xdiff_string_d iff", but I don't really understand it or how I
would get the common text.

Jeff
Nov 13 '08 #1
3 6385

"Jeff" <jeff@spam_me_n ot.comwrote in message
news:Qd******** *************** *******@earthli nk.com...
I've got a series of data like this:

Long Sleeve White P/C Sm 32/33
Long Sleeve White P/C Med 32/33
...

What I'd like to do is extract the differences and the similarity. In
this case:

similar: Long Sleeve White P/C

difference: Med 32/33
If I were writing a function, I'd probably compare increasingly longer
substrings, but I'm thinking that php probably already has functions for
that. What is that?

I found "xdiff_string_d iff", but I don't really understand it or how I
would get the common text.
I haven't looked into that formula yet. One way to think about it as an
alternative would be to turn the strings into arrays and use either
array_intersect , array_diff, or loop through one of the arrays checking to
see if that value is in_array of the second. I'm not sure how your strings
are created, so it's hard to tell what would be appropriate...s ince:

I'm a crochety old man.

is different than

Old man, I'm crochety.

and not just by two characters. :^)
Nov 13 '08 #2
On Thu, 13 Nov 2008 01:39:42 -0500, jeff@spam_me_no t.com wrote:
I've got a series of data like this:

Long Sleeve White P/C Sm 32/33
Long Sleeve White P/C Med 32/33
...

What I'd like to do is extract the differences and the similarity. In
this case:

similar: Long Sleeve White P/C

difference: Med 32/33
If I were writing a function, I'd probably compare increasingly
longer substrings, but I'm thinking that php probably already has
functions for that. What is that?

I found "xdiff_string_d iff", but I don't really understand it or how I
would get the common text.

Jeff
If your string comparison needs are all as simple as your example,
using strspn() could probably suit your needs. Perhaps something
like:

<?php
$s1 = 'Long Sleeve White P/C Sm 32/33';
$s2 = 'Long Sleeve White P/C Med 32/33';

$matchlen = strspn($s1, $s2);

// remove 1st non-matching char
$same = substr($s1, 0, $matchlen - 1);

// include 1st non-matching char
$diff = substr($s2, $matchlen - 1);

printf("Same: [%s]\nDiff: [%s]", $same, $diff);
?>

strspn() will give us the length of the initial matching segment in
$s1. When writing a function, I'd check to see if the strings are
equal first, and preemptively return the string or whatever suits
your needs.

If you need a more complex algorithm, see the manual:

<URL:http://php.net/manual/en/function.levens htein.php>
--
Curtis
$email = str_replace('si g.invalid', 'gmail.com', $from);
Nov 13 '08 #3
Jeff escribió:
I've got a series of data like this:

Long Sleeve White P/C Sm 32/33
Long Sleeve White P/C Med 32/33
...

What I'd like to do is extract the differences and the similarity. In
this case:

similar: Long Sleeve White P/C

difference: Med 32/33
The documentation looks slightly scarce but this package features inline
diffs:

http://pear.php.net/package/Text_Diff


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Nov 13 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
6296
by: Bosconian | last post by:
I'm looking for a method to compare two strings and grade them for similarity. My idea is to strip out common words and punctuation and create a checksum of each remaining string. I would then compare the checksums and if they are close then there's a potential match (to be judged by user interaction.) Can someone suggest an existing function or class to perform such a task? Thanks!
5
7994
by: Achim Domma | last post by:
Hi, I have a list of lets say 100-1000 strings and want to know which one is most similar to a reference string. Does somebody know such a library for Python? I don't need complicated scientific stuff, I think the most simple ones will do it for my data. regards, Achim
21
2424
by: Chris S. | last post by:
I have a number of strings, containing wildcards (e.g. 'abc#e#' where # is anything), which I want to match with a test string (e.g 'abcdef'). What would be the best way for me to store my strings so lookup is as fast as possible?
3
541
by: John Harman | last post by:
Hi, I'm trying to do a MySQL Query using Mysql 3.23.58 something like that below SELECT name FROM customers WHERE name LIKE "Fred" ORDER BY difference(name,"Fred"); The difference piece doesn't seem to be working (Syntax Error returned). Is this not possible without upgrading the server? or am I doing something wrong, all I want to do is be able to return the results closest to that
0
1552
by: Anibal Acosta | last post by:
Somebody know an algorithm for determine the similarity between two string for example: string 1: "Hello what is your name?, where are you from?" string 2: "Hello man, where are you from?" In this case I think that this two string has 75% of similarity. However, if you know an algorithm that do that I'll appreciate very very
4
3483
by: almurph | last post by:
Hi, Hope you can help me with this one. I'm looking for some nice string comparison algorithms. I want to be able to compare 2 strings (fairly smallish, less than 50 characters) and return a % of how well they are similar. So, 2 strings that are absoloutly identical will return 100%. Strings that are radically different will return numbers near 0%:-
9
7255
by: Rajarshi | last post by:
Hi, I have some code that takes a string and obtains a compressed version using zlib.compress Does anybody know how I can remove the header portion of the compressed bytes, such that I only have the compressed data remaining? (Obviously I do not intend to perform the decompression!) Thanks,
6
2634
by: Muhs | last post by:
Hi !! i want to compare two strings and return the difference For example, i have two strings, string str1="then"; string str2="than"; it compared the two strings and returns 1, as only one character is different (a and e). Is there a function in c# that can do this??
6
4334
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the computation 2) compare the string binary data to get the similarity or dissimilarity result. The problem is, i already done with the image (jpg) conversion to binary and also try the algorithm structure in C# language, but i having a problem to...
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.