Commit 809d4c58 authored by Sebastian's avatar Sebastian

Small changes to enable specified answer_set_list on which the learning is based on

parent a879924c
......@@ -3,27 +3,33 @@ import nltk
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from Products.ERP5Type.Log import log
stemmer = nltk.stem.porter.PorterStemmer()
def stemTokens(tokens):
return [stemmer.stem(item) for item in tokens]
def Answer_getCorrectionSuggestion(self):
def Answer_getCorrectionSuggestion(self, answer_set_list=None, try_cache=True):
memcached_tool = self.getPortalObject().portal_memcached
memcached_dict = memcached_tool.getMemcachedDict(key_prefix='correctionSuggestion', plugin_path='portal_memcached/default_memcached_plugin')
try:
existing_suggestion = memcached_dict[self.getPath()]
return existing_suggestion
except KeyError:
pass
def getCorrectionSuggestion():
if try_cache:
try:
existing_suggestion = memcached_dict[self.getPath()]
return existing_suggestion
except KeyError:
pass
else:
log("Answer_getCorrectionSuggestion without caching!")
def getCorrectionSuggestion(answer_set_list):
question_id = str(self.getResourceId())
portal = self.getPortalObject()
answer_set_list = portal.portal_catalog(portal_type="Answer Set", validation_state="archived", version="001")
if answer_set_list is None:
log("Answer_getCorrectionSuggestion without trainingsdata!")
answer_set_list = portal.portal_catalog(portal_type="Answer Set", validation_state="archived", version="001")
## Get ALL the data from ERP and process within this cell so other cells do not depend on the ERP interface
# this is due to ERP being slightly unstable and causing trouble
......@@ -90,32 +96,37 @@ def Answer_getCorrectionSuggestion(self):
# Caching. Does not permanetly cache so not used anymore
#getCorrectionSuggestion = CachingMethod(getCorrectionSuggestion, "getCorrectionSuggestion@" + self.getPath())
#return getCorrectionSuggestion()
correctionSuggestion = getCorrectionSuggestion()
correctionSuggestion = getCorrectionSuggestion(answer_set_list)
memcached_dict[self.getPath()] = correctionSuggestion
return correctionSuggestion
# returns a dict with similar, suggested configurations to the Answer Set this method is called on
# the format of the returned dict is given by the Javascript processing it
def AnswerSet_getSuggestionExampleData(self):
def AnswerSet_getSuggestionExampleData(self, answer_set_list=None, try_cache=True):
memcached_tool = self.getPortalObject().portal_memcached
memcached_dict = memcached_tool.getMemcachedDict(key_prefix='correctionSuggestion', plugin_path='portal_memcached/default_memcached_plugin')
try:
existing_suggestion = memcached_dict[self.getPath()]
return existing_suggestion
except KeyError:
pass
if try_cache:
try:
existing_suggestion = memcached_dict[self.getPath()]
return existing_suggestion
except KeyError:
pass
else:
log("Answer_getSuggestionExampleData without caching!")
def getSuggestionExampleData():
def getSuggestionExampleData(answer_set_list):
portal = self.getPortalObject()
answer_set_module = portal.answer_set_module
new_answer_set = (self.getId(), getTextBlob(self))
answer_set_list = self.portal_catalog(portal_type="Answer Set", validation_state="released")
if answer_set_list is None:
answer_set_list = self.portal_catalog(portal_type="Answer Set", validation_state="released")
answer_set_list_tb = [(a.getId(), getTextBlob(a)) for a in answer_set_list]
scoring_list = rateSimilarity(new_answer_set, answer_set_list_tb)[1:]
## for testing:
......@@ -177,7 +188,7 @@ def AnswerSet_getSuggestionExampleData(self):
#getSuggestionExampleData = CachingMethod(getSuggestionExampleData, "getSuggestionExampleData@" + self.getPath())
#return getSuggestionExampleData()
exampleSuggestion = getSuggestionExampleData()
exampleSuggestion = getSuggestionExampleData(answer_set_list)
memcached_dict[self.getPath()] = exampleSuggestion
return exampleSuggestion
......
......@@ -46,7 +46,7 @@
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple>
<string>W: 35, 4: Unused variable \'example_answer\' (unused-variable)</string>
<string>W: 41, 4: Unused variable \'example_answer\' (unused-variable)</string>
</tuple>
</value>
</item>
......
erp5_survey_ml_test
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment