我正在進行各種單詞列表的廣泛工作。將單詞列表轉換爲這些單詞出現的頻率列表
請考慮以下問題,我有:
docText={"settlement", "new", "beginnings", "wildwood", "settlement", "book",
"excerpt", "agnes", "leffler", "perry", "my", "mother", "junetta",
"hally", "leffler", "brought", "my", "brother", "frank", "and", "me",
"to", "edmonton", "from", "monmouth", "illinois", "mrs", "matilda",
"groff", "accompanied", "us", "her", "husband", "joseph", "groff",
"my", "father", "george", "leffler", "and", "my", "uncle", "andrew",
"henderson", "were", "already", "in", "edmonton", "they", "came",
"in", "1910", "we", "arrived", "july", "1", "1911", "the", "sun",
"was", "shining", "when", "we", "arrived", "however", "it", "had",
"been", "raining", "for", "days", "and", "it", "was", "very",
"muddy", "especially", "around", "the", "cn", "train"}
searchWords={"the","for","my","and","me","and","we"}
這些列表的要長得多(說250個字的searchWords
列表和docText
爲約12000字)。
現在,我必須做這樣的事情找出給定單詞的頻率的能力:
docFrequency=Sort[Tally[docText],#1[[2]]>#2[[2]]&];
Flatten[Cases[docFrequency,{"settlement",_}]][[2]]
但是,在我收到掛了是我的追求,以生成特定列表。具體而言,將單詞列表轉換爲這些單詞出現的頻率列表的問題。我試圖用Do
循環來做到這一點,但已經撞牆了。
我想通過docText
與searchWords
並用純粹的外觀頻率替換docText的每個元素。即因爲「結算」出現兩次,它將在列表中被替換爲2,而由於「我」出現3次,它將變爲3.然後列表將是類似2,1,1,1,2等等的東西向前。
我懷疑答案在If[]
和Map[]
之間?
這聽起來不可思議,但我想預先處理一堆的詞頻信息...
加成的透明度(我希望):
這裏是一個更好的例子。
searchWords={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "A", "about",
"above", "across", "after", "again", "against", "all", "almost",
"alone", "along", "already", "also", "although", "always", "among",
"an", "and", "another", "any", "anyone", "anything", "anywhere",
"are", "around", "as", "at", "b", "B", "back", "be", "became",
"because", "become", "becomes", "been", "before", "behind", "being",
"between", "both", "but", "by", "c", "C", "can", "cannot", "could",
"d", "D", "do", "done", "down", "during", "e", "E", "each", "either",
"enough", "even", "ever", "every", "everyone", "everything",
"everywhere", "f", "F", "few", "find", "first", "for", "four",
"from", "full", "further", "g", "G", "get", "give", "go", "h", "H",
"had", "has", "have", "he", "her", "here", "herself", "him",
"himself", "his", "how", "however", "i", "I", "if", "in", "interest",
"into", "is", "it", "its", "itself", "j", "J", "k", "K", "keep", "l",
"L", "last", "least", "less", "m", "M", "made", "many", "may", "me",
"might", "more", "most", "mostly", "much", "must", "my", "myself",
"n", "N", "never", "next", "no", "nobody", "noone", "not", "nothing",
"now", "nowhere", "o", "O", "of", "off", "often", "on", "once",
"one", "only", "or", "other", "others", "our", "out", "over", "p",
"P", "part", "per", "perhaps", "put", "q", "Q", "r", "R", "rather",
"s", "S", "same", "see", "seem", "seemed", "seeming", "seems",
"several", "she", "should", "show", "side", "since", "so", "some",
"someone", "something", "somewhere", "still", "such", "t", "T",
"take", "than", "that", "the", "their", "them", "then", "there",
"therefore", "these", "they", "this", "those", "though", "three",
"through", "thus", "to", "together", "too", "toward", "two", "u",
"U", "under", "until", "up", "upon", "us", "v", "V", "very", "w",
"W", "was", "we", "well", "were", "what", "when", "where", "whether",
"which", "while", "who", "whole", "whose", "why", "will", "with",
"within", "without", "would", "x", "X", "y", "Y", "yet", "you",
"your", "yours", "z", "Z"}
這些是從WordData[]
自動生成的停用詞。所以我想比較這些詞與docText。由於「結算」不是searchWords
的一部分,因此它會顯示爲0.但由於「我的」是searchWords
的一部分,因此它會彈出作爲計數(所以我可以告訴給定詞出現多少次)。
我真的很感謝你的幫助 - 我很期待能夠參加一些正式課程,因爲我碰到了能夠真正解釋我想要做什麼的邊緣!
您是否需要處理那些出現在searchWords中的單詞? 「docWords」中的其餘內容會發生什麼? – Szabolcs
@Szabolcs如果他們沒有出現,他們應該顯示爲0.在以前的程序中,我使用了'If'將其轉換爲0,因爲我會得到null問題。 –
我還是完全不明白。你能解釋一下'searchWords'的作用嗎? – Szabolcs