Prolog lists (using stemming function) - list

I know this is a very simple question but I seem to be having some problems.
I am trying to stem a list of words using porter_stem but I am getting an error:
Out of local stack
This is my code:
stemming([],[]).
stemming([H|T], A) :-
stemming(T,Answer),
porter_stem(H,S),
append(Answer,S,A).
Basically the pseudocode for this is as follows:
for all items in list
stem item
add item to list2
return list2
Can anyone please point me in the right direction please?

Consider using maplist/3 or equivalent depending on your prolog implementation: something like maplist(porter_stem, List, Result). would suffice.
If you're interested in learning how to build a proper recursion, post a comment and I'll try to expand my answer :)

Related

Add an arrow inside a list (mathematica)

I have a list of variables {a,b,c,d} (the lists I have are much bigger, this is a small example). I want to make the following list: {a->1, b->1, c->1, d->1}. I tried {a,b,c,d}->1, but this does not work and I dont know how to search for the right keywords.
That is pretty easy in Mathematica:
Thread[{a, b, c} -> 1]
Now, that you know this one function Thread, go into the documentation center and read through the material. Then you can look at the very bottom of the doc-page and you find plenty of guides and related functions that give you hints on where to go from there.

autocompletion of parameter : list static fields without typing a ClassName::

How to make auto-completion list all (static) fields of a certain class that has appropriate variable-type when ctrl+space at a slot of parameter in a certain function?
Example
I tried to ctrl+space in the below code :-
(Code as text is here.)
Question: How to make it show E_1 E_2 E_3?
I don't mind another plugin if I really need one.
It currently works but only for enum :-
My workaround
In practice, to get smart clue, I have to type more (PrototypeList::) :-
Bounty Reason
Here is the result of the current answer (citizenmatt's):-
It is different, but still not show E_1 E_2 E_3.
Have you tried Smart Completion? This feature will only show completion items that are valid for the current context. I think it works in C++, too.
In fact, ReSharper does help you here. All of E_1, E_2 and E_3 are in the completion list, but not on the top of it - they are assigned lower scores because they need an additional qualifier. That said, looks like there is still an issue with scoring:
E_2 and E_3 are in the list too, but they are not shown alongside E_1. We'll investigate this (RSCPP-19501).

appending strings to lists in prolog

i'm a new on Prolog, and i'm already having some problems to understand, the thing
is, i was making a test on appending some strings introduced by console:
append_str([],Adder,Adder).
append_str([Head|Tail],Adder,Result):-
append_str(Tail,[Head|Adder],Result).
sread_str(String):-
read(String),
atom(String).
sinput:-
sinput_str([]).
sinput_str(Lista):-
sread_str(String),
sinput_str([String|List]).
sinput_str(List):-
append_str(List,[],Result),
nl,
display(Result),
nl.
And eventually always getting this output:
|-? sinput.
sinput.
hello.
hello.
world.
world.
9.
9.
'.'(hello,'.'(world,[]))
The number is just for the console to end asking for some more values, i don't know what is wrong, thank you guys in advance.
you should try to understand what happens when you enter a number, forcing sread_str to fail.
Prolog explores all the alternatives available to prove a given goal, and doing so change the variables, undoing such changes when a path fail. Such model it's complicated by side effects required by IO (the read/display builtins).
Then attempt first to have a working sinput_str, something like this
sinput_str([String|List]):-
sread_str(String),
sinput_str(List).
sinput_str([]).

create data tree (including node.id) using django_treebeard

I'm trying to display a directory tree from a a treebeard model. The annotated list method suggested in the treebeard tutorial works fine, but I'd like to include id information in the data tree.
The dump_bulk() has all the info I need, but as a python and django newbie I'm strugglling to find a way to extract the information and display it in the template.
I've thought about switching to javascript, and parsing the json string, but javascript doesn't like the u prefix before the string values. Is there a simple way to avoid the u prefix?
I've also thought about writing a function based on the get_annotated_list() that does include id information. I'm assuming it should be possible to overload the get_annotated_ list so that id information is included, but I'm not too sure how to tackle that either.
Any and all suggestions to help me progress along the learning curve will be appreciated.
As you probably know, get_annotated_list() will return an array of tuples, in the form (node, info). info is just a dictionary, so you can iterate over the list, and add any additional key-pairs you like. E.g.,
for node, info in my_annotated_list:
info['foo'] = node.id
Pass this to your template, and you should be fine.
You can also use a generator. This is from a project I'm working on right now:
def annotated_menu_items(initial_header, menu_items):
headings = [initial_header]
for item, info in menu_items:
yield item, info, item.is_leaf(), headings[-1:][0]
if info['open']:
headings.append(item.title)
for close in info['close']:
headings.pop()
Here, I'm adding as extra info whether the node in question is a leaf, and pushing the title from the most recently opened node onto a stack so I can access it from deeper levels of the tree.
You say you're new to Python, so you may want to read up on generators. They you materialize the elements of a (potentially infinite) list lazily. In order to use it, you invoke the function which constructs the generator, then you can treat the generator object as an iterable. E.g.,
my_fancy_menus = annotated_menu_items("My Menu", my_annotated_list)
for menu in my_fancy_menus:
do_stuff(menu)
You can also pass generators to Django templates, where they are treated like lists.

How to implement previous- and next-links in the object-detail-view of a search-result?

I am almost done with my first production-ready django-project. I got one big problem left:
I got an article-search-view that renders a list of found articles. Pagination is working just fine for the resultlist. When I click on the article-title the object-detail-page opens. What I want: previous- and next-result-links on the object-detail-page.
I tried several approaches to similar problems but didn't find a working solution. If I try to use a paginator with only one article (for the object-detail-page) I need to know that paginator-index in the resultlist. But how?
Even the .get_next(previous)_by_foo-Method is not really usable in this scenario AFAICT. Or am I missing something obvious here? Thanks for any help in advance!
Paginator from django works with lists. A way of searching indexes in lists it's like that:
['aaa', 'bbb', 'ccc'].index('bbb') # result: 1
or so like this:
model = object()
[object(), object(), model].index(model) # result: 2
Hope that gives you a hint on how you find paginator-index on your list.
If you want to get a link, at object-details-page, to next item from search-result, you must get next item from the search-result. To get a next item you need to perform the same search query which was executed in search-page and apply some extra filters to get only the next item from that list. But here you have a problem: you only have object-id in object-details page, you don't have the search-term. Without search-term you won't be able to create the search-query. That means you need to get search-term somehow. How do you get that search-term? You need to pass it from the search-result-page somehow. You can save the search-term in session/cookie, or, maybe better: you can pass it via a GET parameter to object-details page. Now when you search-term in object-display page, you can perform a search-query, and from that query you can select the next and the previous objects.
I think now you should be able to implement that. If not you could show some of your code of object-details view, maybe someone will write some code for you.
you could use this
next »
« previous