-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_processor.py
52 lines (42 loc) · 1.54 KB
/
text_processor.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
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
import spacy
import re
class TextProcessor:
def __init__(self):
self._nltk = nltk
self._nlp = spacy.load("en_core_web_sm")
self._link_pattern = r"http\S+"
self._markdown_link_pattern = r"\[([^\]]+)\]\((http\S+)\)"
@property
def nlp(self):
return self._nlp
def clean_text(self, txt: str) -> str:
txt = txt.lower()
txt = self.replace_links(txt)
txt = self.remove_punctuation(txt)
txt = txt.replace("\\", " ").replace("[", " ").replace("]", " ")
txt = txt.strip()
return txt
@staticmethod
def get_sentiment_scores(sentence: str) -> (float, float, float, float):
sia = SentimentIntensityAnalyzer()
scores = sia.polarity_scores(sentence)
return scores["compound"], scores["pos"], scores["neg"], scores["neu"]
@staticmethod
def remove_punctuation(txt: str) -> str:
return re.sub(r"[^\w\s\-\'\.,]", "", txt)
def replace_links(self, txt: str) -> str:
def replace_plain_links(match):
if re.match(self._markdown_link_pattern, match.group(0)):
return match.group(0)
return "<link>"
def replace_markdown_link(match):
return match.group(1)
txt = re.sub(self._markdown_link_pattern, replace_markdown_link, txt)
txt = re.sub(
r"(\[([^\]]+)\]\((http\S+)\))|" + self._link_pattern,
replace_plain_links,
txt,
)
return txt