### Similarword.replace Source: https://github.com/425776024/nlpcda/blob/master/README.md Performs random synonym replacement on the input text. ```APIDOC ## Similarword.replace ### Description Replaces words in the input text with their synonyms based on a provided or default synonym dictionary. ### Parameters - **base_file** (str) - Optional - Path to a text file containing synonyms separated by spaces. - **create_num** (int) - Optional - Number of augmented texts to generate (default: 3). - **change_rate** (float) - Optional - Probability of changing a word (default: 0.3). - **seed** (int) - Optional - Random seed for reproducibility. ### Usage Example ```python from nlpcda import Similarword smw = Similarword(create_num=3, change_rate=0.3) rs1 = smw.replace("这是个实体:58同城") ``` ``` -------------------------------- ### Simbert.replace Source: https://github.com/425776024/nlpcda/blob/master/README.md Generates semantically similar sentences using a pre-trained SimBERT model. ```APIDOC ## Simbert.replace ### Description Uses a SimBERT model to generate semantically similar variations of an input sentence. ### Parameters - **sent** (string) - Required - The input sentence to augment. - **create_num** (int) - Required - The number of variations to generate. ### Response - **synonyms** (list) - A list of tuples containing the augmented sentence and its similarity score. ``` -------------------------------- ### EquivalentChar.replace Source: https://github.com/425776024/nlpcda/blob/master/README.md Replaces characters in a string with their equivalent forms based on a provided or default dictionary. ```APIDOC ## EquivalentChar.replace ### Description Replaces characters in the input string with equivalent characters (e.g., numbers to Chinese numerals) to generate variations. ### Parameters - **text** (string) - Required - The input text to augment. ### Response - **result** (list) - A list of augmented strings. ``` -------------------------------- ### Add Custom Dictionaries Source: https://github.com/425776024/nlpcda/blob/master/README.md Registers custom words to improve tokenization and augmentation results for various classes. ```python from nlpcda import Randomword from nlpcda import Similarword from nlpcda import Homophone from nlpcda import RandomDeleteChar from nlpcda import Ner from nlpcda import CharPositionExchange Randomword.add_word('小明') Randomword.add_words(['小明','小白','天地良心']) # Similarword,Homophone,RandomDeleteChar 同上 ``` -------------------------------- ### Perform SimBERT-based Augmentation Source: https://github.com/425776024/nlpcda/blob/master/README.md Uses pre-trained SimBERT models to generate semantically similar sentences. ```python from nlpcda import Simbert config = { 'model_path': '/xxxx/chinese_simbert_L-12_H-768_A-12', 'CUDA_VISIBLE_DEVICES': '0,1', 'max_len': 32, 'seed': 1 } simbert = Simbert(config=config) sent = '把我的一个亿存银行安全吗' synonyms = simbert.replace(sent=sent, create_num=5) print(synonyms) ``` -------------------------------- ### Randomword.replace Source: https://github.com/425776024/nlpcda/blob/master/README.md Performs random entity replacement on the input text using either a built-in or custom entity file. ```APIDOC ## Randomword.replace ### Description Replaces entities in the input text with equivalent entities from a provided or default list. ### Parameters - **base_file** (str) - Optional - Path to a text file containing entities, one per line. - **create_num** (int) - Optional - Number of augmented texts to generate (default: 3). - **change_rate** (float) - Optional - Probability of changing a character/entity (default: 0.3). - **seed** (int) - Optional - Random seed for reproducibility. ### Usage Example ```python from nlpcda import Randomword smw = Randomword(create_num=3, change_rate=0.3) rs1 = smw.replace("这是个实体:58同城") ``` ``` -------------------------------- ### Perform Random Synonym Substitution Source: https://github.com/425776024/nlpcda/blob/master/README.md Uses the Similarword class to substitute synonyms. A custom synonym dictionary can be provided via base_file. ```python from nlpcda import Similarword test_str = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击''' smw = Similarword(create_num=3, change_rate=0.3) rs1 = smw.replace(test_str) print('随机同义词替换>>>>>>') for s in rs1: print(s) ``` -------------------------------- ### Perform Translation-based Augmentation Source: https://github.com/425776024/nlpcda/blob/master/README.md Uses Baidu or Google translation APIs to perform round-trip translation for data augmentation. ```python from nlpcda import baidu_translate zh = '天气晴朗,天气很不错,空气很好' # 申请你的 appid、secretKey # 两遍洗数据法(回来的中文一般和原来不一样,要是一样,就不要了,靠运气?) en_s = baidu_translate(content=zh, appid='xxx', secretKey='xxx',t_from='zh', t_to='en') zh_s = baidu_translate(content=en_s, appid='xxx', secretKey='xxx',t_from='en', t_to='zh') print(zh_s) ``` ```python from googletrans import Translator def googletrans(content='一个免费的谷歌翻译API', t_from='zh-cn', t_to='en'): translator = Translator() s = translator.translate(text=content, dest=t_to,src=t_from) return s.text ``` -------------------------------- ### CharPositionExchange.replace Source: https://github.com/425776024/nlpcda/blob/master/README.md Randomly swaps adjacent characters in a string to perform data augmentation. ```APIDOC ## CharPositionExchange.replace ### Description Swaps adjacent characters within a specified range to generate augmented text. Non-Chinese characters like numbers and symbols are ignored. ### Parameters - **text** (string) - Required - The input text to augment. ### Response - **result** (list) - A list of augmented strings. ``` -------------------------------- ### Perform Random Entity Replacement Source: https://github.com/425776024/nlpcda/blob/master/README.md Uses the Randomword class to replace entities in a string. You can provide a custom base_file for entity definitions. ```python from nlpcda import Randomword test_str = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击''' smw = Randomword(create_num=3, change_rate=0.3) rs1 = smw.replace(test_str) print('随机实体替换>>>>>>') for s in rs1: print(s) ``` -------------------------------- ### Ner.augment Source: https://github.com/425776024/nlpcda/blob/master/README.md Performs data augmentation on NER-labeled data files based on specified tags and augmentation size. ```APIDOC ## Ner.augment ### Description Performs data augmentation on a specific NER-labeled text file. It targets specific entity tags for augmentation and generates a set number of new samples. ### Parameters - **file_name** (string) - Required - The path to the NER-labeled training file (e.g., '0.txt'). ### Response - **data_sentence_arrs** (list) - List of augmented sentences. - **data_label_arrs** (list) - List of corresponding labels for the augmented sentences. ``` -------------------------------- ### RandomDeleteChar.replace Source: https://github.com/425776024/nlpcda/blob/master/README.md Performs random character deletion on the input text. ```APIDOC ## RandomDeleteChar.replace ### Description Randomly deletes characters from the input text while preserving specific patterns like dates and numbers. ### Parameters - **create_num** (int) - Optional - Number of augmented texts to generate (default: 3). - **change_rate** (float) - Optional - Probability of deleting a character (default: 0.3). - **seed** (int) - Optional - Random seed for reproducibility. ### Usage Example ```python from nlpcda import RandomDeleteChar smw = RandomDeleteChar(create_num=3, change_rate=0.3) rs1 = smw.replace("这是个实体:58同城") ``` ``` -------------------------------- ### Perform NER Data Augmentation Source: https://github.com/425776024/nlpcda/blob/master/README.md Augments NER-labeled data by specifying directories and tags. Requires data in standard BIO format. ```python from nlpcda import Ner ner = Ner(ner_dir_name='ner_data', ignore_tag_list=['O'], data_augument_tag_list=['P', 'LOC','ORG'], augument_size=3, seed=0) data_sentence_arrs, data_label_arrs = ner.augment(file_name='0.txt') # 3条增强后的句子、标签 数据,len(data_sentence_arrs)==3 # 你可以写文件输出函数,用于写出,作为后续训练等 print(data_sentence_arrs, data_label_arrs) ``` -------------------------------- ### baidu_translate Source: https://github.com/425776024/nlpcda/blob/master/README.md Performs data augmentation by translating text between Chinese and English using the Baidu Translate API. ```APIDOC ## baidu_translate ### Description Translates text from one language to another using Baidu's translation service. Typically used in a two-step process (e.g., ZH -> EN -> ZH) to generate paraphrased text. ### Parameters - **content** (string) - Required - The text to translate. - **appid** (string) - Required - Your Baidu API app ID. - **secretKey** (string) - Required - Your Baidu API secret key. - **t_from** (string) - Required - Source language code. - **t_to** (string) - Required - Target language code. ### Response - **translated_text** (string) - The translated output string. ``` -------------------------------- ### Perform Equivalent Character Replacement Source: https://github.com/425776024/nlpcda/blob/master/README.md Replaces characters with their equivalents based on a provided or default list. Useful for increasing text diversity. ```python from nlpcda import EquivalentChar test_str = '''今天是2020年3月8日11:40,天气晴朗,天气很不错。''' s = EquivalentChar(create_num=3, change_rate=0.3) # 添加等价字 s.add_equivalent_list(['看', '瞅']) res=s.replace(test_str) print('等价字替换>>>>>>') for s in res: print(s) ``` -------------------------------- ### Homophone.replace Source: https://github.com/425776024/nlpcda/blob/master/README.md Performs random homophone replacement on the input text. ```APIDOC ## Homophone.replace ### Description Replaces characters in the input text with their homophones based on a provided or default homophone dictionary. ### Parameters - **base_file** (str) - Optional - Path to a text file containing homophones separated by tabs. - **create_num** (int) - Optional - Number of augmented texts to generate (default: 3). - **change_rate** (float) - Optional - Probability of changing a character (default: 0.3). - **seed** (int) - Optional - Random seed for reproducibility. ### Usage Example ```python from nlpcda import Homophone smw = Homophone(create_num=3, change_rate=0.3) rs1 = smw.replace("这是个实体:58同城") ``` ``` -------------------------------- ### Perform Random Homophone Replacement Source: https://github.com/425776024/nlpcda/blob/master/README.md Uses the Homophone class to replace characters with their homophones. A custom homophone dictionary can be provided via base_file. ```python from nlpcda import Homophone test_str = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击''' smw = Homophone(create_num=3, change_rate=0.3) rs1 = smw.replace(test_str) print('随机近义字替换>>>>>>') for s in rs1: print(s) ``` -------------------------------- ### Perform Random Character Position Exchange Source: https://github.com/425776024/nlpcda/blob/master/README.md Swaps adjacent characters within a specified window size. Non-Chinese characters like numbers and symbols are ignored. ```python from nlpcda import CharPositionExchange ts = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击''' smw = CharPositionExchange(create_num=3, change_rate=0.3,char_gram=3,seed=1) rs=smw.replace(ts) for s in rs: print(s) ``` -------------------------------- ### Perform Random Character Deletion Source: https://github.com/425776024/nlpcda/blob/master/README.md Uses the RandomDeleteChar class to randomly remove characters from the input text while preserving specific patterns like dates. ```python from nlpcda import RandomDeleteChar test_str = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击''' smw = RandomDeleteChar(create_num=3, change_rate=0.3) rs1 = smw.replace(test_str) print('随机字删除>>>>>>') for s in rs1: print(s) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.