從給定句子中查詢以給定單詞作為字首的單詞
在處理自然語言處理或文字分析時,通常需要在較大的文字主體中搜索特定的單詞或短語。一項常見的任務是在句子中查詢所有以給定字首開頭的單詞。在本文中,我們將探討如何完成此任務。
演算法
讀取輸入句子和字首。
將輸入句子分解成單個單詞。
對於句子中的每個單詞,檢查它是否以給定字首開頭。
如果單詞以該字首開頭,則將其新增到匹配單詞列表中。
列印匹配單詞列表。
示例
以下是解決該問題的程式:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { // Declare variables char sentence[] = "The quick brown fox jumps over the lazy dog"; char prefix[] = "fox"; char *word = strtok(sentence, " "); // Tokenization using space as delimiter char *words[100]; // Array to store tokenized words int wordCount = 0; // Tokenize the sentence into individual words while (word != NULL) { words[wordCount++] = word; // Store each word in the array word = strtok(NULL, " "); // Get the next word } char *matches[100]; // Array to store matching words int matchCount = 0; // Check for words that start with the given prefix for (int i = 0; i < wordCount; i++) { if (strncmp(words[i], prefix, strlen(prefix)) == 0) { // Compare with prefix matches[matchCount++] = words[i]; // Store matching words } } // Print the matching words printf("Matching words:\n"); for (int i = 0; i < matchCount; i++) { printf("%s\n", matches[i]); } return 0; }
輸出
Matching words: Fox
#include <iostream> #include <string> #include <vector> using namespace std; int main() { string sentence, prefix; vector<string> words; // Read in the input sentence and prefix sentence="The quick brown fox jumps over the lazy dog"; prefix="fox"; // Tokenize the input sentence into individual words string word = ""; for (auto c : sentence) { if (c == ' ') { words.push_back(word); word = ""; } else { word += c; } } words.push_back(word); // Find all words in the sentence that start with the given prefix vector<string> matches; for (auto w : words) { if (w.substr(0, prefix.length()) == prefix) { matches.push_back(w); } } // Print the list of matching words cout << "Matching words:" << endl; for (auto m : matches) { cout << m << endl; } return 0; }
輸出
Matching words: Fox
import java.util.ArrayList; public class Main { public static void main(String[] args) { // Declare variables String sentence = "The quick brown fox jumps over the lazy dog"; String prefix = "fox"; String[] words = sentence.split(" "); // Tokenization using space as delimiter ArrayList<String> matches = new ArrayList<>(); // ArrayList to store matching words // Check for words that start with the given prefix for (String w : words) { if (w.startsWith(prefix)) { matches.add(w); // Store matching words in ArrayList } } // Print the matching words System.out.println("Matching words:"); for (String m : matches) { System.out.println(m); } } }
輸出
Matching words: Fox
sentence = "The quick brown fox jumps over the lazy dog" prefix = "fox" words = sentence.split() # Tokenization using space as delimiter matches = [w for w in words if w.startswith(prefix)] # List comprehension to find matching words print("Matching words:") for m in matches: print(m)
輸出
Matching words: Fox
測試用例示例
假設我們有以下輸入句子:
The quick brown fox jumps over the lazy dog
並且我們想要查詢所有以字首“fox”開頭的單詞。使用此輸入執行上述程式碼將產生以下輸出
在此示例中,句子中唯一以字首“fox”開頭的單詞是“fox”本身,因此它是唯一列印為匹配項的單詞。
結論
查詢句子中所有以給定字首開頭的單詞是自然語言處理和文字分析中一項有用的任務。透過將輸入句子分解成單個單詞並檢查每個單詞是否存在匹配的字首,我們可以輕鬆完成此任務。
廣告