Twitter4j gather only a certain country - api

for a project I am currently working on I need to gather tweets from a stream only for one country.
Although the Twitter4j streaming API allows to filter by language the results aren't accurate enough. So I thought to put a filter on top of the filter by checking if the country attribute of the tweet is filled. This works fine when I check if theres a value at all:
if(status.getPlace().getCountry() != null) {
System.out.println("User: " + status.getUser().getName());
System.out.println("Text: : " + status.getText());
System.out.println("Country: " + status.getPlace().getCountry());
System.out.println("Language: " + status.getLang());
}
}
TwitterStream ts = new TwitterStreamFactory(cb.build()).getInstance();
ts.addListener(listener);
FilterQuery filter = new FilterQuery();
String[] language = {"country"};
String[] keywords = {"some keywords"};
filter.track(keywords);
filter.language(language);
ts.filter(filter);
But if I check for a certain country e.g. germany I don't receive any tweets:
if(status.getPlace().getCountry() != "germany") {
System.out.println("User: " + status.getUser().getName());
System.out.println("Text: : " + status.getText());
System.out.println("Country: " + status.getPlace().getCountry());
System.out.println("Language: " + status.getLang());
}
}
It would be great if there's someone who can help me with this.

filter.track(keywords);
filter.language(language);
Keep in mind that the above code means track(keywords) OR language(language).
This is not logical AND.
If you want tweets from only one country, and with certain keywords, then remove filter.language(language) and check the country after you get status.
if(status.getPlace().getCountry().equalsIgnoreCase("germany")) {}
// String comparison
You won't get a tweet from Germany if nobody from Germany tweets with the filters you have specified.

Related

Unable to locate appropriate constructor on class [ClassName]

I want to send a sql query by using Spring JPA like :
"SELECT NEW com.blalba.model.service.FamilyMaterialDto "
+ "(ms.id, mi.partNumber, ftc.commodityType, ftc.materialType, ms.grade, ms.thickness, ms.width) "
+ "FROM MaterialInstance mi, FamilyTypeCommodity ftc, MaterialSpecification ms "
+ "WHERE ftc.materialFamily.id = :familyId "
+ "AND (:typeId is null OR ftc.materialType.id = :typeId) "
+ "AND ftc.id = ms.familyTypeCommodity.id "
+ "AND ms.id = mi.materialSpecification.id "
+ "AND mi.materialSpecification.isActive = true"
However, when I remove some fields like "ms.width", I get the error:
Unable to locate appropriate constructor on class [com.commencis.sova.model.service.FamilyMaterialDto]. Expected arguments are: java.lang.String, java.lang.String, com.commencis.sova.model.entity.material.CommodityType, com.commencis.sova.model.entity.material.MaterialType, com.commencis.sova.model.entity.material.Grade, com.commencis.sova.model.entity.material.Thickness [SELECT NEW com.commencis.sova.model.service.FamilyMaterialDto (ms.id, mi.partNumber, ftc.commodityType, ftc.materialType, ms.grade, ms.thickness) FROM com.commencis.sova.model.entity.material.MaterialSpecification ms, com.commencis.sova.model.entity.material.MaterialInstance mi, com.commencis.sova.model.entity.material.FamilyTypeCommodity ftc WHERE ftc.materialFamily.id = :familyId AND (:typeId is null OR ftc.materialType.id = :typeId) AND mi.materialSpecification.isActive = true AND ms.id = mi.materialSpecification.id AND ftc.id = ms.familyTypeCommodity.id]
I understand that return Object[] cannot be parsed to DTO object. If I write constructor without the parameter - "Width", it will work properly. However, I want to provide that a query can be sendable without some parameters(sometimes one of them, sometimes five of them) and a result can be parsable with FamilyMaterialDTO.
How can I do? I don't have to use DTO, if there is another solution for this problem, please recommend.
I think you can make the query to return a map and you can create a constructor for FamilyMaterialDTO which takes the argument as a map. And based on the keys present in the map you can set the values...
For simplicity let me create my own class.
Class foo { String a; Integer b; Boolean c;}
And a sample query
"select new map (a as a, b as b) from Foo f"
Now this query will return a list of maps and the size of the list depends on how many rows the query returns.
Now you can create a constructor like this. Assume the size of list is 1.
foo (List<Map<?,?> list) {
Map map = list.get(0);
If(map.containsKey("a")) this.a = map.get("a");
If(map.containsKey("b")) this.b = map.get("b");
If(map.containsKey("c")) this.c = map.get("c"); }

multiple search in domino documents

In the onclick event of a button , I would like to search for a notes document , with multiple conditions with ssjs.
I have a form with a few fields. Now I would like to find a notes document where field a="123" and field b="456" field c="789" and field d >"A123456" , and then I would like to read the contents of field e.
If it was the search in a view I would use something like :
var tmpArray = new Array("");
var cTerms = 0;
if(viewScope.fong != null & viewScope.fong != "") {
tmpArray[cTerms++] = "(FIELD Site = \"" + viewScope.fong + "\")"
}
if(#Text(viewScope.sDate) != null & #Text(viewScope.sDate) != "") {
tmpArray[cTerms++] = "(FIELD StartDate = \"" + #Text(viewScope.sDate) + "\")"
}
qstring = tmpArray.join(" AND ").trim();
viewScope.queryString = qstring;
return qstring
If I only had 1 condition I would have used #DbLookup (and still how select documents >"A123456"?)
What's the best way of dooing this in ssjs ?
UPDATE
tried with FTSearch , but it seems in the searchkey "FIELD d > A123456" , doesn't seem to work
OTHER UPDATE
var dc = db.FTSearch("FIELD a=123 and FIELD b =456 and FIELD d =A123456");
seems to work but
var dc = db.FTSearch("FIELD a=123 and FIELD b =456 and FIELD d >A123456"); doesn't. It gives error : Exception occurred calling method NotesDatabase.FTSearch(string) null
If you want to use comparison operators > and <, then you need to use the NotesDatabase.Search method instead of FTSearch. Search is slower and can't access data in non-summary (i.e., rich text) fields, but it has all the same capabilities that you can use in a view selection formula.

