Попробуйте что-то вроде этого:
var array = [];
function searchForStringInJsonRecursive (jsonData, str) {
for(var i in jsonData) {
// If the element is an Array or nested object continue to search
if (typeof jsonData[i] === 'object') {
searchForStringInJsonRecursive(jsonData[i], str);
}
// If the element is not an Object and matches str add the it to the array
else if (jsonData[i] === str) {
array.push(i);
}
}
}
Предполагая, что ваш json находится в переменной с именем data
, вы можете вызвать функцию следующим образом: searchForStringInJsonRecursive(data, 'str');
array
будет содержать все ключи, которые соответствие содержимого 'str'
Хорошо, проблема не в кодировке, так как все в связанном файле может быть полностью закодировано в нижнем 128-значном ASCII. Проблема была в пробелах, вызванных пустыми строками. Есть также ведущие места, которые нуждаются в удалении. Это легко сделать, используя некоторые поднаборы и некоторые операции очистки stringi .
library("quanteda")
## Package version: 1.3.14
autonomy <- readLines("~/Downloads/risktaking.txt", encoding = "UTF-8")
head(autonomy, 15)
## [1] "adventuresome" " adventurous" " audacious" " bet"
## [5] " bold" " bold-spirited" " brash" " brave"
## [9] " chance" " chancy" " courageous" " danger"
## [13] "" "dangerous" " dare"
# strip leading or trailing whitespace
autonomy <- stringi::stri_trim_both(autonomy)
# get rid of empties
autonomy <- autonomy[!autonomy == ""]
Теперь вы можете создать словарь и применить функцию quanteda.dictionaries::liwcalike()
.
# now define the quanteda dictionary
EODic <- dictionary(list(autonomy = autonomy))
txt <- c("12th Battalion Productions is producing a fully holographic feature length production. Presenting a 3D audio-visual projection without a single cast member present, to give the illusion of live stage performance.")
library("quanteda.dictionaries")
liwcalike(txt, dictionary = EODic)
## docname Segment WC WPS Sixltr Dic autonomy AllPunc Period Comma Colon
## 1 text1 1 35 15.5 34.29 0 0 11.43 5.71 2.86 0
## SemiC QMark Exclam Dash Quote Apostro Parenth OtherP
## 1 0 0 0 2.86 0 0 0 8.57