-1

I want to perform some operation like string search, iterate, split etc on sentence. I checked couple of Prolog tuts but no help - http://kti.ms.mff.cuni.cz/~bartak/prolog/contents.html, http://www.bitwisemag.com/copy/programming/prolog/intro/firststeps.html. I wanted to write program like this :

for linking in post_script_link_list:
        sub_link = re.search('\((.*?)\)',linking).group(1)
        if sub_link in links_list1:         
            if ('Who').lower() in sent_split or 'Who' in sent_split:
                result = 'person'
                break
            elif (('Where').lower() in sent_split or 'Where' in sent_split) and 'What' not in read_str and 'what' not in read_str:
                result = 'location'
                break
            elif ('Why').lower() in sent_split or 'Why' in sent_split:
                result = 'reason'
                break
            elif ('How many').lower() in read_str or 'How many' in read_str:
                result = 'count'
                break
            elif (('When').lower() in sent_split or 'When' in sent_split) and 'What' not in read_str and 'what' not in read_str:
                result = 'time'
                break
            elif (('How').lower() in sent_split or 'How' in sent_split) and ('How long').lower() not in read_str and 'How long' not in read_str and ('How much').lower() not in read_str and 'How much' not in read_str:
                result = 'manner'
                break
        elif sub_link in links_list2:
            check_yn = verify_yesno(post_script_link_list)
            if check_yn == 1:
                result = 'Yes/No'
                break
            elif check_yn == 0:
                result = 'Not found'
                break
            break
        else:
            result = 'Not found'

is it possible to perform usnig prolog? I am using pyswip and python

UPDATE

1 ?- split_string("a.b.c.d", ".", "", L).
ERROR: toplevel: Undefined procedure: split_string/4 (DWIM could not correct goal)
user123
  • 5,269
  • 16
  • 73
  • 121
  • 2
    Why do you want to use prolog? What is wrong with what you have? Do you have any experience with prolog or other functional programming languages? – Vincent Beltman Jan 08 '15 at 09:50
  • @VincentBeltman: Yes, I have little experience with prolog, where there were simple rules, which was fine. While working on rules based system, it become simple to manage using prolog. In case of python, every time need to edite code and add if ... else and code become too nested, complicated – user123 Jan 08 '15 at 09:57
  • 1
    I see a lot of enhancements for your code. I could clean it up a little. Or you could post your code on code review. – Vincent Beltman Jan 08 '15 at 10:01
  • 1
    If you are using SWI-Prolog, there is also "strings": http://www.swi-prolog.org/pldoc/man?section=strings The predicates on "strings" are a bit closer in semantics to what you are trying to achieve than atom and code list predicates. This won't make your work much easier, though, it seems you rather have a problem with organizing your thoughts. –  Jan 08 '15 at 10:21
  • @VincentBeltman: I appreciate if suggest cleaning @ https://codereview.stackexchange.com/questions/76956/pytho-code-for-rules-based-system – user123 Jan 08 '15 at 11:04
  • Are you talking about string processing, or more generally, natural language processing? Or what is the bigger picture? There are some known patters for NLP in Prolog. As far as avoiding addition of lots of `if-else` constructs in Python, you might consider a more data driven approach rather than and *ad hoc* approach to scanning for and acting on words. – lurker Jan 08 '15 at 11:30
  • @luker: yes, for NLP. Rules keep varying and increasing, I appreciate if you suggest if any 'library or package' for NLP in prolog exist. Bigger picture here is `identifying the answer type` for input query – user123 Jan 08 '15 at 11:40

1 Answers1

2

To check if a string contains a particular substring, you can use append/3 or sub_atom/5, depending how you represent your string.

If your strings are atoms, use sub_atom/5:

substring(Atom, Substring):-
    sub_atom(Atom, _, _, _, Substring).

?- substring('... Where is that?', 'Where').
true ;
false.

If your strings are lists of codes, use append/3:

substring(Codes, Substring):-
    append(S, _, Codes),
    append(_, Substring, S).

?- substring(".. Where is that?", "Where").
true ;
false.
Tudor Berariu
  • 4,910
  • 2
  • 18
  • 29