I’m looping through Reddit’s API to get comments from multiple submissions. The result looks like this:
https://ibb.co/FDLJWjF
Now I'm trying to combine all the comments (outputs) together into one string. Can't find any documentation on this... Any ideas???
Related
I need to create a custom field lookup in Django that is similar to startswith.
The built in startswith lookup works like this:
id__startswith='abc'
So if the ID field is 'abc1' then it would match.
However I need create a 'contained from start' (cfs) lookup that looks this:
id__cfs='abc12'
So if the ID field is 'abc' then it would match.
Basically __startswith but the other way around.
How should I implement this?
It turns out that creating this custom lookup would be inefficient. For example, if the custom query looked like this:
id__custom='abc12'
Then the sql query would look like this:
WHERE 'abc12' LIKE id||%
However, this LIKE query would be very slow because it can't use any indexes. Every single record would have to be compared to 'abc12'. So the best way to achieve the same goal is to divide the string into substrings and store them in a list. Then you can search for those using the __in lookup.
For instance, if you wanted to find matches that contained at least half of the string you could create three substrings and search for those. By using the __in lookup your query would be much faster because you could use existing indexes. The result would look like this:
substrings = ['abc', 'abc1', 'abc12']
query = Item.objects.filter(id__in=substrings).values('id', 'manufacturer')
I have two TopDocs objects. They both contain the same results but one is ordered by relevance and the other is weighted by date. I want to alternate between showing a relevant result and showing a recent result.
I can't think of a way to do this which doesn't involve iterating over every single result. Does anyone have any ideas?
Thanks,
Joe
Set<ScoreDoc> set = new HashSet<ScoreDoc>();
set.addAll(Arrays.asList(firstScoreDoc));
set.addAll(Arrays.asList(secondScoreDoc));
Something like this?
I want to query DBpedia for multiple keywords, when I query freebase for example using this:
http://api.freebase.com/api/service/search?query=%2BEgypt%2BPyramids
I get reasonable results like: "Egyptian Pyramids", "Ancient Egypt", "Pyramids of Giza (Egypt)".
However whenever I try to query dbpedia with multiple keywords I get an empty ArrayofResult, although when I query for each keyword on its own I do get results..
I couldn't find any documentation for DBpedia's Keyword search service, only this: http://dbpedia.org/lookup
I write the query like this:
http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=place&QueryString=Egypt+pyramid
Is this the right way of doing it?
Don't limit the results with place class, it seems doesn't work
http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=&QueryString=Egyptian%20Pyramids
I am having some trouble understanding CSqlDataProvider and how it works.
When I am using CActiveDataProvider, the results can be accessed as follows:
$data->userProfile['first_name'];
However, when I use CSqlDataProvider, I understand that the results are returned as an array not an object. However, the structure of the array is flat. In other words, I am seeing the following array:
$data['first_name']
instead of
$data['userProfile']['first_name']
But the problem here is what if I have another joined table (let's call it 'author') in my sql code that also contains a first_name field? With CActiveDataProvider, the two fields are disambiguated, so I can do the following to access the two fields:
$data->userProfile['first_name'];
$data->author['first_name'];
But with CSqlDataProvider, there doesn't seem to be anyway I can access the data as follows:
$data['userProfile']['first_name'];
$data['author']['first_name'];
So, outside of assigning a unique name to those fields directly inside my SQL, by doing something like this:
select author.first_name as author_first_name, userProfile.first_name as user_first_name
And then referring to them like this:
$data['author_first_name'];
$data['user_first_name']
is there anyway to get CSqlDataProvider to automatically structure the arrays so they are nested in the same way that CActiveDataProvider objects are? So that I can call them by using $data['userProfile']['first_name']
Or is there another class I should be using to obtain these kinds of nested arrays?
Many thanks!
As far as I can tell, no Yii DB methods break out JOIN query results in to 2D arrays like you are looking for. I think you will need to - as you suggest - alias the column names in your select statement.
MySql returns a single row of data when you JOIN tables in a query, and CSqlDataProvider returns exactly what MySql does: single tabular array representation indexed/keyed by the column names, just like your query returns.
If you want to break apart your results into a multi-dimensional array I would either alias the columns, or use a regular CActiveDataProvider (which you can still pass complex queries and joins in via CDbCritiera).
I want to show the closest related item for a product. So say I am showing a product and the style number is SG-sfs35s. Is there a way to select whatever product's style number is closest to that?
Thanks.
EDIT: to answer your questions. Well I definitely want to keep the first 2 letters as that is the manufacturer code but as for the part after the first dash, just whatever matches closest. so for example SG-sfs35s would match SG-shs35s much more than SG-sht64s. I hope this makes sense whenever I do LIKE product_style_number it only pulls the exact match.
There normally isn't a simple way to match product codes that are roughly similar.
A more SQL friendly solution is to create a new table that maps each product to all the products it is similar to.
This table would either need to be maintained manually, or a more sophisticated script can be executed periodically to update it.
If your product codes follow a consistent pattern (all the letters are the same for similar products, with only the numbers changing), then you should be able to use a regular expression to match the similar items. There are docs on this here...
It sounds like what you want is levenshtein distance .
Unfortunately, there isn't a built-in levenshtein function for mysql, but some folks have come up with a user-defined function that does it(deadlink).
You will probably want to do it as a stored procedure, as I expect that the algorithm may not be trivial.
For example, you may split the term at the -, so you have two parts. You do a LIKE query on each part and use that to make a decision.
You could just loop though, replacing the last character with "%" until you get at least one result, in your stored procedure.
Sounds like you need something like Lucene, though i'm not sure if that would be overkill for your situation. But it certainly would be able to do text searches and return the ones most similar first.
If you need something more simple I would try to start by searching with the full product code, then if that doesn't work try to use wildcards/remove some characters until you return a result.
JD Isaacks.
This situation of yours is very simple to solve.
It`s not like you need to use Artificial Intelligence like the Google.
http://www.w3schools.com/sql/sql_wildcards.asp
Take a look at this manual at w3schools about wildcards to use with your SELECT code.
But also you will need to create a new table with 3 columns: LeftCode, RightCode and WildCard.
Example:
Rows on Table:
LeftCode = SG | RightCode = 35s | WildCard = SG-s_s35s
LeftCode = SG | RightCode = 64s | WildCard = SG-s_t64s
SQL Code
If the user typed the code that matches the row1 of the table:
SELECT * FROM PRODUCTS WHERE CODE LIKE "$WildCard";
Where $WildCard is the PHP variable containing the column 3 of the new table.
I hope I helped, even 4 years late...