2013-04-03 51 views
-2

我有兩個txt文件,多次使用相同的詞。我設法將它們都拉入數組中,並通過插入排序格式化非格式化的txt文件之一。查找兩個文本文件中使用最多的詞

現在我需要比較兩個格式化的數組以找出最常用的單詞以及它們已被使用的次數。

我知道我可以使用for循環,通過每個數組,但我不知道如何。 有什麼幫助嗎?

編輯: 這是我到目前爲止。

#include<iostream> 
#include<fstream> 
#include<string> 
using namespace std; 

const int size = 100; 
void checkIF(string x) 
{ 
    fstream infile; 
    cout << "Attempting to open "; 
    cout << x; 
    cout << "\n"; 
    infile.open(x); 
    if(!infile) 
    { 
     cout << "Error: File couldn't be opened.\n"; 
    } 
    else 
    { 
     cout << "File opened succsesfully.\n"; 
    } 
} 
void checkFile() 
{ 
    string f1 = "text1.txt", f2 = "abbreviations.txt"; 
    checkIF(f1); 
    checkIF(f2); 
} 

string* readFiles(string txt1[],string abb[]) 
{ 
    fstream intxt1("text1.txt"); 
    fstream inabb("abbreviations.txt"); 
    int i = 0; 
    while (!intxt1.eof()) 
    { 
     intxt1 >> txt1[i]; 
     //cout << txt1[i]; 
     i++; 
    } 
     while (!inabb.eof()) 
    { 
     inabb >> abb[i]; 
     //cout << abb[i]; 
     i++; 
    } 

    return txt1; 
    return abb; 
} 

string* insertionSort(string txt1[], int arraySize) 
{ 
    int i, j; 
    string insert; 

    for (i = 1; i < arraySize; i++) 
    { 
     insert = txt1[i]; 
     j = i; 
     while ((j > 0) && (txt1[j - 1] > insert)) 
     { 
      txt1[j] = txt1[j - 1]; 
      j = j - 1; 
     } 
     txt1[j] = insert; 
    } 
    return txt1; 
} 


void compare(string txt1[],string abb[]) 
{ 

} 

void main() 
{ 
    string txt1Words[size]; 
    string abbWords[size]; 
    checkFile(); 
    readFiles(txt1Words,abbWords); 
    insertionSort(txt1Words,100); 
    compare(txt1Words,abbWords); 
    system("Pause"); 
} 
+2

向我們展示你已經嘗試了什麼。如果你這樣做,你更有可能回答。 – john

+0

根據您提供的內容,開始修改您的代碼對我們任何人都是無效的。 –

回答

0

也許你應該用一個HashMap映射每個字開始它的使用

+0

我將如何實現?我不熟悉hashmap的 – bobby

0

可以使用地圖每次找到字的時間量。

std::map<std::string, int> wordmap; 

for (int i = 0; i < arraylength; ++i) 
{ 
    ++wordmap[array[i]]; 
} 

我假設arraystd::string陣列。 之後,你可以用一個特定的單詞查詢地圖,並獲得該單詞的計數。

wordmap[word] // returns count for word 
0

使用數組時使用矢量。

string txt1Words[size]; 

vector<string> txt1Words; 

的,你可以簡單地使用

std::count(txt1Words.begin(), txt1Words.end(), word_to_search); 
+0

我怎樣才能找到一個循環,然後找到在txt1文件中使用的縮寫量?如果我執行矢量 – bobby

+0

的縮寫量?我不明白 – 2013-04-03 16:49:18

+0

我怎樣才能找到一個循環來找出abb數組中有多少個字母在txt1數組中? – bobby

0

首先讓解決 「兩個文本文件中最常用的詞」 的問題。這取決於你如何定義最常用的。你基本上有兩套有計數的單詞。

例如

文件:"apple apple apple banana"

文件B:"apple apple banana orange orange orange orange orange"

如果你把它存儲爲一組名稱,並計算你得到它

文件:{("apple",5), ("banana",1)}

文件B:{("apple",2), ("banana",1), ("orange",5)}

注意:這不是代碼,它只是一個模型符號。

那麼在這個小例子中,最常用的文件是什麼?但問題是蘋果應該是最常用的,因爲它出現在兩個文件中?還是應該使用「橙色」,因爲它在其中一個文件中使用最多?

我打算假設你想要某種交集的兩套。所以只有出現在兩個文件中的單詞纔算數。此外,如果我是你,我會通過最低排名的話,他們出現了,這種方式5「蘋果」 S在文件中不會重「蘋果」太高,因爲它只有在文件B.出現兩次

所以如果我們寫出來的代碼,你會得到這樣的事情

class Word 
{ 
public: 
    std::string Token; 
    int Count; 

    Word (const std::string &token, int count) 
     : Token(token), Count(count) {} 
}; 

std::map<std::string, int> FileA; 
    std::map<std::string, int> FileB; 

    std::vector<Word> intersection; 

    for (auto i = FileA.begin(); i != FileA.end(); ++i) 
    { 
     auto bentry = FileB.find (i->first); //Look up the word from A in B 
     if (bentry == FileB.end()) 
     { 
      continue; //The word from file A was not in file B, try the next word 
     } 

     //We found the word from A in B 
     intersection.push_back(Word (i->first,std::min(i->second,bentry->second))); //You can replace the std::min call with whatever method you want to qualitate "most common" 
    } 

    //Now sort the intersection by Count 
    std::sort (intersection.begin(),intersection.end(), [](const Word &a, const Word &b) { return a.Count > b.Count;}); 

    for (auto i = intersection.begin(); i != intersection.end(); ++i) 
    { 
     std::cout << (*i).Token << ": " << (*i).Count << std::endl; 
    } 

看見它運行: http://ideone.com/jbPm1g

我希望有所幫助。

+0

現在這個編輯使得這個沒用。 – Tocs

相關問題