473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

best way to align words?

Hello,

i would like to write a piece of code to help me to align some sequence
of words and suggest me the ordered common subwords of them

s0 = "this is an example of a thing i would like to have".split()
s1 = "another example of something else i would like to have".split()
s2 = 'and this is another " example " but of something ; now i would
still like to have'.split()
....
alist = (s0, s1, s2)

result should be : ('example', 'of', 'i', 'would', 'like', 'to', 'have'

but i do not know how should i start, may be have you a helpful
suggestion?
a trouble i have if when having many different strings my results tend
to be nothing while i still would like to have one of the, or maybe,
all the best matches.

best.

Nov 30 '06 #1
10 2161
Robert R. schrieb:
Hello,

i would like to write a piece of code to help me to align some sequence
of words and suggest me the ordered common subwords of them

s0 = "this is an example of a thing i would like to have".split()
s1 = "another example of something else i would like to have".split()
s2 = 'and this is another " example " but of something ; now i would
still like to have'.split()
...
alist = (s0, s1, s2)

result should be : ('example', 'of', 'i', 'would', 'like', 'to', 'have'

but i do not know how should i start, may be have you a helpful
suggestion?
a trouble i have if when having many different strings my results tend
to be nothing while i still would like to have one of the, or maybe,
all the best matches.

best.
As far as I can see, you want to have the words, that all three lists
have in common, right?

s0 = "this is an example of a thing i would like to have".split()
s1 = "another example of something else i would like to have".split()
s2 = 'and this is another " example " but of something ; now i would
still like to have'.split()

def findCommons(s0, s1, s2):
res = []
for word in s0:
if word in s1 and word in s2:
res.append(word)
return res
>>>print findCommons(s0,s1,s2)
['example', 'of', 'i', 'would', 'like', 'to', 'have']
Nov 30 '06 #2
Robert R. wrote:
Hello,

i would like to write a piece of code to help me to align some sequence
of words and suggest me the ordered common subwords of them

a trouble i have if when having many different strings my results tend
to be nothing while i still would like to have one of the, or maybe,
all the best matches.
"align"?
Anyway, for finding the commonest words, you'll be best off
counting how many times each word appears:

lst = ["foo bar baz", "qux foo foo kaka", "one foo and kaka
times qux"]

for line in lst:
for word in line.split():
count[word] = count.get(word,0) + 1

Now you go for the ones with the highest count:

for (word, n) in sorted(d.items(), key = lambda x: x[1],
reverse = True):
print word, 'appears', n, 'times'

Untested. If you want to count the number of lines a word
appears in (as opposed to the number of times it appears at
all), add an extra condition before count[word] = ...
Nov 30 '06 #3
i would like to write a piece of code to help me to align some sequence
of words and suggest me the ordered common subwords of them
Im not sure what you want, but in case you are guy who knows how
quicksort and Djikstra algorithms work :) and wants to find out more.

There are many algorithms out there, discovered on "Text algorithms"
univesity course. The first one does not directly solve your problem -
"edit distance" (Levenshtein distance)
http://en.wikipedia.org/wiki/Levenshtein_distance
I mention it here only because it is simple and shows basic idea of
Dynamic Programming
http://en.wikipedia.org/wiki/Dynamic_programming

If you scroll down you'll see "Longest common subsequence problem" with
implementation in Python for 2 sequences. If you dont understand how it
works just look into "edit distance" idea and see it is exactly the
same algorithm with changed rules.

Oleg

Nov 30 '06 #4
Robert R.:
i would like to write a piece of code to help me to align some sequence
of words and suggest me the ordered common subwords of them [...]
a trouble i have if when having many different strings my results tend
to be nothing while i still would like to have one of the, or maybe,
all the best matches.
This is my first solution try, surely there are faster, shorter, better
solutions...
from collections import defaultdict
from itertools import chain
from graph import Graph
# http://sourceforge.net/projects/pynetwork/

def commonOrdered(*strings):
lists = [[w for w in string.lower().split() if w.isalpha()] for
string in strings]

freqs = defaultdict(int)
for w in chain(*lists):
freqs[w] += 1

g = Graph()
for words in lists:
g.addPath(words)

len_strings = len(strings)
return [w for w in g.toposort() if freqs[w]==len_strings]
s0 = "this is an example of a thing i would like to have"
s1 = "another example of something else i would like to have"
s2 = 'and this is another " example " but of something ; now i would
still like to have'

print commonOrdered(s0, s1, s2)

It creates a graph with the paths of words, then sorts the graph
topologically, then takes only the words of the sorting that are
present in all the original strings.
With a bit of work the code can be used if it contains words like
"example" instead of " example ".
An xtoposort method too can be added to the Graph class...

Bye,
bearophile

Nov 30 '06 #5
This is my first solution try, surely there are faster, shorter, better
solutions...
It creates a graph with the paths of words, then sorts the graph
topologically,
Beside possible inefficiencies, this "solution" breaks if words aren't
in the correct order, the topological sort can't work...
I'll have to think about better solutions, if possible.

Sorry,
bye,
bearophile

Dec 1 '06 #6
Robert R. wrote:
Hello,

i would like to write a piece of code to help me to align some sequence
of words and suggest me the ordered common subwords of them

s0 = "this is an example of a thing i would like to have".split()
s1 = "another example of something else i would like to have".split()
s2 = 'and this is another " example " but of something ; now i would
still like to have'.split()
...
alist = (s0, s1, s2)

result should be : ('example', 'of', 'i', 'would', 'like', 'to', 'have'

but i do not know how should i start, may be have you a helpful
suggestion?
a trouble i have if when having many different strings my results tend
to be nothing while i still would like to have one of the, or maybe,
all the best matches.

best.
Your requirements are a little vague... how are these three strings handled?

s1 = "hello there dudes"
s2 = "dudes hello there"
s3 = "there dudes hello"

they all share the 3 words, but what order do you want them back?

here is a simplistic approach using sets that results in a list of words
that are in all strings ordered arbitrarily by their order in the first
string ( it also doesn't worry about matches (or lack of) due to
punctuation and case and crap like that)
>>strList = []
strList.append('this is an example of a thing i would like to have')
strList.append('another example of something else i would like to
have')
>>strList.append('and this is another " example " but of something ;
now i would still like to have')
>>[word for word in strList[0].split() if word in reduce(lambda x, y:
x.intersection(y), [set(str.split()) for str in strList])]
['example', 'of', 'i', 'would', 'like', 'to', 'have']

but you still have issues with mutiple matches and how they are handled
etc...

noah
Dec 1 '06 #7
Noah Rawlins wrote:
>
>>strList = []
>>strList.append('this is an example of a thing i would like to have')
>>strList.append('another example of something else i would like to
have')
>>strList.append('and this is another " example " but of something ;
now i would still like to have')
>>[word for word in strList[0].split() if word in reduce(lambda x, y:
x.intersection(y), [set(str.split()) for str in strList])]
['example', 'of', 'i', 'would', 'like', 'to', 'have']
I think that ends up doing the set reduction over and over for every
word in the first string, so you probably want to move that outside the
list comprehension

noah
Dec 1 '06 #8

Hello,

thanks for all your replies, i'm now looking to dynamic programming...

sorry for forgetting to say that i wanted the words to be ordered, thus
:

s1 = "hello there dudes"
s2 = "dudes hello there"
s3 = "there dudes hello"

will not return anything while sharing all three words.

Bearophile your solution with graph looks interesting although i still
do not understand how it works, but yes there is definitively something
with drawing path around words.

i have tried SequenceMatcher from difflib after using combinations of
all sentences as i need to process much more than the 3 of my first
example.

best.

Dec 2 '06 #9
thanks for all your replies, i'm now looking to dynamic programming...
Id better warn you before you go further.
"Notice that LCS is often defined to be finding all common
subsequences of a maximum length. This problem inherently has higher
complexity, as the number of such subsequences is exponential in the
worst case"

This means that if you have 10 sentences with 5 words in each there is
5^10 space and time complexity. Definitelly, there are better
algorithms from dynamic programming, but you should review your needs:
how many sentences, words you have.

There can be easier way than dynamic programming.

Oleg

Dec 2 '06 #10

Oleg Batrashev a écrit :
This means that if you have 10 sentences with 5 words in each there is
5^10 space and time complexity. Definitelly, there are better
algorithms from dynamic programming, but you should review your needs:
how many sentences, words you have.
it can be few to many, actually it depends of the words i'm looking for.

Dec 3 '06 #11

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

Similar topics

5
by: Tony Vasquez | last post by:
What is the best way to center one, or more DIVs in the browser window? Can someone give me a quick code snipplette. I need something that will center the DIV, for all sorts of awkward screen...
28
by: A.Translator | last post by:
I am still struggling with an unordered list (http://www.xs4all.nl/~hogen/TaalVlinder/). The top navbar contains 4 divs with each an ul, and no padding or margins. But I get far too much...
6
by: TJ | last post by:
I've got a calendar that is based on the concept of lots of blocks that are spans with float:left. I would like to be able to have a detail section on the right side of the screen, so that when...
6
by: Viken Karaguesian | last post by:
Hello, Just wondering what the difference is between "float" and "align"? If I align a picture to the left, is that not the same as floating it? Can align and float be used together or do you...
4
by: Will Hartung | last post by:
The designers have handed me a page that has 5 different blocks on it in the center column (in a typical 3 column layout with page spanning headers and footers). The blocks have elaborate...
2
by: Newry | last post by:
Hi, I'm trying to position something with CSS, to have the equivalent of: <table> <tr> <td><img src="foo.jgp"></td> <td valign=bottom>Label</td> </tr> </table>
6
by: Dan V. | last post by:
What is the best/simplest way to have a large top image with let's say 14 thumbnails under it in 7 rows and to replace the top image with the larger one when a user clicks on a thumbnail? I would...
3
by: Russell | last post by:
Hey, ok i have numerous tables to search through for a 'site search'. some of the searchble fields have html embeded within so after some quick referencing, saw I can use the regExp function...
1
by: Muchach | last post by:
Hello, Ok so what I've got going on is a form that is populated by pulling info from database then using php do{} to create elements in form. I have a text box in each table row for the user to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.