I have a bug in this code. The same text is being processed twice. instead of two…
- User
I have a bug in this code. The same text is being processed twice. instead of two separate testimonies.
""" import random import time import pandas as pd import matplotlib.pyplot as plt import spacy
def segment_hebrew_spacy(text, nlp): doc = nlp(text) return [sent.text for sent in doc.sents]
def test_segment_hebrew_spacy(text, nlp=None): """ Mock segmentation: instead of using spaCy sentences, split the text into chunks of ~10 words. A small random jitter is added so chunks aren't all exactly 600. """ words = text.split() segments = [] i = 0
codewhile i < len(words): # Add some randomness to the split size (e.g., 580–620 words) chunk_size = 10 segment = words[i:i + chunk_size] segments.append(" ".join(segment)) i += chunk_size return segmentsdef add_tid_order(df): df['order'] = df.groupby(['Identifier']).cumcount() df['tid'] = df['Identifier'].str.split(".").str[-1].astype(str) df['tid_order'] = df['tid'] + '_' + df['order'].astype(str) return df
def preprocess_text_segments(df: pd.DataFrame, max_words: int) -> pd.DataFrame: """ Divide the text into segments of approx max_words words each. Make sure to not split sentences. Add a column "segment_id" to the dataframe to identify each segment. The ids should be in the format <testimony_id>_<segment_number>. 1. If a testimony has less than max_words, it should have a single segment with segment_number 0. 2. If a testimony has more than max_words, it should be split into multiple segments, each with segment_number starting from 0. """ all_segments = []
code - Assistant
I see the problem. Inside your loop over testimonies, you’re reprocessing the whole dataframe (df) each time, instead of just the current row. Specifically, in preprocess_text_segments: pythonCopy codefor idx, row in df.iterrows(): identifier = row['Identifier'] testimony_id = identifier.split(".")[-1] df['sentences'] = df['text'].apply(test_segment_hebrew_spacy, nlp=nlp)
- User
Ok. Implement whats missing
def preprocess_text_segments(df: pd.DataFrame, max_words: int) -> pd.DataFrame: """ Divide the text into segments of approx max_words words each. Make sure to not split sentences. Add a column "segment_id" to the dataframe to identify each segment. The ids should be in the format <testimony_id>_<segment_number>. 1. If a testimony has less than max_words, it should have a single segment with segment_number 0. 2. If a testimony has more than max_words, it should be split into multiple segments, each with segment_number starting from 0. """ all_segments = []
- Assistant
- User
How can I remove the signature at the end here?
""" אבל אני חושבת שבגלל שבגלל שלחם סולו, אנחנו לא נשארים כלום בעיני טרוריסטים בישראל. - ואתם עדיין בגלל שלחם סולו? - כן. - אתם לא רוצים ללכת ללכת לפיליפינס? - אני חושבת שיש לי זמן לדבר על זה, אבל בגלל שהמי גדולה גם צריכה אותי, בגלל שאני צריכה לחלוק, אני צריכה להיות מי גדולה עם המי גדולה, בגלל שהמי גדולות היו לגבי החולה. - האם אתם תגיד להם מה קרה? - כן. - הם חושבים עליך? - כן. ומרבה פרנסים קלו ומאזינים אותי אם אתם אוקיי. אז אני אמרתי, תודה לדבר, אנחנו אוקיי. - האם אתה רוצה לחזור לנירים? - כן, בטח. - - - - כן. - אני רוצה לנירים, בגלל שהמלחמה תיגמר, כמובן, לא, לא היום. - - - - - כן. - - - - - - - - - אני מקווה בשבילכם, בקרוב. - ואז נשאלת השאלה האם בכלל יכול להיות שקט עם שני מיליון ערבים בהישג יד. אז עכשיו מציירים איזה פס טשטוש של קילומטר רוחב שאסור להיכנס. - לא יודע. אחרי מה שקרה כבר קשה להאמין. - אתה רוצה להגיד לנו משהו אולי על יומי ועל איך שהיא הייתה בשבעה באוקטובר? תגיד לנו משהו על יומי, תספר לנו על יומי. - היא מאוד נאמנה והיא לא עוזבת אותי לרגע. את רואה גם פה, אני שואל אותה, אני אומר לה, למשל, הבוקר אני הלכתי... - - ... הלכתי לבריכה. אמרתי לה, את יכולה רק להמשיך לישון. היא לא התקרמה והולכת איתי יחד עד הבריכה. ויש לה משפחה בפיליפינים, ילדים, נכדים, והיא כל הזמן, לא כל הזמן, אבל הרבה מאוד בטלפון, ואני גם כן מדבר עם הבת שלה ועם הנכדה שלה, קשה, קשה מאוד. והיא עתה פעם אחת נסעה לחופשה וחזרה. אחרי שהיא חזרה זה היה קשה. שוב, להתרגל לניתוק, קשה, קשה מאוד. אבל פה עכשיו יש לה יתרון, שיש פה, אמרו לי, 40 פיליפיניות. אז זה אחרת ממה שמנהלים. אתה מתנהל פה במקום שאנחנו נמצאים, שאנחנו מראיינים אותך, שזה, תספר לנו איפה אנחנו נמצאים כאן. מה זה המקום הזה? שמה? -איפה אנחנו נמצאים עכשיו? איפה אנחנו מראיינים אותך? מה זה המקום הזה? זה מקום מושב זקנים, ששימש אותנו למקלט, והממשלה, שהיו צריכים לפנות, אז פינו לכל מיני מקומות, לבתי מלון, כמו שהיה באילת, ואנחנו התפנינו להנה. היו פה דירות פנויות, אז יש פה שבעה חברי נירים. וזה אחרת לגמרי מאשר כשהייתי אצל אחד הבנים שלי, שזה אומנם, כמו שאמרתי לכם, פינוק שאין דוגמתו, אבל לבד. ופה יש יתרון שיש בכל זאת. -חברים. כן. תומלל באמצעות שירות התמלול של ivrit.ai """
- Assistant
The bit you want to remove is this part at the very end: Copy codeתומלל באמצעות שירות התמלול של ivrit.ai
That’s the transcription service signature added by ivrit.ai.
✅ How to remove it in Python If all your texts end with that signature (or something very similar), you can strip it out safely: pythonCopy codedef remove_signature(text: str) -> str: signature = "תומלל באמצעות שירות התמלול של ivrit.ai" return text.replace(signature, "").strip()
Usage: pythonCopy codeclean_text = remove_signature(raw_text)
🔎 More general (regex version) If sometimes there are small variations (like extra spaces, punctuation, or line breaks), you can use a regex: pythonCopy codeimport re
def remove_signature(text: str) -> str: return re.sub(r"[-–—\s]תומלל באמצעות שירות התמלול של ivrit.ai\s$", "", text).strip()
That will delete the signature only if it appears at the end of the text.
Do you want me to adjust this so it works across your whole dataset (e.g., clean the text column of your DataFrame before segmentation)?