Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tokenizer instead of split #145

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions wordview/text_analysis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import plotly.graph_objs as go
from langdetect import detect
from nltk.corpus import stopwords
from nltk.tokenize import sent_tokenize
from nltk.tokenize import sent_tokenize, word_tokenize
from plotly.subplots import make_subplots
from tqdm import tqdm
from wordcloud import WordCloud, get_single_color_func
Expand Down Expand Up @@ -337,25 +337,30 @@ def get_pos(tagged_tokens: List[Tuple[str, str]], goal_pos: str) -> List:
ls = detect(text).upper()
languages.update([ls])
try:
tokens = text.lower().split(" ")
doc_lengths.append(len(tokens))
doc_len = 0
doc_tokens = []
sentences = sent_tokenize(text.lower())
for sentence in sentences:
sentence_tokens = sentence.split(" ")
sentence_tokens = word_tokenize(sentence)
sentence_lengths.append(len(sentence_tokens))
doc_len += len(sentence_tokens)
doc_tokens.extend(sentence_tokens)
doc_lengths.append(doc_len)
if skip_stopwords_punc:
tokens = [
t for t in tokens if t not in stop_words and t not in punctuations
doc_tokens = [
t
for t in doc_tokens
if t not in stop_words and t not in punctuations
]
update_count(token_to_count_dict, tokens)
update_count(token_to_count_dict, doc_tokens)

except Exception as e:
logger.warning(
"Processing entry --- %s --- lead to exception: %s" % (text, e.args[0])
)
continue

postag_tokens = nltk.pos_tag(tokens)
postag_tokens = nltk.pos_tag(doc_tokens)

for pos in pos_tags:
pos_items = get_pos(postag_tokens, pos)
Expand Down
Loading