技术求助:如何按句号、逗号及but拆分目标英文语句
Got it, let's solve this! You want to split your target sentence not just on periods, but also commas and the conjunction "but"—here's a clean, reliable way to do this using Python (one of the most common languages for string manipulation tasks like this).
First, let's restate your input sentence for clarity:
Very disorganized and hard professor. Does not come to classes on time, she grades tough, does not help on anything. She says come for help but when you go to her office hour, she is not there to help.
Step-by-Step Implementation
We'll use Python's re module (regular expressions) because it lets us define multiple, precise split patterns—including ensuring "but" is treated as a standalone word (not part of longer words like "butter").
Code Example
import re # Your original sentence sentence = "Very disorganized and hard professor. Does not come to classes on time, she grades tough, does not help on anything. She says come for help but when you go to her office hour, she is not there to help." # Define the split pattern: matches periods, commas, or the standalone word "but" # \s* handles spaces around separators so we don't get empty/whitespace-only parts split_pattern = r'\s*(?:\.|,|\bbut\b)\s*' # Perform the split split_result = re.split(split_pattern, sentence) # Filter out any empty strings that might result from trailing separators cleaned_result = [part for part in split_result if part] # Print the final split parts print(cleaned_result)
What the Pattern Does
Let's break down the regex pattern r'\s*(?:\.|,|\bbut\b)\s*':
\s*: Matches any number of spaces (including zero) before/after the separator—this ensures our split parts don't have leading/trailing whitespace.(?:...): A non-capturing group that groups our separators without storing them (we don't need the separators in our result).\.: Matches a literal period (we escape it with\because.is a special character in regex).|: Acts as an "OR" operator between our separators.,: Matches a literal comma.\bbut\b: Matches the standalone word "but"—\bis a word boundary, so it won't match "but" inside longer words (like "butterfly").
Output
Running the code will give you this clean list of split parts:
['Very disorganized and hard professor', 'Does not come to classes on time', 'she grades tough', 'does not help on anything', 'She says come for help', 'when you go to her office hour', 'she is not there to help']
Alternative for Non-Regex Approach (If You Prefer)
If you don't want to use regex, you can chain replacements to turn all target separators into a single separator, then split:
sentence = "Very disorganized and hard professor. Does not come to classes on time, she grades tough, does not help on anything. She says come for help but when you go to her office hour, she is not there to help." # Replace "but" with a temporary separator (like "|"), then replace commas/periods with the same processed = sentence.replace(" but ", " | ").replace(". ", " | ").replace(", ", " | ") # Split on the temporary separator and clean up split_result = [part.strip() for part in processed.split("|") if part.strip()] print(split_result)
Note: This approach is simpler but less robust—it relies on "but" being surrounded by spaces, whereas the regex method handles cases where "but" might have varying whitespace (or even no space, though that's rare in proper grammar).
内容的提问来源于stack exchange,提问作者AnjuT

