forked from asrdav/stackoverflow-answer-predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperties_text.py
62 lines (55 loc) · 1.66 KB
/
Properties_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import re
from collections import Counter
from enchant.checker import SpellChecker
from textblob import TextBlob
from rake_nltk import Rake
class TextProperties:
@staticmethod
def NonstopWords(text):
text = re.sub(r'[^\w\s]', ' ', text)
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(text)
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
return len(filtered_sentence)
@staticmethod
def UniqueWords(text):
text = re.sub(r'[^\w\s]', ' ', text)
c = Counter(text.split())
c = set(c)
return len(c)
@staticmethod
def spellcheck(text):
chkr = SpellChecker("en_US")
chkr.set_text(text)
c = 0
for _ in chkr:
c = c + 1
return c
@staticmethod
def subjective(text):
sub = TextBlob(text).sentiment.subjectivity
return sub
@staticmethod
def relevancy(answer, qBody, qTags):
r = Rake()
r.extract_keywords_from_text(answer)
keyfromans = r.get_ranked_phrases()
r.extract_keywords_from_text(qBody)
keyfromque = r.get_ranked_phrases()
value = 100
try:
for anskey in keyfromans:
for quekey in keyfromque:
if anskey == quekey:
value = value + 100
for anskey in keyfromans:
if anskey in qTags:
value = value + 40
except:
value = 0
return value