Returns the sentence's modality as a weight between -1.0 and +1.0. Currently, the only type implemented is EPISTEMIC. Epistemic modality is used to express possibility (i.e. how truthful is what is being said).
(sentence, type=EPISTEMIC)
| 374 | } |
| 375 | |
| 376 | def modality(sentence, type=EPISTEMIC): |
| 377 | """ Returns the sentence's modality as a weight between -1.0 and +1.0. |
| 378 | Currently, the only type implemented is EPISTEMIC. |
| 379 | Epistemic modality is used to express possibility (i.e. how truthful is what is being said). |
| 380 | """ |
| 381 | S, n, m = sentence, 0.0, 0 |
| 382 | if not (hasattr(S, "words") and hasattr(S, "parse_token")): |
| 383 | raise TypeError, "%s object is not a parsed Sentence" % repr(S.__class__.__name__) |
| 384 | if type == EPISTEMIC: |
| 385 | r = S.string.rstrip(" .!") |
| 386 | for k, v in epistemic_weaseling.items(): |
| 387 | for phrase in v: |
| 388 | if phrase in r: |
| 389 | n += k |
| 390 | m += 2 |
| 391 | for i, w in enumerate(S.words): |
| 392 | for type, dict, weight in ( |
| 393 | ( "MD", epistemic_MD, 4), |
| 394 | ( "VB", epistemic_VB, 2), |
| 395 | ( "RB", epistemic_RB, 2), |
| 396 | ( "JJ", epistemic_JJ, 1), |
| 397 | ( "NN", epistemic_NN, 1), |
| 398 | ( "CC", epistemic_CC_DT_IN, 1), |
| 399 | ( "DT", epistemic_CC_DT_IN, 1), |
| 400 | ( "IN", epistemic_CC_DT_IN, 1), |
| 401 | ("PRP" , epistemic_PRP, 1), |
| 402 | ("PRP$", epistemic_PRP, 1), |
| 403 | ( "WP" , epistemic_PRP, 1)): |
| 404 | # "likely" => weight 1, "very likely" => weight 2 |
| 405 | if i > 0 and s(S[i-1]) in MODIFIERS: |
| 406 | weight += 1 |
| 407 | # likely" => score 0.25 (neutral inclining towards positive). |
| 408 | if w.type and w.type.startswith(type): |
| 409 | for k, v in dict.items(): |
| 410 | # Prefer lemmata. |
| 411 | if (w.lemma or s(w)) in v: |
| 412 | # Reverse score for negated terms. |
| 413 | if i > 0 and s(S[i-1]) in ("not", "n't", "never", "without"): |
| 414 | k = -k * 0.5 |
| 415 | n += weight * k |
| 416 | m += weight |
| 417 | break |
| 418 | # Numbers, citations, explanations make the sentence more factual. |
| 419 | if w.type in ("CD", "\"", "'", ":", "("): |
| 420 | n += 0.75 |
| 421 | m += 1 |
| 422 | if m == 0: |
| 423 | return 1.0 # No modal verbs/adverbs used, so statement must be true. |
| 424 | return max(-1.0, min(n / (m or 1), +1.0)) |
| 425 | |
| 426 | def uncertain(sentence, threshold=0.5): |
| 427 | return modality(sentence) <= threshold |