diff options
author | Aleksey Lim <alsroot@member.fsf.org> | 2009-04-14 21:41:19 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@member.fsf.org> | 2009-04-14 21:41:19 (GMT) |
commit | 9524662ce1c5f662ba684c1681178cdc9237e5b9 (patch) | |
tree | 796d7cb89dc7abd74e2565fecba2bee8177ab519 /Speak.activity/aiml/Utils.py | |
parent | d30dd27d2214652ff4c1f9c28bb92dc7640c60d5 (diff) |
Initial commit of HablarConSara
Diffstat (limited to 'Speak.activity/aiml/Utils.py')
-rwxr-xr-x | Speak.activity/aiml/Utils.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Speak.activity/aiml/Utils.py b/Speak.activity/aiml/Utils.py new file mode 100755 index 0000000..75e5a1c --- /dev/null +++ b/Speak.activity/aiml/Utils.py @@ -0,0 +1,32 @@ +"""This file contains assorted general utility functions used by other +modules in the PyAIML package. + +""" + +def sentences(s): + """Split the string s into a list of sentences.""" + try: s+"" + except: raise TypeError, "s must be a string" + pos = 0 + sentenceList = [] + l = len(s) + while pos < l: + try: p = s.index('.', pos) + except: p = l+1 + try: q = s.index('?', pos) + except: q = l+1 + try: e = s.index('!', pos) + except: e = l+1 + end = min(p,q,e) + sentenceList.append( s[pos:end].strip() ) + pos = end+1 + # If no sentences were found, return a one-item list containing + # the entire input string. + if len(sentenceList) == 0: sentenceList.append(s) + return sentenceList + +# Self test +if __name__ == "__main__": + # sentences + sents = sentences("First. Second, still? Third and Final! Well, not really") + assert(len(sents) == 4)
\ No newline at end of file |