Spring data repository query to retrieve values containing null

I have following User table and repository.
User:
id;name;job;age
1;steve;nurse;33
2;steve;programmer;null
3;steve;programmer;null
Repository method:
#Query("SELECT u FROM User u WHERE ("
+ "LOWER(u.name) = LOWER(:name) AND "
+ "LOWER(u.beruf) = LOWER(:job) AND "
+ "LOWER(u.alter) = LOWER(:age))")
public List<User> findUsers(#Param("name") String name,
#Param("job") String job,
#Param("age") String age);
If I call the repository method with following parameters
String name = "steve";
String job = "programmer";
List<User> result = repository.findUsers(name, job, null); // empy list ..why ?
I get an empty list as result, although I expect to get the entities with id=2 and id=3 as result.
What am I doing wrong ? How should I change the query to get the two entities as result.
Thanks
According to the documentation this behaviour is normal there is no way to ignore null fields. using #Query method.
instead you can use the query method specifications.
more information [here][jpa documentaiton]
if you want to keep your existing method you can also go like this:
#Query("SELECT u FROM User u WHERE ("
+ "LOWER(u.name) = LOWER(:name) AND "
+ "LOWER(u.beruf) = LOWER(:job) AND "
+ "( " +
" :age is null or LOWER(u.alter) = LOWER(:age) " +
")"
)
public List<User> findUsers(#Param("name") String name,
#Param("job") String job,
#Param("age") String age);

Extracting Fields Value using Lucene

My problem is that I would like to parse one document only (not multiple documents) with textual data and extract some relevant information based on my query.
For example:
If I have the following text:
This is a sample document.
Name: Te
Age: 25
Email: te#gmail.com
Some text in the end of the document
I would like to extract the fields (Name, Age, Email) with there corresponding values
Many of the examples I found are mainly to search for documents that matches a query. I would appreciate if someone can guide me on which Analyzer or Query classes to lookin in lucene library or any materials to read.
This should get you started. With a regular expression, in Java, where the document content has been assigned to the variable text:
String expr = "Name\:\s(\w+)\sAge\:\s+(\d+)\s+Email\:\s+([a-z0-9.#]+)\s+";
Pattern r = Pattern.compile(expr, Pattern.CASE_INSENSITIVE);
Matcher m = r.matcher(text);
if (m.find( ))
{
System.out.println("Name: " + m.group(1) );
System.out.println("Age: " + m.group(2) );
System.out.println("Email: " + m.group(3) );
}
else { System.out.println("Match not found"); }

Tree search with Lucene

I have a taxonomy index that describes a tree structure. When performing a query I want to get the number of hits for multiple categories (not necessarily in the same level of the tree). For example, given the following list of paths:
[Root/Cat1, Root/Cat1/Cat12, Root/Cat3]
I want to obtain the number of hits for each of these three categories.
I've been looking for a solution and I know that is possible to make a tree request and then get the results by calling .getSubResults() (as it is explained in the API). However I haven't found any example and I don't really know how to implement it. So far I've got to the following:
// Build query
Query query = extendQuery(queryGenerator.generateQuery(resource));
// Set the number of top results
TopScoreDocCollector tdc = TopScoreDocCollector.create(numTopDocuments, true);
// Set a faceted search
FacetSearchParams facetSearchParams = new FacetSearchParams();
// Search at level of the category in interests
CountFacetRequest facetRequest = new CountFacetRequest(new CategoryPath("Top", '/'), numTopCategories);
facetRequest.setResultMode(ResultMode.PER_NODE_IN_TREE);
facetSearchParams.addFacetRequest(facetRequest);
// To collect the number of hits per facet
FacetsCollector facetsCollector =
new FacetsCollector(facetSearchParams, documentReader, taxonomyReader);
try {
// Collect the number of hits per facet
documentSearcher
.search(query, MultiCollector.wrap(tdc, facetsCollector));
for (FacetResult res : facetsCollector.getFacetResults()){
//this is the top lvl facet
FacetResultNode toplvl = res.getFacetResultNode();
System.out.println(toplvl.getLabel() + " (" + toplvl.getValue() + ")");
for (FacetResultNode secondlvl : toplvl.getSubResults()) {
//second lvl facet categories
System.out.println(" " + secondlvl.getLabel().getComponent(1)
+ " (" + secondlvl.getValue() + ")");
for (FacetResultNode thirdlvl : secondlvl.getSubResults()) {
//second lvl facet categories
System.out.println(" " + thirdlvl.getLabel().getComponent(2)
+ " (" + thirdlvl.getValue() + ")");
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However, when I get to the third level I get null. What is going on?
Thanks.
You have to set also:
facetRequest.setDepth(MAX_DEPTH_TREE);