R/search_with_py.R

Defines functions search_with_py

Documented in search_with_py

#' This function is used to search all pinyin with input one pinyin without shengdiao.
#' @author lgm
#' @param pinyin : a pin yin
#' @return vector: all words of pitches with the pin yin
#' @export
#' @examples
#' search_with_py("fu")
#' search_with_py(c("fu","hu"))
#' search_with_py("f") # search all pin yin
#' that begins with "f"

search_with_py <- function(pinyin_vector) {
	#library(stringr)
	library(dplyr)
	# search a pin yin
	search_a_py <- function(pyin) {

		library(stringr)
		library(dplyr)

		pyword1 <- pyword %>% mutate(X1=gsub(",","",.$X1))

		pyword$py <- str_replace_all(pyword1$X1,c("[āáǎà]"="a","[ōóǒò]"="o","[ēéěè]"="e","[īíǐì]"="i","[ūúǔù]"="u","[ǖǘǚǜ]"="v"))

		# if pyin such as "fu" is in pyword$X1, search the pyin;else search
		# all pinyin that begins with the pyin("f"),that one letter search.
		if (pyin %in% as.character(pyword$py)) {
			res <- pyword %>% filter(py==pyin) %>% select(-py)
		} else {
       res <- pyword %>% filter(grepl(paste0("^",pyin),pyword$py)) %>%
       	select(-py)
		}
		return(res)
	}

	# bind a list of data.frame by row into a data.frame
	bwords <- bind_rows(lapply(pinyin_vector, search_a_py)) %>%
		mutate(words = paste(X1,X2,sep="-") ) %>%
		select(words) %>% as.matrix(ncol =1)

	return(bwords)
}
Gabegit/gmdata documentation built on May 6, 2019, 5:32 p.m.