Note that there are some explanatory texts on larger screens.

plurals
  1. POI might be confused about std::set works. My code isn't working right
    primarykey
    data
    text
    <p>I'm making a plagiarism detection program. The finished product will compare two entire text documents sentence-by-sentence; at this point I'm just testing my algorithm for comparing to sentences and giving a number between 0 and 1 for how similar their words are. </p> <p>I'll try to step through the code and show you what the problem is.</p> <p>Directives and a function declaration:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;algorithm&gt; #include &lt;math.h&gt; #include &lt;set&gt; double sntnc_cmpr_qtnt(const std::vector&lt;std::string&gt;&amp;, const std::vector&lt;std::string&gt;&amp;); </code></pre> <p>The main takes two arrays of strings and puts them into vectors. I know this seems useless, but it's just for my testing purposes. I calculate the sentence comparison quotient between the two vectors of strings (which are supposed to be 2 sentences). </p> <pre><code>int main (int argc, char* const argv[]) { std::string arr1[] = {"Yo", "dawg", "I", "heard", "you", "like", "functions", "so", "we", "put", "a", "function", "inside"}; std::vector&lt;std::string&gt; str1, str2; for (int i = 0; i &lt; sizeof(arr1)/sizeof(std::string); ++i) str1.push_back(arr1[i]); std::string arr2[] = {"Yo", "dawg", "I", "heard", "you", "like", "cars", "so", "we", "put", "a", "car", "inside"}; for (int i = 0; i &lt; sizeof(arr2)/sizeof(std::string); ++i) str2.push_back(arr2[i]); std::cout &lt;&lt; sntnc_cmpr_qtnt(str1, str2); return 0; } </code></pre> <p>Here's the sentence comparison quotient function. It counts the number of words in common between the 2 sentences. </p> <p>Something is going wrong, though. My count ("cnt") is coming out to 158, which is obviously too high. I can't figure out why that is. </p> <pre><code>double sntnc_cmpr_qtnt(const std::vector&lt;std::string&gt;&amp; s1, const std::vector&lt;std::string&gt;&amp; s2) { // Place the words of sentences s1 and s2 each into seperate sets s1_set and s2_set: std::set&lt;std::string&gt; s1set, s2set; for (std::vector&lt;std::string&gt;::const_iterator it = s1.begin(); it != s1.end(); ++it) s1set.insert(*it); for (std::vector&lt;std::string&gt;::const_iterator it = s2.begin(); it != s2.end(); ++it) s2set.insert(*it); /* Compute the proportion of words in common between str1_set and str2_set, multiped by 1 over 1 minus the squareroot of the size of the smaller set. This is the sentence comparison quotient that is returned. */ double cnt(0.0); for (std::set&lt;std::string&gt;::iterator it1 = s1set.begin(); it1 != s1set.end(); ++it1) { for (std::set&lt;std::string&gt;::iterator it2 = s2set.begin(); it2 != s2set.end(); ++it2) { if ((*it1).compare(*it2)) cnt += 1.0; } } if (cnt == 0.0) { return 0.0; } else { double minsz = (double)std::min(s1set.size(), s2set.size()); return ((1-1/sqrt(minsz))*cnt/minsz); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload