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. 10 2036
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']
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] = ...
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
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
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
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
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
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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>
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |