Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Wit.ai document type determiner #63

Merged
merged 23 commits into from
Oct 28, 2017
Merged

Added Wit.ai document type determiner #63

merged 23 commits into from
Oct 28, 2017

Conversation

Awinja-j
Copy link
Contributor

@Awinja-j Awinja-j commented Oct 13, 2017

We are currently using keywords to determine which document type the application should query from, which limits to the application to some words. Wit.ai will identify key words in queries and determine which document type it should query.

This PR therefore introduces:

  • wit.ai message API which returns the extracted meaning from a sentence, based on the app data.

This task involved:

  • opening a wit.ai account
  • Training wit.ai to check for the 8 entities that health tools uses which are:
    doctors, nurses, clinical-officers,health-facilities,nhif-inpatient,nhif-outpatient,nhif-outpatient-cs and query

wit.ai is suitable for the /search endpoint and further SMS function

It will enable users do searches like:
http://127.0.0.1:5000/search?q=is Doctor Jane Wairimu registered?

screen shot 2017-10-16 at 11 49 28 am

or send texts like:
where are civil servants outpatient services offered in kerugoya?

screen shot 2017-10-16 at 11 50 04 am

Relevant pivotal tracker stories

#51

This checks the doc type against the doc list an determines whether it should be elastic search or nurses search.
If the doc type is empty,search type is None
"""
doc = ['nhif-outpatient', 'nhif-inpatient', 'nhif-outpatient-cs', 'doctors', 'health-facilities', 'clinical-officers']
Copy link
Contributor

@RyanSept RyanSept Oct 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of a nitpick but lines 30-33 aren't pep8

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick is so important in these uncertain times.. 💯

'result': {'hits': [], 'total': 0},
'doc_type': doc_type,
'status': 'FAILED',
'msg': '' # TODO: Pass run_query message here.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have this TODO here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's me. We should pass an actual message if this fails. Currently we don't have any message being returned so we only know it's failed but not why.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, @Awinja-Andela why are these changes reflecting here? I thought I had already written this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DavidLemayian i am reusing this code for wit.ai. I am using it just so that we can have two options for searching for the document name.
using wit.ai or using document.py
It is either i do this, or i remove document.py completely.

'msg': '' # TODO: Pass run_query message here.
})

# TODO: Log event here (send to Google Analytics)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is me again. Notes in code to get back to. Showing where we should do stuff.



blueprint = Blueprint('search_api', __name__)


@blueprint.route('/search', methods=['GET'], strict_slashes=False)
def search_index(doc_type=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Awinja-Andela Why are we creating a separate function for what's already below as "index"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DavidLemayian notice that i have introduced :
from healthtools.search.wit_ai import wit_run_query
while still retaining:
from healthtools.search import run_query

@blueprint.route('/wit_search', methods=['GET'], strict_slashes=False)
that uses:
result, doc_type = wit_run_query(query, doc_type)

and
@blueprint.route('/search', methods=['GET'], strict_slashes=False) @blueprint.route('/search/<doc_type>', methods=['GET'], strict_slashes=False)
that still uses:
result, doc_type = run_query(query, doc_type)


#wit.ai access token

access_token = os.getenv("access_token")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formating and naming on these changes do not follow the rest of the project..

@Awinja-j Awinja-j changed the title Wit.ai Added Wit.ai document type determiner Oct 16, 2017
resp = client.message(message_text)
query = ''.join(nested_lookup('value', resp['entities']['query']))
doc_type = ''.join([var for var in (resp['entities'].keys()) if var != 'query'])
#wit returns the entity with an underscore,this changes it to hyphen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you see it.

search_type = 'nurses'
else:
search_type = 'elastic'
return search_type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already defined in the DOCUMENTS. Why an entire function for it? https://github.com/CodeForAfricaLabs/HealthTools.API/blob/develop/healthtools/documents.py


################################################################################

#Wit.ai Access Token
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you see it.

@DavidLemayian
Copy link
Contributor

@Awinja-Andela I can kind of see what's happening here now.

So as to avoid replication, let's approach this as a doc-type.

When you search using https://api.healthtools/search/wit?q=Dr+Ouma it then determines the doc type using wit.ai. Make sense?

@Awinja-j
Copy link
Contributor Author

@DavidLemayian yes. i see. let me try it out.

@Awinja-j
Copy link
Contributor Author

@DavidLemayian check it out!!!

@DavidLemayian
Copy link
Contributor

This is shaping up nicely @Awinja-Andela. Now what I'd like is for determine_doc_type be the only function wit_ai runs. It should return only the query and doc_type. Makes sense?

@Awinja-j
Copy link
Contributor Author

@DavidLemayian Makes Sense. I have changed it to return query and doc_type

@Awinja-j
Copy link
Contributor Author

@DavidLemayian you will see a lot has changed now.
wit_ai.py is no more.
there's a new function in query.py
and run_query fucntion has changed to accomodate the new function
And search_api.py no longer checks if doc_type is 'wit'

@DavidLemayian DavidLemayian merged commit a6aeeb9 into CodeForAfrica:develop Oct 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants